-
Notifications
You must be signed in to change notification settings - Fork 7
/
MovesSquaredStrings.cs
97 lines (77 loc) · 2.92 KB
/
MovesSquaredStrings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//https://www.codewars.com/kata/moves-in-squared-strings-i/train/csharp
//https://www.codewars.com/kata/56dbe0e313c2f63be4000b25/solutions/csharp
//This kata is the first of a sequence of four about "Squared Strings".
//You are given a string of n lines, each substring being n characters long: For example:
//s = "abcd\nefgh\nijkl\nmnop"
//We will study some transformations of this square of strings.
//Vertical mirror: vert_mirror (or vertMirror or vert-mirror)
//vert_mirror(s) => "dcba\nhgfe\nlkji\nponm"
//Horizontal mirror: hor_mirror (or horMirror or hor-mirror)
//hor_mirror(s) => "mnop\nijkl\nefgh\nabcd"
//or printed:
//vertical mirror |horizontal mirror
//abcd --> dcba |abcd --> mnop
//efgh hgfe |efgh ijkl
//ijkl lkji |ijkl efgh
//mnop ponm |mnop abcd
//# Task:
//Write these two functions
//and
//high-order function oper(fct, s) where
//fct is the function of one variable f to apply to the string s (fct will be one of vertMirror, horMirror)
//#Examples:
//s = "abcd\nefgh\nijkl\nmnop"
//oper(vert_mirror, s) => "dcba\nhgfe\nlkji\nponm"
//oper(hor_mirror, s) => "mnop\nijkl\nefgh\nabcd"
//Note:
//The form of the parameter fct in oper changes according to the language. You can see each form according to the language in "Sample Tests".
//Bash Note:
//The input strings are separated by , instead of \n. The ouput strings should be separated by \r instead of \n. See "Sample Tests".
//Forthcoming katas will study other tranformations.
namespace CodeWars
{
public static class MovesSquaredStrings
{
//vertical mirror
//abcd\nefgh\nijkl\nmnop -> dcba\nhgfe\nlkji\nponm
//abcd dcba
//efgh hgfe
//ijkl lkji
//mnop ponm
public static string VertMirror(string s)
{
return string.Join("\n", s.Split("\n").Select(c => new string(c.Reverse().ToArray())));
}
//horizontal mirror
//abcd\nefgh\nijkl\nmnop -> mnop\nijkl\nefgh\nabcd
//abcd mnop
//efgh ijkl
//ijkl efgh
//mnop abcd
public static string HorMirror(string s)
{
return string.Join("\n", s.Split("\n").Reverse());
}
public static string Oper(Func<string, string> fct, string s)
{
return fct(s);
}
// Best Practices
// public static string VertMirror(string strng)
// {
// return string.Join("\n", strng.Split('\n').Select(i => string.Concat(i.Reverse())));
// }
// public static string HorMirror(string strng)
// {
// return string.Join("\n", strng.Split('\n').Reverse());
// }
// public static string Oper(Func<string, string> fct, string s)
// {
// return fct(s);
// }
}
}