Skip to content

add PascalsTriangle generator #459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 5, 2017
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
11 changes: 10 additions & 1 deletion exercises/pascals-triangle/Example.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
using System.Collections.Generic;
using System;

public static class PascalsTriangle
{
public static IEnumerable<IEnumerable<int>> Calculate(int rows)
{
if (rows < 0)
throw new ArgumentOutOfRangeException();

return IterateRows(rows);
}

private static IEnumerable<IEnumerable<int>> IterateRows(int rows)
{
for (var i = 1; i <= rows; i++)
{
yield return Row(i);
}
}

private static IEnumerable<int> Row(int row)
{
yield return 1;
Expand Down
1 change: 1 addition & 0 deletions exercises/pascals-triangle/PascalsTriangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public static IEnumerable<IEnumerable<int>> Calculate(int rows)
{
throw new NotImplementedException();
}

}
67 changes: 17 additions & 50 deletions exercises/pascals-triangle/PascalsTriangleTest.cs
Original file line number Diff line number Diff line change
@@ -1,80 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
// This file was auto-generated based on version 1.0.0 of the canonical data.

using Xunit;
using System;

public class PascalsTriangleTest
{
[Fact]
public void One_row()
public void Zero_rows()
{
Assert.Empty(PascalsTriangle.Calculate(0));
}

[Fact(Skip = "Remove to run test")]
public void Single_row()
{
var actual = PascalsTriangle.Calculate(1);
var expected = new[] { new[] { 1 } };
Assert.Equal(expected, actual, NestedEnumerableEqualityComparer<int>.Instance);
Assert.Equal(expected, PascalsTriangle.Calculate(1));
}

[Fact(Skip = "Remove to run test")]
public void Two_rows()
{
var actual = PascalsTriangle.Calculate(2).ToArray();
var expected = new[] { new[] { 1 }, new[] { 1, 1 } };
Assert.Equal(expected, actual, NestedEnumerableEqualityComparer<int>.Instance);
Assert.Equal(expected, PascalsTriangle.Calculate(2));
}

[Fact(Skip = "Remove to run test")]
public void Three_rows()
{
var actual = PascalsTriangle.Calculate(3);
var expected = new[] { new[] { 1 }, new[] { 1, 1 }, new[] { 1, 2, 1 } };
Assert.Equal(expected, actual, NestedEnumerableEqualityComparer<int>.Instance);
Assert.Equal(expected, PascalsTriangle.Calculate(3));
}

[Fact(Skip = "Remove to run test")]
public void Four_rows()
{
var actual = PascalsTriangle.Calculate(4);
var expected = new[] { new[] { 1 }, new[] { 1, 1 }, new[] { 1, 2, 1 }, new[] { 1, 3, 3, 1 } };
Assert.Equal(expected, actual, NestedEnumerableEqualityComparer<int>.Instance);
}

[Fact(Skip = "Remove to run test")]
public void Five_rows()
{
var actual = PascalsTriangle.Calculate(5);
var expected = new[] { new[] { 1 }, new[] { 1, 1 }, new[] { 1, 2, 1 }, new[] { 1, 3, 3, 1 }, new[] { 1, 4, 6, 4, 1 } };
Assert.Equal(expected, actual, NestedEnumerableEqualityComparer<int>.Instance);
Assert.Equal(expected, PascalsTriangle.Calculate(4));
}

[Fact(Skip = "Remove to run test")]
public void Twenty_rows()
public void Negative_rows()
{
var actual = PascalsTriangle.Calculate(20).Last();
var expected = new[] { 1, 19, 171, 969, 3876, 11628, 27132, 50388, 75582, 92378, 92378, 75582, 50388, 27132, 11628, 3876, 969, 171, 19, 1 };
Assert.Equal(expected, actual);
}

private class EnumerableEqualityComparer<T> : IEqualityComparer<IEnumerable<T>>
{
public static readonly EnumerableEqualityComparer<T> Instance = new EnumerableEqualityComparer<T>();

public bool Equals(IEnumerable<T> x, IEnumerable<T> y) => x.SequenceEqual(y);

public int GetHashCode(IEnumerable<T> obj)
{
throw new NotImplementedException();
}
}

private class NestedEnumerableEqualityComparer<T> : IEqualityComparer<IEnumerable<IEnumerable<T>>>
{
public static readonly NestedEnumerableEqualityComparer<T> Instance = new NestedEnumerableEqualityComparer<T>();

public bool Equals(IEnumerable<IEnumerable<T>> x, IEnumerable<IEnumerable<T>> y)
=> x.SequenceEqual(y, EnumerableEqualityComparer<T>.Instance);

public int GetHashCode(IEnumerable<IEnumerable<T>> obj)
{
throw new NotImplementedException();
}
Assert.Throws<ArgumentOutOfRangeException>(() => PascalsTriangle.Calculate(-1));
}
}
28 changes: 28 additions & 0 deletions generators/Exercises/PascalsTriangle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Linq;
using Generators.Input;
using Newtonsoft.Json.Linq;

namespace Generators.Exercises
{
public class PascalsTriangle : Exercise
{
protected override void UpdateCanonicalData(CanonicalData canonicalData)
{
//remove null case until canonical data is updated
var cases = canonicalData.Cases.ToList();
cases.RemoveAll(x => x.Properties["count"] == null);
canonicalData.Cases = cases.ToArray();

foreach (var canonicalDataCase in canonicalData.Cases)
{
canonicalDataCase.UseVariableForExpected = true;
canonicalDataCase.Property = "calculate";
if (!(canonicalDataCase.Expected is JArray))
canonicalDataCase.ExceptionThrown = typeof(ArgumentOutOfRangeException);
if (canonicalDataCase.Properties["count"] == null)
canonicalDataCase.Properties["count"] = -1;
}
}
}
}