-
-
Notifications
You must be signed in to change notification settings - Fork 368
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,14 @@ public static IEnumerable<IEnumerable<int>> Calculate(int rows) | |
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
private static IEnumerable<int> Row(int row) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
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)); | ||
} | ||
} |
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) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jpreese @bmeverett I've created a PR to fix the canonical data. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ItterateRows should be IterateRows