diff --git a/exercises/series/Example.cs b/exercises/series/Example.cs index d46bae8cb9..ab716c99d2 100644 --- a/exercises/series/Example.cs +++ b/exercises/series/Example.cs @@ -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(); - - 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(); } } \ No newline at end of file diff --git a/exercises/series/Series.cs b/exercises/series/Series.cs index 5eb412662b..b41d3ff657 100644 --- a/exercises/series/Series.cs +++ b/exercises/series/Series.cs @@ -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."); } diff --git a/exercises/series/SeriesTest.cs b/exercises/series/SeriesTest.cs index f72dd00198..f901cf0c91 100644 --- a/exercises/series/SeriesTest.cs +++ b/exercises/series/SeriesTest.cs @@ -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 SliceOneTestData = new TheoryData - { - { "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 SliceTwoTestData = new TheoryData - { - { "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 SliceThreeTestData = new TheoryData - { - { "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 SliceFourTestData = new TheoryData + [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(() => Series.Slices("12345", 6)); } - public static readonly TheoryData SliceFiveTestData = new TheoryData - { - { "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(() => 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(() => 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(() => new Series(input).Slices(slice)); + Assert.Throws(() => Series.Slices("", 1)); } } \ No newline at end of file diff --git a/generators/Exercises/Generators/Series.cs b/generators/Exercises/Generators/Series.cs new file mode 100644 index 0000000000..9db3af8e18 --- /dev/null +++ b/generators/Exercises/Generators/Series.cs @@ -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); + } + } +} \ No newline at end of file