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 1 commit
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 ItterateRows(rows);
}

private static IEnumerable<IEnumerable<int>> ItterateRows(int rows)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ItterateRows should be IterateRows

for (var i = 1; i <= rows; i++)
{
yield return Row(i);
}
}

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

private static IEnumerable<int> Row(int row)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this function? The stub implementation only needs the public methods to allow it to compile.

{
throw new NotImplementedException();
}

private static IEnumerable<IEnumerable<int>> ItterateRows(int rows)
{
throw new NotImplementedException();
}
}
65 changes: 19 additions & 46 deletions exercises/pascals-triangle/PascalsTriangleTest.cs
Original file line number Diff line number Diff line change
@@ -1,80 +1,53 @@
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);
Assert.Equal(expected, PascalsTriangle.Calculate(4));
}

[Fact(Skip = "Remove to run test")]
public void Five_rows()
public void Negative_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.Throws<ArgumentOutOfRangeException>(() => PascalsTriangle.Calculate(-1));
}

[Fact(Skip = "Remove to run test")]
public void Twenty_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 void Null_no_rows()
{
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));
}
}
23 changes: 23 additions & 0 deletions generators/Exercises/PascalsTriangle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using Generators.Input;
using Generators.Output;
using Newtonsoft.Json.Linq;

namespace Generators.Exercises
{
public class PascalsTriangle : Exercise
{
protected override void UpdateCanonicalData(CanonicalData canonicalData)
{
foreach (var dataCases in canonicalData.Cases)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change dataCases to dataCase (most use canonicalDataCase). Since the variable is a single case.

dataCases.UseVariableForExpected = true;
dataCases.Property = "calculate";
if (!(dataCases.Expected is JArray))
dataCases.ExceptionThrown = typeof(ArgumentOutOfRangeException);
if (dataCases.Properties["count"] == null)
dataCases.Properties["count"] = -1;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be converting this to a -1? It should be entirely possible to pass in null (int?), or pass in nothing with a default param.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpreese Well, I think the canonical data is at fault here. In what way would passing null make sense for calculating pascal's triangle? I've created an issue to discuss removing this strange case. In the mean time, I'm in favor of manually removing this error case from the canonical data to make sure we don't output it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpreese @bmeverett I've created a PR to fix the canonical data.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'd agree. I know there was a PR over in the problem-specs repo about wanting to test edge cases (e.g. null). I'm not sure if this is a result of that or where that got left off.

}
}
}