Skip to content

Commit ccb207c

Browse files
bmeverettErikSchierboom
authored andcommitted
add PascalsTriangle generator (#459)
* add PascalsTriangle generator and modified example and code file to adhear to generation * remove null case for now and minor fixes * remove unnecessary methods
1 parent 4beeb96 commit ccb207c

File tree

4 files changed

+56
-51
lines changed

4 files changed

+56
-51
lines changed

exercises/pascals-triangle/Example.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
using System.Collections.Generic;
2+
using System;
23

34
public static class PascalsTriangle
45
{
56
public static IEnumerable<IEnumerable<int>> Calculate(int rows)
7+
{
8+
if (rows < 0)
9+
throw new ArgumentOutOfRangeException();
10+
11+
return IterateRows(rows);
12+
}
13+
14+
private static IEnumerable<IEnumerable<int>> IterateRows(int rows)
615
{
716
for (var i = 1; i <= rows; i++)
817
{
918
yield return Row(i);
1019
}
1120
}
12-
21+
1322
private static IEnumerable<int> Row(int row)
1423
{
1524
yield return 1;

exercises/pascals-triangle/PascalsTriangle.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public static IEnumerable<IEnumerable<int>> Calculate(int rows)
77
{
88
throw new NotImplementedException();
99
}
10+
1011
}
Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,47 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
1+
// This file was auto-generated based on version 1.0.0 of the canonical data.
2+
43
using Xunit;
4+
using System;
55

66
public class PascalsTriangleTest
77
{
88
[Fact]
9-
public void One_row()
9+
public void Zero_rows()
10+
{
11+
Assert.Empty(PascalsTriangle.Calculate(0));
12+
}
13+
14+
[Fact(Skip = "Remove to run test")]
15+
public void Single_row()
1016
{
11-
var actual = PascalsTriangle.Calculate(1);
1217
var expected = new[] { new[] { 1 } };
13-
Assert.Equal(expected, actual, NestedEnumerableEqualityComparer<int>.Instance);
18+
Assert.Equal(expected, PascalsTriangle.Calculate(1));
1419
}
15-
20+
1621
[Fact(Skip = "Remove to run test")]
1722
public void Two_rows()
1823
{
19-
var actual = PascalsTriangle.Calculate(2).ToArray();
2024
var expected = new[] { new[] { 1 }, new[] { 1, 1 } };
21-
Assert.Equal(expected, actual, NestedEnumerableEqualityComparer<int>.Instance);
25+
Assert.Equal(expected, PascalsTriangle.Calculate(2));
2226
}
2327

2428
[Fact(Skip = "Remove to run test")]
2529
public void Three_rows()
2630
{
27-
var actual = PascalsTriangle.Calculate(3);
2831
var expected = new[] { new[] { 1 }, new[] { 1, 1 }, new[] { 1, 2, 1 } };
29-
Assert.Equal(expected, actual, NestedEnumerableEqualityComparer<int>.Instance);
32+
Assert.Equal(expected, PascalsTriangle.Calculate(3));
3033
}
3134

3235
[Fact(Skip = "Remove to run test")]
3336
public void Four_rows()
3437
{
35-
var actual = PascalsTriangle.Calculate(4);
3638
var expected = new[] { new[] { 1 }, new[] { 1, 1 }, new[] { 1, 2, 1 }, new[] { 1, 3, 3, 1 } };
37-
Assert.Equal(expected, actual, NestedEnumerableEqualityComparer<int>.Instance);
38-
}
39-
40-
[Fact(Skip = "Remove to run test")]
41-
public void Five_rows()
42-
{
43-
var actual = PascalsTriangle.Calculate(5);
44-
var expected = new[] { new[] { 1 }, new[] { 1, 1 }, new[] { 1, 2, 1 }, new[] { 1, 3, 3, 1 }, new[] { 1, 4, 6, 4, 1 } };
45-
Assert.Equal(expected, actual, NestedEnumerableEqualityComparer<int>.Instance);
39+
Assert.Equal(expected, PascalsTriangle.Calculate(4));
4640
}
4741

4842
[Fact(Skip = "Remove to run test")]
49-
public void Twenty_rows()
43+
public void Negative_rows()
5044
{
51-
var actual = PascalsTriangle.Calculate(20).Last();
52-
var expected = new[] { 1, 19, 171, 969, 3876, 11628, 27132, 50388, 75582, 92378, 92378, 75582, 50388, 27132, 11628, 3876, 969, 171, 19, 1 };
53-
Assert.Equal(expected, actual);
54-
}
55-
56-
private class EnumerableEqualityComparer<T> : IEqualityComparer<IEnumerable<T>>
57-
{
58-
public static readonly EnumerableEqualityComparer<T> Instance = new EnumerableEqualityComparer<T>();
59-
60-
public bool Equals(IEnumerable<T> x, IEnumerable<T> y) => x.SequenceEqual(y);
61-
62-
public int GetHashCode(IEnumerable<T> obj)
63-
{
64-
throw new NotImplementedException();
65-
}
66-
}
67-
68-
private class NestedEnumerableEqualityComparer<T> : IEqualityComparer<IEnumerable<IEnumerable<T>>>
69-
{
70-
public static readonly NestedEnumerableEqualityComparer<T> Instance = new NestedEnumerableEqualityComparer<T>();
71-
72-
public bool Equals(IEnumerable<IEnumerable<T>> x, IEnumerable<IEnumerable<T>> y)
73-
=> x.SequenceEqual(y, EnumerableEqualityComparer<T>.Instance);
74-
75-
public int GetHashCode(IEnumerable<IEnumerable<T>> obj)
76-
{
77-
throw new NotImplementedException();
78-
}
45+
Assert.Throws<ArgumentOutOfRangeException>(() => PascalsTriangle.Calculate(-1));
7946
}
8047
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Linq;
3+
using Generators.Input;
4+
using Newtonsoft.Json.Linq;
5+
6+
namespace Generators.Exercises
7+
{
8+
public class PascalsTriangle : Exercise
9+
{
10+
protected override void UpdateCanonicalData(CanonicalData canonicalData)
11+
{
12+
//remove null case until canonical data is updated
13+
var cases = canonicalData.Cases.ToList();
14+
cases.RemoveAll(x => x.Properties["count"] == null);
15+
canonicalData.Cases = cases.ToArray();
16+
17+
foreach (var canonicalDataCase in canonicalData.Cases)
18+
{
19+
canonicalDataCase.UseVariableForExpected = true;
20+
canonicalDataCase.Property = "calculate";
21+
if (!(canonicalDataCase.Expected is JArray))
22+
canonicalDataCase.ExceptionThrown = typeof(ArgumentOutOfRangeException);
23+
if (canonicalDataCase.Properties["count"] == null)
24+
canonicalDataCase.Properties["count"] = -1;
25+
}
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)