-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGive_me_a_Diamond.cs
77 lines (59 loc) · 2.13 KB
/
Give_me_a_Diamond.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
using System.Text;
namespace CodingChallenges;
[TestClass]
public class Give_me_a_Diamond {
[TestMethod]
public void Test() {
/*
Give me a Diamond
Jamie is a programmer, and James' girlfriend. She likes diamonds, and wants a diamond string from James. Since James doesn't know how to make this happen, he needs your help.
Task
You need to return a string that looks like a diamond shape when printed on the screen, using asterisk (*) characters. Trailing spaces should be removed, and every line must be terminated with a newline character (\n).
Return null/nil/None/... if the input is an even number or negative, as it is not possible to print a diamond of even or negative size.
Examples
A size 3 diamond:
*
***
*
...which would appear as a string of " *\n***\n *\n"
A size 5 diamond:
*
***
*****
***
*
...that is:
" *\n ***\n*****\n ***\n *\n"
*/
Assert.IsNull(print(0));
Assert.IsNull(print(-2));
Assert.IsNull(print(2));
var expected = new StringBuilder();
expected.Append("*\n");
Assert.AreEqual(expected.ToString(), print(1));
expected = new StringBuilder();
expected.Append(" *\n");
expected.Append("***\n");
expected.Append(" *\n");
Assert.AreEqual(expected.ToString(), print(3));
expected = new StringBuilder();
expected.Append(" *\n");
expected.Append(" ***\n");
expected.Append("*****\n");
expected.Append(" ***\n");
expected.Append(" *\n");
Assert.AreEqual(expected.ToString(), print(5));
}
public static string print(int n) {
if (n < 1 || n % 2 == 0) return null;
if (n == 1) return "*\n";
string lines = "";
int stars = 1;
for (int i = 0; i < n; i++) {
string gap = new(' ', n / 2 - stars / 2);
lines += gap + new string('*', stars) + "\n";
if (i < n / 2) stars += 2; else stars -= 2;
}
return lines;
}
}