Skip to content

Commit 596c45d

Browse files
committed
Merge pull request #56 from ErikSchierboom/new-exercises
Add 5 new exercises
2 parents 1258ace + c7d2cfa commit 596c45d

11 files changed

Lines changed: 724 additions & 1 deletion

File tree

config.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@
5151
"beer-song",
5252
"kindergarten-garden",
5353
"pascals-triangle",
54-
"saddle-points"
54+
"saddle-points",
55+
"nth-prime",
56+
"palindrome-products",
57+
"binary-search-tree",
58+
"robot-simulator",
59+
"simple-linked-list"
5560
],
5661
"deprecated": [
5762

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System.Linq;
2+
using NUnit.Framework;
3+
4+
public class BinarySearchTreeTest
5+
{
6+
[Test]
7+
public void Data_is_retained()
8+
{
9+
var tree = new BinarySearchTree(4);
10+
Assert.That(tree.Value, Is.EqualTo(4));
11+
}
12+
13+
[Ignore("Remove to run test")]
14+
[Test]
15+
public void Inserting_less()
16+
{
17+
var tree = new BinarySearchTree(4).Add(2);
18+
Assert.That(tree.Value, Is.EqualTo(4));
19+
Assert.That(tree.Left.Value, Is.EqualTo(2));
20+
}
21+
22+
[Ignore("Remove to run test")]
23+
[Test]
24+
public void Inserting_same()
25+
{
26+
var tree = new BinarySearchTree(4).Add(4);
27+
Assert.That(tree.Value, Is.EqualTo(4));
28+
Assert.That(tree.Left.Value, Is.EqualTo(4));
29+
}
30+
31+
[Ignore("Remove to run test")]
32+
[Test]
33+
public void Inserting_greater()
34+
{
35+
var tree = new BinarySearchTree(4).Add(5);
36+
Assert.That(tree.Value, Is.EqualTo(4));
37+
Assert.That(tree.Right.Value, Is.EqualTo(5));
38+
}
39+
40+
[Ignore("Remove to run test")]
41+
[Test]
42+
public void Complex_tree()
43+
{
44+
var tree = new BinarySearchTree(new [] { 4, 2, 6, 1, 3, 7, 5 });
45+
Assert.That(tree.Value, Is.EqualTo(4));
46+
Assert.That(tree.Left.Value, Is.EqualTo(2));
47+
Assert.That(tree.Left.Left.Value, Is.EqualTo(1));
48+
Assert.That(tree.Left.Right.Value, Is.EqualTo(3));
49+
Assert.That(tree.Right.Value, Is.EqualTo(6));
50+
Assert.That(tree.Right.Left.Value, Is.EqualTo(5));
51+
Assert.That(tree.Right.Right.Value, Is.EqualTo(7));
52+
}
53+
54+
[Ignore("Remove to run test")]
55+
[Test]
56+
public void Iterating_one_element()
57+
{
58+
var elements = new BinarySearchTree(4);
59+
Assert.That(elements, Is.EqualTo(new [] { 4 }));
60+
}
61+
62+
[Ignore("Remove to run test")]
63+
[Test]
64+
public void Iterating_over_smaller_element()
65+
{
66+
var elements = new BinarySearchTree(new[] { 4, 2 }).AsEnumerable();
67+
Assert.That(elements, Is.EqualTo(new[] { 2, 4 }));
68+
}
69+
70+
[Ignore("Remove to run test")]
71+
[Test]
72+
public void Iterating_over_larger_element()
73+
{
74+
var elements = new BinarySearchTree(new[] { 4, 5 }).AsEnumerable();
75+
Assert.That(elements, Is.EqualTo(new[] { 4, 5 }));
76+
}
77+
78+
[Ignore("Remove to run test")]
79+
[Test]
80+
public void Iterating_over_complex_element()
81+
{
82+
var elements = new BinarySearchTree(new[] { 4, 2, 1, 3, 6, 7, 5 }).AsEnumerable();
83+
Assert.That(elements, Is.EqualTo(new[] { 1, 2, 3, 4, 5, 6, 7 }));
84+
}
85+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
public class BinarySearchTree : IEnumerable<int>
7+
{
8+
public BinarySearchTree(int value)
9+
{
10+
Value = value;
11+
}
12+
13+
public BinarySearchTree(IEnumerable<int> values)
14+
{
15+
var array = values.ToArray();
16+
17+
if (array.Length == 0)
18+
{
19+
throw new ArgumentException("Cannot create tree from empty list");
20+
}
21+
22+
Value = array[0];
23+
24+
foreach (var value in array.Skip(1))
25+
{
26+
Add(value);
27+
}
28+
}
29+
30+
public int Value { get; }
31+
32+
public BinarySearchTree Left { get; private set; }
33+
34+
public BinarySearchTree Right { get; private set; }
35+
36+
public BinarySearchTree Add(int value)
37+
{
38+
if (value <= Value)
39+
{
40+
Left = Add(value, Left);
41+
}
42+
else
43+
{
44+
Right = Add(value, Right);
45+
}
46+
47+
return this;
48+
}
49+
50+
private static BinarySearchTree Add(int value, BinarySearchTree tree)
51+
{
52+
if (tree == null)
53+
{
54+
return new BinarySearchTree(value);
55+
}
56+
57+
return tree.Add(value);
58+
}
59+
60+
public IEnumerator<int> GetEnumerator()
61+
{
62+
foreach (var left in Left?.AsEnumerable() ?? Enumerable.Empty<int>())
63+
{
64+
yield return left;
65+
}
66+
67+
yield return Value;
68+
69+
foreach (var right in Right?.AsEnumerable() ?? Enumerable.Empty<int>())
70+
{
71+
yield return right;
72+
}
73+
}
74+
75+
IEnumerator IEnumerable.GetEnumerator()
76+
{
77+
return GetEnumerator();
78+
}
79+
}

exercises/nth-prime/Example.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
public static class NthPrime
6+
{
7+
public static int Nth(int nth)
8+
{
9+
return Primes().Skip(nth - 1).First();
10+
}
11+
12+
private static IEnumerable<int> Primes()
13+
{
14+
yield return 2;
15+
yield return 3;
16+
17+
foreach (var prime in PossiblePrimes().Where(IsPrime))
18+
{
19+
yield return prime;
20+
}
21+
}
22+
23+
private static IEnumerable<int> PossiblePrimes()
24+
{
25+
var n = 6;
26+
27+
while (true)
28+
{
29+
yield return n - 1;
30+
yield return n + 1;
31+
32+
n += 6;
33+
}
34+
}
35+
36+
private static bool IsPrime(int n)
37+
{
38+
var r = (int)Math.Floor(Math.Sqrt(n));
39+
40+
return r < 5 || Enumerable.Range(5, r - 4).All(x => n % x != 0);
41+
}
42+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using NUnit.Framework;
2+
3+
public class NthPrimeTest
4+
{
5+
[TestCase(1, ExpectedResult = 2)]
6+
[TestCase(2, ExpectedResult = 3, Ignore = "Remove to run test case")]
7+
[TestCase(3, ExpectedResult = 5, Ignore = "Remove to run test case")]
8+
[TestCase(4, ExpectedResult = 7, Ignore = "Remove to run test case")]
9+
[TestCase(5, ExpectedResult = 11, Ignore = "Remove to run test case")]
10+
[TestCase(6, ExpectedResult = 13, Ignore = "Remove to run test case")]
11+
[TestCase(7, ExpectedResult = 17, Ignore = "Remove to run test case")]
12+
[TestCase(8, ExpectedResult = 19, Ignore = "Remove to run test case")]
13+
[TestCase(1000, ExpectedResult = 7919, Ignore = "Remove to run test case")]
14+
[TestCase(10000, ExpectedResult = 104729, Ignore = "Remove to run test case")]
15+
[TestCase(10001, ExpectedResult = 104743, Ignore = "Remove to run test case")]
16+
public int Nth_prime_calculated(int nth)
17+
{
18+
return NthPrime.Nth(nth);
19+
}
20+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
public class Palindrome
6+
{
7+
private Palindrome(int value, ISet<Tuple<int, int>> factors)
8+
{
9+
Value = value;
10+
Factors = factors;
11+
}
12+
13+
public int Value { get; }
14+
15+
public ISet<Tuple<int, int>> Factors { get; }
16+
17+
public static Palindrome Largest(int maxFactor)
18+
{
19+
return Largest(1, maxFactor);
20+
}
21+
22+
public static Palindrome Largest(int minFactor, int maxFactor)
23+
{
24+
return FindPalindrome(minFactor, maxFactor, x => x.Max(p => p.Item1));
25+
}
26+
27+
public static Palindrome Smallest(int maxFactor)
28+
{
29+
return Smallest(1, maxFactor);
30+
}
31+
32+
public static Palindrome Smallest(int minFactor, int maxFactor)
33+
{
34+
return FindPalindrome(minFactor, maxFactor, x => x.Min(p => p.Item1));
35+
}
36+
37+
private static Palindrome FindPalindrome(int minFactor, int maxFactor, Func<List<Tuple<int, Tuple<int, int>>>, int> valueSelector)
38+
{
39+
var palindromes = FindAllPalindromes(minFactor, maxFactor);
40+
var value = valueSelector(palindromes);
41+
var factors = new HashSet<Tuple<int, int>>(palindromes.Where(p => p.Item1 == value).Select(p => p.Item2));
42+
43+
return new Palindrome(value, factors);
44+
}
45+
46+
private static List<Tuple<int, Tuple<int, int>>> FindAllPalindromes(int minFactor, int maxFactor)
47+
{
48+
return (from pair in Pairs(minFactor, maxFactor)
49+
let product = pair.Item1 * pair.Item2
50+
where IsPalindrome(product)
51+
select Tuple.Create(product, pair))
52+
.ToList();
53+
}
54+
55+
private static IEnumerable<Tuple<int, int>> Pairs(int minFactor, int maxFactor)
56+
{
57+
return from x in Enumerable.Range(minFactor, maxFactor + 1 - minFactor)
58+
from y in Enumerable.Range(x, maxFactor + 1 - x)
59+
select Tuple.Create(x, y);
60+
}
61+
62+
private static bool IsPalindrome(int num)
63+
{
64+
var n = num;
65+
var rev = 0;
66+
while (num > 0)
67+
{
68+
var dig = num % 10;
69+
rev = rev * 10 + dig;
70+
num = num / 10;
71+
}
72+
73+
return n == rev;
74+
}
75+
}

0 commit comments

Comments
 (0)