Skip to content

Series generator #604

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 1 commit into from
Jul 16, 2018
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
34 changes: 7 additions & 27 deletions exercises/series/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,15 @@
using System.Collections.Generic;
using System.Linq;

public class Series
public static class Series
{
private readonly string numbers;

public Series(string numbers)
{
this.numbers = numbers;
}

public int[][] Slices(int sliceLength)
{
if (sliceLength > numbers.Length)
throw new ArgumentException("Slice length can't be longer than the given number");

var result = new List<int[]>();

for (int i = 0; SliceEndIndex(sliceLength, i) < numbers.Length; i++)
result.Add(SplitIntoInts(numbers.Substring(i, sliceLength)));

return result.ToArray();
}

private static int SliceEndIndex(int sliceLength, int idx)
public static string[] Slices(string numbers, int sliceLength)
{
return idx + sliceLength - 1;
}
if (numbers.Length == 0 || sliceLength <= 0 || sliceLength > numbers.Length)
throw new ArgumentException();

private static int[] SplitIntoInts(string value)
{
return value.Select(x => int.Parse(x.ToString())).ToArray();
return Enumerable.Range(0, numbers.Length - sliceLength + 1)
.Select(i => numbers.Substring(i, sliceLength))
.ToArray();
}
}
8 changes: 2 additions & 6 deletions exercises/series/Series.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using System;

public class Series
public static class Series
{
public Series(string numbers)
{
}

public int[][] Slices(int sliceLength)
public static string[] Slices(string numbers, int sliceLength)
{
throw new NotImplementedException("You need to implement this function.");
}
Expand Down
105 changes: 54 additions & 51 deletions exercises/series/SeriesTest.cs
Original file line number Diff line number Diff line change
@@ -1,80 +1,83 @@
// This file was auto-generated based on version 1.0.0 of the canonical data.

using System;
using Xunit;

public class SeriesTest
{
public static readonly TheoryData<string, int[][]> SliceOneTestData = new TheoryData<string, int[][]>
{
{ "01234", new[] { new[] { 0 }, new[] { 1 }, new[] { 2 }, new[] { 3 }, new[] { 4 } } },
{ "92834", new[] { new[] { 9 }, new[] { 2 }, new[] { 8 }, new[] { 3 }, new[] { 4 } } }
};

[Theory]
[MemberData(nameof(SliceOneTestData))]
public void Series_of_one_splits_to_one_digit(string input, int[][] result)
[Fact]
public void Slices_of_one_from_one()
{
Assert.Equal(result, new Series(input).Slices(1));
var expected = new[] { "1" };
Assert.Equal(expected, Series.Slices("1", 1));
}

public static readonly TheoryData<string, int[][]> SliceTwoTestData = new TheoryData<string, int[][]>
{
{ "01234", new[] { new[] { 0, 1 }, new[] { 1, 2 }, new[] { 2, 3 }, new[] { 3, 4 } } },
{ "98273463", new[] { new[] { 9, 8 }, new[] { 8, 2 }, new[] { 2, 7 }, new[] { 7, 3 }, new[] { 3, 4 }, new[] { 4, 6 }, new[] { 6, 3 } } },
{ "37103", new[] { new[] { 3, 7 }, new[] { 7, 1 }, new[] { 1, 0 }, new[] { 0, 3 } } }
};
[Fact(Skip = "Remove to run test")]
public void Slices_of_one_from_two()
{
var expected = new[] { "1", "2" };
Assert.Equal(expected, Series.Slices("12", 1));
}

[Theory(Skip = "Remove to run test")]
[MemberData(nameof(SliceTwoTestData))]
public void Series_of_two_splits_to_two_digits(string input, int[][] result)
[Fact(Skip = "Remove to run test")]
public void Slices_of_two()
{
Assert.Equal(result, new Series(input).Slices(2));
var expected = new[] { "35" };
Assert.Equal(expected, Series.Slices("35", 2));
}

public static readonly TheoryData<string, int[][]> SliceThreeTestData = new TheoryData<string, int[][]>
{
{ "01234", new[] { new[] { 0, 1, 2 }, new[] { 1, 2, 3 }, new[] { 2, 3, 4 } } },
{ "31001", new[] { new[] { 3, 1, 0 }, new[] { 1, 0, 0 }, new[] { 0, 0, 1 } } },
{ "982347", new[] { new[] { 9, 8, 2 }, new[] { 8, 2, 3 }, new[] { 2, 3, 4 }, new[] { 3, 4, 7 } } }
};
[Fact(Skip = "Remove to run test")]
public void Slices_of_two_overlap()
{
var expected = new[] { "91", "14", "42" };
Assert.Equal(expected, Series.Slices("9142", 2));
}

[Theory(Skip = "Remove to run test")]
[MemberData(nameof(SliceThreeTestData))]
public void Series_of_three_splits_to_three_digits(string input, int[][] result)
[Fact(Skip = "Remove to run test")]
public void Slices_can_include_duplicates()
{
Assert.Equal(result, new Series(input).Slices(3));
var expected = new[] { "777", "777", "777", "777" };
Assert.Equal(expected, Series.Slices("777777", 3));
}

public static readonly TheoryData<string, int[][]> SliceFourTestData = new TheoryData<string, int[][]>
[Fact(Skip = "Remove to run test")]
public void Slices_of_a_long_series()
{
var expected = new[]
{
{ "01234", new[] { new[] { 0, 1, 2, 3 }, new[] { 1, 2, 3, 4 } } },
{ "91274", new[] { new[] { 9, 1, 2, 7 }, new[] { 1, 2, 7, 4 } } }
"91849",
"18493",
"84939",
"49390",
"93904",
"39042",
"90424",
"04243"
};
Assert.Equal(expected, Series.Slices("918493904243", 5));
}

[Theory(Skip = "Remove to run test")]
[MemberData(nameof(SliceFourTestData))]
public void Series_of_four_splits_to_four_digits(string input, int[][] result)
[Fact(Skip = "Remove to run test")]
public void Slice_length_is_too_large()
{
Assert.Equal(result, new Series(input).Slices(4));
Assert.Throws<ArgumentException>(() => Series.Slices("12345", 6));
}

public static readonly TheoryData<string, int[][]> SliceFiveTestData = new TheoryData<string, int[][]>
{
{ "01234", new[] { new[] { 0, 1, 2, 3, 4 } } },
{ "81228", new[] { new[] { 8, 1, 2, 2, 8 } } }
};
[Fact(Skip = "Remove to run test")]
public void Slice_length_cannot_be_zero()
{
Assert.Throws<ArgumentException>(() => Series.Slices("12345", 0));
}

[Theory(Skip = "Remove to run test")]
[MemberData(nameof(SliceFiveTestData))]
public void Series_of_five_splits_to_five_digits(string input, int[][] result)
[Fact(Skip = "Remove to run test")]
public void Slice_length_cannot_be_negative()
{
Assert.Equal(result, new Series(input).Slices(5));
Assert.Throws<ArgumentException>(() => Series.Slices("123", -1));
}

[Theory(Skip = "Remove to run test")]
[InlineData("01234", 6)]
[InlineData("01032987583", 19)]
public void Slice_longer_than_input_is_not_allowed(string input, int slice)
[Fact(Skip = "Remove to run test")]
public void Empty_series_is_invalid()
{
Assert.Throws<ArgumentException>(() => new Series(input).Slices(slice));
Assert.Throws<ArgumentException>(() => Series.Slices("", 1));
}
}
16 changes: 16 additions & 0 deletions generators/Exercises/Generators/Series.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using Exercism.CSharp.Output;

namespace Exercism.CSharp.Exercises.Generators
{
public class Series : GeneratorExercise
{
protected override void UpdateTestMethod(TestMethod testMethod)
{
testMethod.UseVariableForExpected = true;

if (!(testMethod.Expected is string[]))
testMethod.ExceptionThrown = typeof(ArgumentException);
}
}
}