Skip to content

Update exercises #492

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 4 commits into from
Nov 18, 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
14 changes: 9 additions & 5 deletions exercises/all-your-base/AllYourBaseTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file was auto-generated based on version 1.1.0 of the canonical data.
// This file was auto-generated based on version 2.0.0 of the canonical data.

using Xunit;
using System;
Expand Down Expand Up @@ -91,7 +91,8 @@ public void Empty_list()
var inputBase = 2;
var inputDigits = new int[0];
var outputBase = 10;
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, inputDigits, outputBase));
var expected = new[] { 0 };
Assert.Equal(expected, AllYourBase.Rebase(inputBase, inputDigits, outputBase));
}

[Fact(Skip = "Remove to run test")]
Expand All @@ -100,7 +101,8 @@ public void Single_zero()
var inputBase = 10;
var inputDigits = new[] { 0 };
var outputBase = 2;
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, inputDigits, outputBase));
var expected = new[] { 0 };
Assert.Equal(expected, AllYourBase.Rebase(inputBase, inputDigits, outputBase));
}

[Fact(Skip = "Remove to run test")]
Expand All @@ -109,7 +111,8 @@ public void Multiple_zeros()
var inputBase = 10;
var inputDigits = new[] { 0, 0, 0 };
var outputBase = 2;
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, inputDigits, outputBase));
var expected = new[] { 0 };
Assert.Equal(expected, AllYourBase.Rebase(inputBase, inputDigits, outputBase));
}

[Fact(Skip = "Remove to run test")]
Expand All @@ -118,7 +121,8 @@ public void Leading_zeros()
var inputBase = 7;
var inputDigits = new[] { 0, 6, 0 };
var outputBase = 10;
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, inputDigits, outputBase));
var expected = new[] { 4, 2 };
Assert.Equal(expected, AllYourBase.Rebase(inputBase, inputDigits, outputBase));
}

[Fact(Skip = "Remove to run test")]
Expand Down
10 changes: 7 additions & 3 deletions exercises/all-your-base/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ public static int[] Rebase(int inputBase, int[] inputDigits, int outputBase)
{
if (inputBase < 2) throw new ArgumentException("Invalid input base.");
if (outputBase < 2) throw new ArgumentException("Invalid output base.");
if (inputDigits.Length == 0) throw new ArgumentException("Empty input digits.");

return ToDigits(outputBase, FromDigits(inputBase, inputDigits));
var inputDigitsWithoutLeadingZeros = inputDigits.SkipWhile(digit => digit == 0).ToArray();

if (inputDigitsWithoutLeadingZeros.Length == 0)
return new[] { 0 };

return ToDigits(outputBase, FromDigits(inputBase, inputDigitsWithoutLeadingZeros));
}

private static int FromDigits(int fromBase, int[] fromDigits)
{
return fromDigits.Aggregate(0, (acc, x) =>
{
if (x < 0 || x >= fromBase || (x == 0 & acc == 0)) throw new ArgumentException("Invalid input digit");
if (x < 0 || x >= fromBase) throw new ArgumentException("Invalid input digit");

return acc*fromBase + x;
});
Expand Down
2 changes: 1 addition & 1 deletion exercises/nth-prime/NthPrimeTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file was auto-generated based on version 1.0.0 of the canonical data.
// This file was auto-generated based on version 2.0.0 of the canonical data.

using Xunit;
using System;
Expand Down
3 changes: 2 additions & 1 deletion generators/Exercises/AllYourBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Generators.Input;

namespace Generators.Exercises
Expand All @@ -9,7 +10,7 @@ protected override void UpdateCanonicalData(CanonicalData canonicalData)
{
foreach (var canonicalDataCase in canonicalData.Cases)
{
canonicalDataCase.ExceptionThrown = canonicalDataCase.Expected is null ? typeof(ArgumentException) : null;
canonicalDataCase.ExceptionThrown = canonicalDataCase.Expected is Dictionary<string, object> ? typeof(ArgumentException) : null;
canonicalDataCase.UseVariablesForInput = true;
canonicalDataCase.UseVariableForExpected = true;
}
Expand Down
3 changes: 2 additions & 1 deletion generators/Exercises/NthPrime.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Generators.Input;

namespace Generators.Exercises
Expand All @@ -8,7 +9,7 @@ public class NthPrime : Exercise
protected override void UpdateCanonicalData(CanonicalData canonicalData)
{
foreach (var canonicalDataCase in canonicalData.Cases)
canonicalDataCase.ExceptionThrown = canonicalDataCase.Expected is bool ? typeof(ArgumentOutOfRangeException) : null;
canonicalDataCase.ExceptionThrown = canonicalDataCase.Expected is Dictionary<string, object> ? typeof(ArgumentOutOfRangeException) : null;
}
}
}