Skip to content

Commit e609512

Browse files
Merge pull request #90 from ErikSchierboom/largest-series-product
Update largest-series-product exercise. Fixes #64
2 parents b6c05c0 + d54983f commit e609512

2 files changed

Lines changed: 176 additions & 48 deletions

File tree

exercises/largest-series-product/Example.cs

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,33 @@
22
using System.Collections.Generic;
33
using System.Linq;
44

5-
public class LargestSeriesProduct
5+
public static class LargestSeriesProduct
66
{
7-
private readonly string digits;
7+
public static long GetLargestProduct(string digits, int span) => GetSlices(ParseDigits(digits), span).Max(l => GetProduct(l));
88

9-
private int[] Digits;
10-
11-
public LargestSeriesProduct(string digits)
9+
private static IEnumerable<IEnumerable<long>> GetSlices(long[] digits, int span)
1210
{
13-
this.digits = digits;
14-
Digits = ParseValues(digits);
15-
}
11+
if (span < 0 || span > digits.Length)
12+
{
13+
throw new ArgumentException("Invalid span.");
14+
}
1615

17-
private int[] ParseValues(IEnumerable<char> values)
18-
{
19-
return values.Select(x => int.Parse(x.ToString())).ToArray();
16+
return Enumerable.Range(0, GetNumberOfSlices(digits, span)).Select(i => digits.Skip(i).Take(span));
2017
}
2118

22-
private int[][] GetSlices(int limit)
19+
private static long[] ParseDigits(string digits) => digits.ToCharArray().Select(ParseDigit).ToArray();
20+
21+
private static long ParseDigit(char c)
2322
{
24-
if (limit > digits.Length) throw new ArgumentException("Slice size is too big");
25-
var slices = new List<int[]>();
26-
for (int i = 0; i <= digits.Length - limit; i++)
23+
if (!char.IsDigit(c))
2724
{
28-
slices.Add(ParseValues(digits.Skip(i).Take(limit)));
25+
throw new ArgumentException("Invalid digit.");
2926
}
30-
return slices.ToArray();
31-
}
3227

33-
public int GetLargestProduct(int seriesLength)
34-
{
35-
if (seriesLength < 1) return 1;
36-
return GetSlices(seriesLength).Aggregate(0, (prev, next) =>
37-
{
38-
int product = next.Aggregate((x, y) => x * y);
39-
return product > prev ? product : prev;
40-
});
28+
return long.Parse(c.ToString());
4129
}
30+
31+
private static long GetProduct(IEnumerable<long> numbers) => numbers.Aggregate(1L, (x, product) => x * product);
32+
33+
private static int GetNumberOfSlices(long[] digits, int span) => digits.Length + 1 - span;
4234
}

exercises/largest-series-product/LargestSeriesProductTest.cs

Lines changed: 158 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,185 @@
44
[TestFixture]
55
public class LargestSeriesProductTest
66
{
7-
[TestCase("01234567890", 2, ExpectedResult = 72)]
8-
[TestCase("1027839564", 3, ExpectedResult = 270)]
9-
public int Gets_the_largest_product(string digits, int seriesLength)
7+
[Test]
8+
public void Can_find_the_largest_product_of_2_with_numbers_in_order()
9+
{
10+
const string digits = "0123456789";
11+
const int span = 2;
12+
const int expected = 72;
13+
14+
Assert.That(LargestSeriesProduct.GetLargestProduct(digits, span), Is.EqualTo(expected));
15+
}
16+
17+
[Ignore("Remove to run test")]
18+
[Test]
19+
public void Can_find_the_largest_product_of_2()
20+
{
21+
const string digits = "576802143";
22+
const int span = 2;
23+
const int expected = 48;
24+
25+
Assert.That(LargestSeriesProduct.GetLargestProduct(digits, span), Is.EqualTo(expected));
26+
}
27+
28+
[Ignore("Remove to run test")]
29+
[Test]
30+
public void Finds_the_largest_product_if_span_equals_length()
31+
{
32+
const string digits = "29";
33+
const int span = 2;
34+
const int expected = 18;
35+
36+
Assert.That(LargestSeriesProduct.GetLargestProduct(digits, span), Is.EqualTo(expected));
37+
}
38+
39+
[Ignore("Remove to run test")]
40+
[Test]
41+
public void Can_find_the_largest_product_of_3_with_numbers_in_order()
42+
{
43+
const string digits = "0123456789";
44+
const int span = 3;
45+
const int expected = 504;
46+
47+
Assert.That(LargestSeriesProduct.GetLargestProduct(digits, span), Is.EqualTo(expected));
48+
}
49+
50+
[Ignore("Remove to run test")]
51+
[Test]
52+
public void Can_find_the_largest_product_of_3()
53+
{
54+
const string digits = "1027839564";
55+
const int span = 3;
56+
const int expected = 270;
57+
58+
Assert.That(LargestSeriesProduct.GetLargestProduct(digits, span), Is.EqualTo(expected));
59+
}
60+
61+
[Ignore("Remove to run test")]
62+
[Test]
63+
public void Can_find_the_largest_product_of_5_with_numbers_in_order()
64+
{
65+
const string digits = "0123456789";
66+
const int span = 5;
67+
const int expected = 15120;
68+
69+
Assert.That(LargestSeriesProduct.GetLargestProduct(digits, span), Is.EqualTo(expected));
70+
}
71+
72+
[Ignore("Remove to run test")]
73+
[Test]
74+
public void Can_get_the_largest_product_of_a_big_number()
75+
{
76+
const string digits = "73167176531330624919225119674426574742355349194934";
77+
const int span = 6;
78+
const int expected = 23520;
79+
80+
Assert.That(LargestSeriesProduct.GetLargestProduct(digits, span), Is.EqualTo(expected));
81+
}
82+
83+
[Ignore("Remove to run test")]
84+
[Test]
85+
public void Can_get_the_largest_product_of_a_big_number_II()
86+
{
87+
const string digits = "52677741234314237566414902593461595376319419139427";
88+
const int span = 6;
89+
const int expected = 28350;
90+
91+
Assert.That(LargestSeriesProduct.GetLargestProduct(digits, span), Is.EqualTo(expected));
92+
}
93+
94+
[Ignore("Remove to run test")]
95+
[Test]
96+
public void Can_get_the_largest_product_of_a_big_number_III()
97+
{
98+
const string digits = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
99+
const int span = 13;
100+
const long expected = 23514624000;
101+
102+
Assert.That(LargestSeriesProduct.GetLargestProduct(digits, span), Is.EqualTo(expected));
103+
}
104+
105+
[Ignore("Remove to run test")]
106+
[Test]
107+
public void Reports_zero_if_the_only_digits_are_zero()
108+
{
109+
const string digits = "0000";
110+
const int span = 2;
111+
const int expected = 0;
112+
113+
Assert.That(LargestSeriesProduct.GetLargestProduct(digits, span), Is.EqualTo(expected));
114+
}
115+
116+
[Ignore("Remove to run test")]
117+
[Test]
118+
public void Reports_zero_if_all_spans_include_zero()
10119
{
11-
return new LargestSeriesProduct(digits).GetLargestProduct(seriesLength);
120+
const string digits = "99099";
121+
const int span = 3;
122+
const int expected = 0;
123+
124+
Assert.That(LargestSeriesProduct.GetLargestProduct(digits, span), Is.EqualTo(expected));
12125
}
13126

14127
[Ignore("Remove to run test")]
15128
[Test]
16-
public void Largest_product_works_for_small_numbers()
129+
public void Reports_1_for_empty_string_and_empty_product_0_span()
17130
{
18-
Assert.That(new LargestSeriesProduct("19").GetLargestProduct(2), Is.EqualTo(9));
131+
const string digits = "";
132+
const int span = 0;
133+
const int expected = 1;
134+
135+
Assert.That(LargestSeriesProduct.GetLargestProduct(digits, span), Is.EqualTo(expected));
19136
}
20137

21138
[Ignore("Remove to run test")]
22139
[Test]
23-
public void Largest_product_works_for_large_numbers()
140+
public void Reports_1_for_nonempty_string_and_empty_product_0_span()
24141
{
25-
const string LARGE_NUMBER = "73167176531330624919225119674426574742355349194934";
26-
Assert.That(new LargestSeriesProduct(LARGE_NUMBER).GetLargestProduct(6), Is.EqualTo(23520));
142+
const string digits = "123";
143+
const int span = 0;
144+
const int expected = 1;
145+
146+
Assert.That(LargestSeriesProduct.GetLargestProduct(digits, span), Is.EqualTo(expected));
27147
}
28148

29149
[Ignore("Remove to run test")]
30-
[TestCase("0000", 2, ExpectedResult = 0)]
31-
[TestCase("99099", 3, ExpectedResult = 0)]
32-
public int Largest_product_works_if_all_spans_contain_zero(string digits, int seriesLength)
150+
[Test]
151+
public void Rejects_span_longer_than_string_length()
33152
{
34-
return new LargestSeriesProduct(digits).GetLargestProduct(seriesLength);
153+
const string digits = "123";
154+
const int span = 4;
155+
156+
Assert.Throws<ArgumentException>(() => LargestSeriesProduct.GetLargestProduct(digits, span));
35157
}
36158

37159
[Ignore("Remove to run test")]
38-
[TestCase("", ExpectedResult = 1)]
39-
[TestCase("123", ExpectedResult = 1)]
40-
public int Largest_product_for_empty_span_is_1(string digits)
160+
[Test]
161+
public void Rejects_empty_string_and_nonzero_span()
41162
{
42-
return new LargestSeriesProduct(digits).GetLargestProduct(0);
163+
const string digits = "";
164+
const int span = 1;
165+
166+
Assert.Throws<ArgumentException>(() => LargestSeriesProduct.GetLargestProduct(digits, span));
43167
}
44168

45169
[Ignore("Remove to run test")]
46-
[TestCase("123", 4)]
47-
[TestCase("", 1)]
48-
public void Cannot_take_largest_product_of_more_digits_than_input(string digits, int seriesLength)
170+
[Test]
171+
public void Rejects_invalid_character_in_digits()
49172
{
50-
Assert.Throws<ArgumentException>(() => new LargestSeriesProduct(digits).GetLargestProduct(seriesLength));
173+
const string digits = "1234a5";
174+
const int span = 2;
175+
176+
Assert.Throws<ArgumentException>(() => LargestSeriesProduct.GetLargestProduct(digits, span));
177+
}
178+
179+
[Ignore("Remove to run test")]
180+
[Test]
181+
public void Rejects_negative_span()
182+
{
183+
const string digits = "12345";
184+
const int span = -1;
185+
186+
Assert.Throws<ArgumentException>(() => LargestSeriesProduct.GetLargestProduct(digits, span));
51187
}
52-
}
188+
}

0 commit comments

Comments
 (0)