Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
"luhn",
"pig-latin",
"pythagorean-triplet",
"series"
"series",
"difference-of-squares",
"secret-handshake"
],
"deprecated": [

Expand Down
82 changes: 82 additions & 0 deletions difference-of-squares/DifferenceOfSquaresTests.cs
Original file line number Diff line number Diff line change
@@ -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<ArgumentException>());
}
}
35 changes: 35 additions & 0 deletions difference-of-squares/Example.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
47 changes: 47 additions & 0 deletions secret-handshake/Example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;

public class SecretHandshake
{
private readonly int commandValue;
private readonly Dictionary<int, string> commandValues;

public SecretHandshake(int commandValue)
{
this.commandValue = commandValue;
commandValues = new Dictionary<int, string>
{
{ 1, "wink" },
{ 2, "double blink" },
{ 4, "close your eyes" },
{ 8, "jump" }
};
}

public string[] Commands()
{
List<string> commands = new List<string>();
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;
}
}
60 changes: 60 additions & 0 deletions secret-handshake/SecretHandshakeTests.cs
Original file line number Diff line number Diff line change
@@ -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" }));
}
}