From dfccca343ffa14a7b04a9219fe0068f932e52913 Mon Sep 17 00:00:00 2001 From: Mike Cooper Date: Wed, 8 Oct 2014 12:20:55 -0400 Subject: [PATCH 1/2] Added two exercises Added the difference-of-squares and secret-handshake exercises --- .../DifferenceOfSquaresTests.cs | 82 +++++++++++++++++++ difference-of-squares/Example.cs | 35 ++++++++ secret-handshake/Example.cs | 47 +++++++++++ secret-handshake/SecretHandshakeTests.cs | 60 ++++++++++++++ 4 files changed, 224 insertions(+) create mode 100644 difference-of-squares/DifferenceOfSquaresTests.cs create mode 100644 difference-of-squares/Example.cs create mode 100644 secret-handshake/Example.cs create mode 100644 secret-handshake/SecretHandshakeTests.cs diff --git a/difference-of-squares/DifferenceOfSquaresTests.cs b/difference-of-squares/DifferenceOfSquaresTests.cs new file mode 100644 index 0000000000..4977a617d4 --- /dev/null +++ b/difference-of-squares/DifferenceOfSquaresTests.cs @@ -0,0 +1,82 @@ +using System; +using NUnit.Framework; + +[TestFixture] +public class DifferenceOfSquaresTests +{ + [Test] + public void Test_square_of_sums_to_5() + { + Assert.That(new Squares(5).SquareOfSums(), Is.EqualTo(225)); + } + + [Ignore] + [Test] + public void Test_sum_of_squares_to_5() + { + Assert.That(new Squares(5).SumOfSquares(), Is.EqualTo(55)); + } + + [Ignore] + [Test] + public void Test_difference_of_sums_to_5() + { + Assert.That(new Squares(5).DifferenceOfSquares(), Is.EqualTo(170)); + } + + [Ignore] + [Test] + public void Test_square_of_sums_to_10() + { + Assert.That(new Squares(10).SquareOfSums(), Is.EqualTo(3025)); + } + + [Ignore] + [Test] + public void Test_sum_of_squares_to_10() + { + Assert.That(new Squares(10).SumOfSquares(), Is.EqualTo(385)); + } + + [Ignore] + [Test] + public void Test_difference_of_sums_to_10() + { + Assert.That(new Squares(10).DifferenceOfSquares(), Is.EqualTo(2640)); + } + + [Ignore] + [Test] + public void Test_square_of_sums_to_100() + { + Assert.That(new Squares(100).SquareOfSums(), Is.EqualTo(25502500)); + } + + [Ignore] + [Test] + public void Test_sum_of_squares_to_100() + { + Assert.That(new Squares(100).SumOfSquares(), Is.EqualTo(338350)); + } + + [Ignore] + [Test] + public void Test_difference_of_sums_to_100() + { + Assert.That(new Squares(100).DifferenceOfSquares(), Is.EqualTo(25164150)); + } + + [Ignore] + [Test] + public void Test_difference_of_sums_0() + { + Assert.That(new Squares(0).DifferenceOfSquares(), Is.EqualTo(0)); + } + + [Ignore] + [Test] + public void Test_negative_numbers_throw_argument_exception() + { + Assert.That(() => new Squares(-5), Throws.TypeOf()); + } +} diff --git a/difference-of-squares/Example.cs b/difference-of-squares/Example.cs new file mode 100644 index 0000000000..feced8487d --- /dev/null +++ b/difference-of-squares/Example.cs @@ -0,0 +1,35 @@ +using System; +using System.Linq; + +public class Squares +{ + private readonly int max; + + public Squares(int max) + { + if (max < 0) + { + throw new ArgumentException("Max must be positive", "max"); + } + + this.max = max; + } + + public int SquareOfSums() + { + var numbers = Enumerable.Range(1, max); + int sum = numbers.Sum(); + return sum * sum; + } + + public int SumOfSquares() + { + var numbers = Enumerable.Range(1, max); + return numbers.Select(x => x * x).Sum(); + } + + public int DifferenceOfSquares() + { + return SquareOfSums() - SumOfSquares(); + } +} \ No newline at end of file diff --git a/secret-handshake/Example.cs b/secret-handshake/Example.cs new file mode 100644 index 0000000000..9f95dd3a70 --- /dev/null +++ b/secret-handshake/Example.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +public class SecretHandshake +{ + private readonly int commandValue; + private readonly Dictionary commandValues; + + public SecretHandshake(int commandValue) + { + this.commandValue = commandValue; + commandValues = new Dictionary + { + { 1, "wink" }, + { 2, "double blink" }, + { 4, "close your eyes" }, + { 8, "jump" } + }; + } + + public string[] Commands() + { + List commands = new List(); + foreach (var value in commandValues.OrderBy(x => x.Key)) + { + if ((commandValue & value.Key) != 0) + { + commands.Add(value.Value); + } + } + + if (ShouldReverse()) + { + return commands.AsEnumerable().Reverse().ToArray(); + } + else + { + return commands.ToArray(); + } + } + + private bool ShouldReverse() + { + return (commandValue & 16) != 0; + } +} diff --git a/secret-handshake/SecretHandshakeTests.cs b/secret-handshake/SecretHandshakeTests.cs new file mode 100644 index 0000000000..5a84820750 --- /dev/null +++ b/secret-handshake/SecretHandshakeTests.cs @@ -0,0 +1,60 @@ +using NUnit.Framework; + +[TestFixture] +public class SecretHandshakeTests +{ + [Test] + public void Test_1_handshake_to_wink() + { + var handshake = new SecretHandshake(1); + Assert.That(handshake.Commands(), Is.EqualTo(new string[] { "wink" })); + } + + [Ignore] + [Test] + public void Test_10_handshake_to_double_blink() + { + var handshake = new SecretHandshake(2); + Assert.That(handshake.Commands(), Is.EqualTo(new string[] { "double blink" })); + } + + [Ignore] + [Test] + public void Test_100_handshake_to_close_your_eyes() + { + var handshake = new SecretHandshake(4); + Assert.That(handshake.Commands(), Is.EqualTo(new string[] { "close your eyes" })); + } + + [Ignore] + [Test] + public void Test_1000_handshake_to_close_your_eyes() + { + var handshake = new SecretHandshake(8); + Assert.That(handshake.Commands(), Is.EqualTo(new string[] { "jump" })); + } + + [Ignore] + [Test] + public void Test_handshake_11_to_wink_and_double_blink() + { + var handshake = new SecretHandshake(3); + Assert.That(handshake.Commands(), Is.EqualTo(new string[] { "wink", "double blink" })); + } + + [Ignore] + [Test] + public void Test_handshake_10011_to_double_blink_and_wink() + { + var handshake = new SecretHandshake(19); + Assert.That(handshake.Commands(), Is.EqualTo(new string[] { "double blink", "wink" })); + } + + [Ignore] + [Test] + public void Test_handshake_11111_to_all_commands_reversed() + { + var handshake = new SecretHandshake(31); + Assert.That(handshake.Commands(), Is.EqualTo(new string[] { "jump", "close your eyes", "double blink", "wink" })); + } +} From 29c41ea59dc1d21675b49c590ca8adc76f6aab22 Mon Sep 17 00:00:00 2001 From: mikecoop Date: Thu, 9 Oct 2014 00:57:13 -0400 Subject: [PATCH 2/2] Added missing exercises to config file --- config.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config.json b/config.json index a8c9cb4b90..d2076b4a5e 100644 --- a/config.json +++ b/config.json @@ -38,7 +38,9 @@ "luhn", "pig-latin", "pythagorean-triplet", - "series" + "series", + "difference-of-squares", + "secret-handshake" ], "deprecated": [