Skip to content

Commit 8257e2b

Browse files
authored
Sync largest-series-product (#833)
1 parent 69d68fa commit 8257e2b

File tree

3 files changed

+94
-72
lines changed

3 files changed

+94
-72
lines changed

exercises/practice/largest-series-product/.meta/example.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

275
class Series

exercises/practice/largest-series-product/.meta/tests.toml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

512
[7c82f8b7-e347-48ee-8a22-f672323324d4]
613
description = "finds the largest product if span equals length"
@@ -48,3 +55,8 @@ description = "rejects invalid character in digits"
4855

4956
[5fe3c0e5-a945-49f2-b584-f0814b4dd1ef]
5057
description = "rejects negative span"
58+
include = false
59+
60+
[c859f34a-9bfe-4897-9c2f-6d7f8598e7f0]
61+
description = "rejects negative span"
62+
reimplements = "5fe3c0e5-a945-49f2-b584-f0814b4dd1ef"

exercises/practice/largest-series-product/LargestSeriesProductTest.php

Lines changed: 79 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

275
class LargestSeriesProductTest extends PHPUnit\Framework\TestCase
@@ -36,6 +14,20 @@ public static function setUpBeforeClass(): void
3614
* We will deal with the series of digits as strings to avoid having them cast to floats.
3715
*/
3816

17+
/**
18+
* uuid: 7c82f8b7-e347-48ee-8a22-f672323324d4
19+
* @testdox finds the largest product if span equals length
20+
*/
21+
public function testFindsTheLargestProductIfSpanEqualsLength(): void
22+
{
23+
$series = new Series("29");
24+
$this->assertEquals(18, $series->largestProduct(2));
25+
}
26+
27+
/**
28+
* uuid: 88523f65-21ba-4458-a76a-b4aaf6e4cb5e
29+
* @testdox can find the largest product of 2 with numbers in order
30+
*/
3931
public function testCanFindTheLargestProductOf2WithNumbersInOrder(): void
4032
{
4133
// The number starts with a 0, qualifying it to be an octal
@@ -44,72 +36,80 @@ public function testCanFindTheLargestProductOf2WithNumbersInOrder(): void
4436
$this->assertEquals(72, $series->largestProduct(2));
4537
}
4638

39+
/**
40+
* uuid: f1376b48-1157-419d-92c2-1d7e36a70b8a
41+
* @testdox can find the largest product of 2
42+
*/
4743
public function testCanFindTheLargestProductOf2(): void
4844
{
4945
$series = new Series("576802143");
5046
$this->assertEquals(48, $series->largestProduct(2));
5147
}
5248

53-
public function testFindsTheLargestProductIfSpanEqualsLength(): void
54-
{
55-
$series = new Series("29");
56-
$this->assertEquals(18, $series->largestProduct(2));
57-
}
58-
49+
/**
50+
* uuid: 46356a67-7e02-489e-8fea-321c2fa7b4a4
51+
* @testdox can find the largest product of 3 with numbers in order
52+
*/
5953
public function testCanFindTheLargestProductOf3WithNumbersInOrder(): void
6054
{
61-
$series = new Series("123456789");
55+
$series = new Series("0123456789");
6256
$this->assertEquals(504, $series->largestProduct(3));
6357
}
6458

59+
/**
60+
* uuid: a2dcb54b-2b8f-4993-92dd-5ce56dece64a
61+
* @testdox can find the largest product of 3
62+
*/
6563
public function testCanFindTheLargestProductOf3(): void
6664
{
6765
$series = new Series("1027839564");
6866
$this->assertEquals(270, $series->largestProduct(3));
6967
}
7068

69+
/**
70+
* uuid: 673210a3-33cd-4708-940b-c482d7a88f9d
71+
* @testdox can find the largest product of 5 with numbers in order
72+
*/
7173
public function testCanFindTheLargestProductOf5WithNumbersInOrder(): void
7274
{
7375
$series = new Series("0123456789");
7476
$this->assertEquals(15120, $series->largestProduct(5));
7577
}
7678

79+
/**
80+
* uuid: 02acd5a6-3bbf-46df-8282-8b313a80a7c9
81+
* @testdox can get the largest product of a big number
82+
*/
7783
public function testCanGetTheLargestProductOfABigNumber(): void
7884
{
7985
$series = new Series("73167176531330624919225119674426574742355349194934");
8086
$this->assertEquals(23520, $series->largestProduct(6));
8187
}
8288

83-
public function testCanGetTheLargestProductOfABigNumberProjectEuler(): void
84-
{
85-
$digits = "731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947"
86-
. "8851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096"
87-
. "3295227443043557668966489504452445231617318564030987111217223831136222989342338030813533627"
88-
. "6614282806444486645238749303589072962904915604407723907138105158593079608667017242712188399"
89-
. "8797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490"
90-
. "7711670556013604839586446706324415722155397536978179778461740649551492908625693219784686224"
91-
. "8283972241375657056057490261407972968652414535100474821663704844031998900088952434506585412"
92-
. "2758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828"
93-
. "4891288314260769004224219022671055626321111109370544217506941658960408071984038509624554443"
94-
. "6298123098787992724428490918884580156166097919133875499200524063689912560717606058861164671"
95-
. "0940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
96-
97-
$series = new Series($digits);
98-
$this->assertEquals(23514624000, $series->largestProduct(13));
99-
}
100-
89+
/**
90+
* uuid: 76dcc407-21e9-424c-a98e-609f269622b5
91+
* @testdox reports zero if the only digits are zero
92+
*/
10193
public function testReportsZeroIfTheOnlyDigitsAreZero(): void
10294
{
10395
$series = new Series("0000");
10496
$this->assertEquals(0, $series->largestProduct(2));
10597
}
10698

99+
/**
100+
* uuid: 6ef0df9f-52d4-4a5d-b210-f6fae5f20e19
101+
* @testdox reports zero if all spans include zero
102+
*/
107103
public function testReportsZeroIfAllSpansIncludeZero(): void
108104
{
109105
$series = new Series("99099");
110106
$this->assertEquals(0, $series->largestProduct(3));
111107
}
112108

109+
/**
110+
* uuid: 5d81aaf7-4f67-4125-bf33-11493cc7eab7
111+
* @testdox rejects span longer than string length
112+
*/
113113
public function testRejectsSpanLongerThanStringLength(): void
114114
{
115115
$this->expectException(InvalidArgumentException::class);
@@ -118,6 +118,30 @@ public function testRejectsSpanLongerThanStringLength(): void
118118
$series->largestProduct(4);
119119
}
120120

121+
/**
122+
* uuid: 06bc8b90-0c51-4c54-ac22-3ec3893a079e
123+
* @testdox reports 1 for empty string and empty product (0 span)
124+
*/
125+
public function testReportsOneForEmptyStringAndEmptyProductSpanZero(): void
126+
{
127+
$series = new Series("");
128+
$this->assertEquals(1, $series->largestProduct(0));
129+
}
130+
131+
/**
132+
* uuid: 3ec0d92e-f2e2-4090-a380-70afee02f4c0
133+
* @testdox reports 1 for nonempty string and empty product (0 span)
134+
*/
135+
public function testReportsOneForNonemptyStringAndEmptyProductSpanZero(): void
136+
{
137+
$series = new Series("123");
138+
$this->assertEquals(1, $series->largestProduct(0));
139+
}
140+
141+
/**
142+
* uuid: 6d96c691-4374-4404-80ee-2ea8f3613dd4
143+
* @testdox rejects empty string and nonzero span
144+
*/
121145
public function testRejectsEmptyStringAndNonzeroSpan(): void
122146
{
123147
$this->expectException(InvalidArgumentException::class);
@@ -126,6 +150,10 @@ public function testRejectsEmptyStringAndNonzeroSpan(): void
126150
$series->largestProduct(1);
127151
}
128152

153+
/**
154+
* uuid: 7a38f2d6-3c35-45f6-8d6f-12e6e32d4d74
155+
* @testdox rejects invalid character in digits
156+
*/
129157
public function testRejectsInvalidCharacterInDigits(): void
130158
{
131159
$this->expectException(InvalidArgumentException::class);
@@ -134,6 +162,10 @@ public function testRejectsInvalidCharacterInDigits(): void
134162
$series->largestProduct(2);
135163
}
136164

165+
/**
166+
* uuid: c859f34a-9bfe-4897-9c2f-6d7f8598e7f0
167+
* @testdox rejects negative span
168+
*/
137169
public function testRejectsNegativeSpan(): void
138170
{
139171
$this->expectException(InvalidArgumentException::class);

0 commit comments

Comments
 (0)