Skip to content

Commit 9d45fe9

Browse files
committed
Make exercise APIs more C#-like
If a problem doesn't necessarily need to contain state, C# classes should utilize the static keyword to make the class a repository of static methods. This allows for easier LINQ-chaining and is generally better for concurrent programming. Updated tests and examples to match new APIs.
1 parent 2402dc5 commit 9d45fe9

20 files changed

Lines changed: 127 additions & 239 deletions

binary/BinaryTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class BinaryTest
1313
[TestCase("10001101000", Result = 1128, Ignore = true)]
1414
public int Binary_converts_to_decimal(string binary)
1515
{
16-
return new Binary(binary).ToDecimal();
16+
return Binary.ToDecimal(binary);
1717
}
1818

1919
[TestCase("carrot", Ignore = true)]
@@ -24,13 +24,13 @@ public int Binary_converts_to_decimal(string binary)
2424
[TestCase("abc10z", Ignore = true)]
2525
public void Invalid_binary_is_decimal_0(string invalidValue)
2626
{
27-
Assert.That(new Binary(invalidValue).ToDecimal(), Is.EqualTo(0));
27+
Assert.That(Binary.ToDecimal(invalidValue), Is.EqualTo(0));
2828
}
2929

3030
[Ignore]
3131
[Test]
3232
public void Binary_can_convert_formatted_strings()
3333
{
34-
Assert.That(new Binary("011").ToDecimal(), Is.EqualTo(3));
34+
Assert.That(Binary.ToDecimal("011"), Is.EqualTo(3));
3535
}
3636
}

binary/Example.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,18 @@
33

44
public class Binary
55
{
6-
private readonly string value;
7-
8-
public Binary(string value)
9-
{
10-
this.value = value;
11-
}
12-
13-
public int ToDecimal()
6+
public static int ToDecimal(string binary)
147
{
15-
if (IsNotValidBinary()) return 0;
8+
if (IsNotValidBinary(binary)) return 0;
169

17-
return value
18-
.Select((c, i) => int.Parse(c.ToString()) * TwoToThePowerOf(value.Length - i - 1))
10+
return binary
11+
.Select((c, i) => int.Parse(c.ToString()) * TwoToThePowerOf(binary.Length - i - 1))
1912
.Sum();
2013
}
2114

22-
private bool IsNotValidBinary()
15+
private static bool IsNotValidBinary(string binary)
2316
{
24-
return !value.All(x => char.IsDigit(x) && int.Parse(x.ToString()) < 2);
17+
return !binary.All(x => char.IsDigit(x) && int.Parse(x.ToString()) < 2);
2518
}
2619

2720
private static int TwoToThePowerOf(int power)

bob/BobTest.cs

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,136 +3,128 @@
33
[TestFixture]
44
public class BobTest
55
{
6-
private Bob teenager;
7-
8-
[SetUp]
9-
public void Init ()
10-
{
11-
teenager = new Bob();
12-
}
13-
146
[Test]
157
public void Stating_something ()
168
{
17-
Assert.That(teenager.Hey("Tom-ay-to, tom-aaaah-to."), Is.EqualTo("Whatever."));
9+
Assert.That(Bob.Hey("Tom-ay-to, tom-aaaah-to."), Is.EqualTo("Whatever."));
1810
}
1911

2012
[Ignore]
2113
[Test]
2214
public void Shouting ()
2315
{
24-
Assert.That(teenager.Hey("WATCH OUT!"), Is.EqualTo("Whoa, chill out!"));
16+
Assert.That(Bob.Hey("WATCH OUT!"), Is.EqualTo("Whoa, chill out!"));
2517
}
2618

2719
[Ignore]
2820
[Test]
2921
public void Asking_a_question ()
3022
{
31-
Assert.That(teenager.Hey("Does this cryogenic chamber make me look fat?"), Is.EqualTo("Sure."));
23+
Assert.That(Bob.Hey("Does this cryogenic chamber make me look fat?"), Is.EqualTo("Sure."));
3224
}
3325

3426
[Ignore]
3527
[Test]
3628
public void Asking_a_question_with_a_trailing_space()
3729
{
38-
Assert.That(teenager.Hey("Do I like my spacebar too much? "), Is.EqualTo("Sure."));
30+
Assert.That(Bob.Hey("Do I like my spacebar too much? "), Is.EqualTo("Sure."));
3931
}
4032

4133
[Ignore]
4234
[Test]
4335
public void Asking_a_numeric_question ()
4436
{
45-
Assert.That(teenager.Hey("You are, what, like 15?"), Is.EqualTo("Sure."));
37+
Assert.That(Bob.Hey("You are, what, like 15?"), Is.EqualTo("Sure."));
4638
}
4739

4840
[Ignore]
4941
[Test]
5042
public void Talking_forcefully ()
5143
{
52-
Assert.That(teenager.Hey("Let's go make out behind the gym!"), Is.EqualTo("Whatever."));
44+
Assert.That(Bob.Hey("Let's go make out behind the gym!"), Is.EqualTo("Whatever."));
5345
}
5446

5547
[Ignore]
5648
[Test]
5749
public void Using_acronyms_in_regular_search ()
5850
{
59-
Assert.That(teenager.Hey("It's OK if you don't want to go to the DMV."), Is.EqualTo("Whatever."));
51+
Assert.That(Bob.Hey("It's OK if you don't want to go to the DMV."), Is.EqualTo("Whatever."));
6052
}
6153

6254
[Ignore]
6355
[Test]
6456
public void Forceful_questions ()
6557
{
66-
Assert.That(teenager.Hey("WHAT THE HELL WERE YOU THINKING?"), Is.EqualTo("Whoa, chill out!"));
58+
Assert.That(Bob.Hey("WHAT THE HELL WERE YOU THINKING?"), Is.EqualTo("Whoa, chill out!"));
6759
}
6860

6961
[Ignore]
7062
[Test]
7163
public void Shouting_numbers ()
7264
{
73-
Assert.That(teenager.Hey("1, 2, 3 GO!"), Is.EqualTo("Whoa, chill out!"));
65+
Assert.That(Bob.Hey("1, 2, 3 GO!"), Is.EqualTo("Whoa, chill out!"));
7466
}
7567

7668
[Ignore]
7769
[Test]
7870
public void Only_numbers ()
7971
{
80-
Assert.That(teenager.Hey("1, 2, 3"), Is.EqualTo("Whatever."));
72+
Assert.That(Bob.Hey("1, 2, 3"), Is.EqualTo("Whatever."));
8173
}
8274

8375
[Ignore]
8476
[Test]
8577
public void Question_with_only_numbers ()
8678
{
87-
Assert.That(teenager.Hey("4?"), Is.EqualTo("Sure."));
79+
Assert.That(Bob.Hey("4?"), Is.EqualTo("Sure."));
8880
}
8981

9082
[Ignore]
9183
[Test]
9284
public void Shouting_with_special_characters ()
9385
{
94-
Assert.That(teenager.Hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"), Is.EqualTo("Whoa, chill out!"));
86+
Assert.That(Bob.Hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"), Is.EqualTo("Whoa, chill out!"));
9587
}
9688

9789
[Ignore]
9890
[Test]
9991
public void Shouting_with_no_exclamation_mark ()
10092
{
101-
Assert.That(teenager.Hey("I HATE YOU"), Is.EqualTo("Whoa, chill out!"));
93+
Assert.That(Bob.Hey("I HATE YOU"), Is.EqualTo("Whoa, chill out!"));
10294
}
10395

10496
[Ignore]
10597
[Test]
10698
public void Statement_containing_question_mark ()
10799
{
108-
Assert.That(teenager.Hey("Ending with ? means a question."), Is.EqualTo("Whatever."));
100+
Assert.That(Bob.Hey("Ending with ? means a question."), Is.EqualTo("Whatever."));
109101
}
110102

111103
[Ignore]
112104
[Test]
113105
public void Prattling_on ()
114106
{
115-
Assert.That(teenager.Hey("Wait! Hang on. Are you going to be OK?"), Is.EqualTo("Sure."));
107+
Assert.That(Bob.Hey("Wait! Hang on. Are you going to be OK?"), Is.EqualTo("Sure."));
116108
}
117109

118110
[Ignore]
119111
[Test]
120112
public void Silence ()
121113
{
122-
Assert.That(teenager.Hey(""), Is.EqualTo("Fine. Be that way!"));
114+
Assert.That(Bob.Hey(""), Is.EqualTo("Fine. Be that way!"));
123115
}
124116

125117
[Ignore]
126118
[Test]
127119
public void Prolonged_silence ()
128120
{
129-
Assert.That(teenager.Hey(" "), Is.EqualTo("Fine. Be that way!"));
121+
Assert.That(Bob.Hey(" "), Is.EqualTo("Fine. Be that way!"));
130122
}
131123

132124
[Ignore]
133125
[Test]
134126
public void Multiple_line_question ()
135127
{
136-
Assert.That(teenager.Hey("Does this cryogenic chamber make me look fat?\nno"), Is.EqualTo("Whatever."));
128+
Assert.That(Bob.Hey("Does this cryogenic chamber make me look fat?\nno"), Is.EqualTo("Whatever."));
137129
}
138130
}

bob/Example.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,27 @@
11
public class Bob
22
{
3-
public string Hey(string statement)
3+
public static string Hey(string statement)
44
{
5-
65
if (IsSilence(statement))
7-
{
86
return "Fine. Be that way!";
9-
}
107
if (IsYelling(statement))
11-
{
128
return "Whoa, chill out!";
13-
}
149
if (IsQuestion(statement))
15-
{
1610
return "Sure.";
17-
}
18-
else
19-
{
20-
return "Whatever.";
21-
}
22-
11+
return "Whatever.";
2312
}
2413

25-
private bool IsSilence (string statement)
14+
private static bool IsSilence (string statement)
2615
{
2716
return statement.Trim() == "";
2817
}
2918

30-
private bool IsYelling (string statement)
19+
private static bool IsYelling (string statement)
3120
{
3221
return statement.ToUpper() == statement && System.Text.RegularExpressions.Regex.IsMatch(statement, "[a-zA-Z]+");
3322
}
3423

35-
private bool IsQuestion (string statement)
24+
private static bool IsQuestion (string statement)
3625
{
3726
return statement.Trim().EndsWith("?");
3827
}

gigasecond/Example.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@
22

33
public class Gigasecond
44
{
5-
private readonly DateTime birthDate;
6-
7-
public Gigasecond(DateTime birthDate)
8-
{
9-
this.birthDate = birthDate;
10-
}
11-
12-
public DateTime Date()
5+
public static DateTime Date(DateTime birthDate)
136
{
147
return birthDate.AddSeconds(1000000000);
158
}

gigasecond/GigasecondTest.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ public class GigasecondTest
77
[Test]
88
public void First_date()
99
{
10-
var gs = new Gigasecond(new DateTime(2011, 4, 25, 0, 0, 0, DateTimeKind.Utc));
11-
Assert.That(gs.Date(), Is.EqualTo(new DateTime(2043, 1, 1, 1, 46, 40, DateTimeKind.Utc)));
10+
var date = Gigasecond.Date(new DateTime(2011, 4, 25, 0, 0, 0, DateTimeKind.Utc));
11+
Assert.That(date, Is.EqualTo(new DateTime(2043, 1, 1, 1, 46, 40, DateTimeKind.Utc)));
1212
}
1313

1414
[Test]
1515
[Ignore]
1616
public void Another_date()
1717
{
18-
var gs = new Gigasecond(new DateTime(1977, 6, 13, 0, 0, 0, DateTimeKind.Utc));
19-
Assert.That(gs.Date(), Is.EqualTo(new DateTime(2009, 2, 19, 1, 46, 40, DateTimeKind.Utc)));
18+
var date = Gigasecond.Date(new DateTime(1977, 6, 13, 0, 0, 0, DateTimeKind.Utc));
19+
Assert.That(date, Is.EqualTo(new DateTime(2009, 2, 19, 1, 46, 40, DateTimeKind.Utc)));
2020
}
2121

2222
[Test]
2323
[Ignore]
2424
public void Yet_another_date()
2525
{
26-
var gs = new Gigasecond(new DateTime(1959, 7, 19, 0, 0, 0, DateTimeKind.Utc));
27-
Assert.That(gs.Date(), Is.EqualTo(new DateTime(1991, 3, 27, 1, 46, 40, DateTimeKind.Utc)));
26+
var date = Gigasecond.Date(new DateTime(1959, 7, 19, 0, 0, 0, DateTimeKind.Utc));
27+
Assert.That(date, Is.EqualTo(new DateTime(1991, 3, 27, 1, 46, 40, DateTimeKind.Utc)));
2828
}
2929
}

octal/Example.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,18 @@
33

44
public class Octal
55
{
6-
private readonly string value;
7-
8-
public Octal(string value)
9-
{
10-
this.value = value;
11-
}
12-
13-
public int ToDecimal()
6+
public static int ToDecimal(string octal)
147
{
15-
if (IsNotValidOctal()) return 0;
8+
if (IsNotValidOctal(octal)) return 0;
169

17-
return value
18-
.Select((c, i) => int.Parse(c.ToString()) * EightToThePowerOf(value.Length - i - 1))
10+
return octal
11+
.Select((c, i) => int.Parse(c.ToString()) * EightToThePowerOf(octal.Length - i - 1))
1912
.Sum();
2013
}
2114

22-
private bool IsNotValidOctal()
15+
private static bool IsNotValidOctal(string octal)
2316
{
24-
return !value.All(x => char.IsDigit(x) && int.Parse(x.ToString()) < 8);
17+
return !octal.All(x => char.IsDigit(x) && int.Parse(x.ToString()) < 8);
2518
}
2619

2720
private static int EightToThePowerOf(int power)

octal/OctalTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class OctalTest
1414
[TestCase("1234567", Result = 342391, Ignore = true)]
1515
public int Octal_converts_to_decimal(string value)
1616
{
17-
return new Octal(value).ToDecimal();
17+
return Octal.ToDecimal(value);
1818
}
1919

2020
[TestCase("carrot", Ignore = true)]
@@ -24,13 +24,13 @@ public int Octal_converts_to_decimal(string value)
2424
[TestCase("abc1z", Ignore = true)]
2525
public void Invalid_octal_is_decimal_0(string invalidValue)
2626
{
27-
Assert.That(new Octal(invalidValue).ToDecimal(), Is.EqualTo(0));
27+
Assert.That(Octal.ToDecimal(invalidValue), Is.EqualTo(0));
2828
}
2929

3030
[Ignore]
3131
[Test]
3232
public void Octal_can_convert_formatted_strings()
3333
{
34-
Assert.That(new Octal("011").ToDecimal(), Is.EqualTo(9));
34+
Assert.That(Octal.ToDecimal("011"), Is.EqualTo(9));
3535
}
3636
}

scrabble-score/Example.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
public class Scrabble
55
{
6-
private readonly string word;
7-
86
private static readonly Dictionary<char, int> LetterScores = new Dictionary<char, int>
97
{
108
{ 'a', 1 }, { 'e', 1 }, { 'i', 1 }, { 'o', 1 }, { 'u', 1 }, { 'l', 1 }, { 'n', 1 }, { 'r', 1 }, { 's', 1 }, { 't', 1 },
@@ -16,21 +14,11 @@ public class Scrabble
1614
{ 'q', 10 }, { 'z', 10 }
1715
};
1816

19-
public Scrabble(string word)
20-
{
21-
this.word = word;
22-
}
23-
2417
public static int Score(string input)
2518
{
2619
if (string.IsNullOrWhiteSpace(input))
2720
return 0;
2821

2922
return input.ToLower().Sum(x => LetterScores[x]);
3023
}
31-
32-
public int Score()
33-
{
34-
return Score(word);
35-
}
3624
}

0 commit comments

Comments
 (0)