Skip to content

Commit

Permalink
Sync largest-series-product (#833)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasnorre authored Oct 30, 2024
1 parent 69d68fa commit 8257e2b
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 72 deletions.
22 changes: 0 additions & 22 deletions exercises/practice/largest-series-product/.meta/example.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
<?php

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

class Series
Expand Down
18 changes: 15 additions & 3 deletions exercises/practice/largest-series-product/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

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

[5fe3c0e5-a945-49f2-b584-f0814b4dd1ef]
description = "rejects negative span"
include = false

[c859f34a-9bfe-4897-9c2f-6d7f8598e7f0]
description = "rejects negative span"
reimplements = "5fe3c0e5-a945-49f2-b584-f0814b4dd1ef"
126 changes: 79 additions & 47 deletions exercises/practice/largest-series-product/LargestSeriesProductTest.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
<?php

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

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

/**
* uuid: 7c82f8b7-e347-48ee-8a22-f672323324d4
* @testdox finds the largest product if span equals length
*/
public function testFindsTheLargestProductIfSpanEqualsLength(): void
{
$series = new Series("29");
$this->assertEquals(18, $series->largestProduct(2));
}

/**
* uuid: 88523f65-21ba-4458-a76a-b4aaf6e4cb5e
* @testdox can find the largest product of 2 with numbers in order
*/
public function testCanFindTheLargestProductOf2WithNumbersInOrder(): void
{
// The number starts with a 0, qualifying it to be an octal
Expand All @@ -44,72 +36,80 @@ public function testCanFindTheLargestProductOf2WithNumbersInOrder(): void
$this->assertEquals(72, $series->largestProduct(2));
}

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

public function testFindsTheLargestProductIfSpanEqualsLength(): void
{
$series = new Series("29");
$this->assertEquals(18, $series->largestProduct(2));
}

/**
* uuid: 46356a67-7e02-489e-8fea-321c2fa7b4a4
* @testdox can find the largest product of 3 with numbers in order
*/
public function testCanFindTheLargestProductOf3WithNumbersInOrder(): void
{
$series = new Series("123456789");
$series = new Series("0123456789");
$this->assertEquals(504, $series->largestProduct(3));
}

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

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

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

public function testCanGetTheLargestProductOfABigNumberProjectEuler(): void
{
$digits = "731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947"
. "8851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096"
. "3295227443043557668966489504452445231617318564030987111217223831136222989342338030813533627"
. "6614282806444486645238749303589072962904915604407723907138105158593079608667017242712188399"
. "8797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490"
. "7711670556013604839586446706324415722155397536978179778461740649551492908625693219784686224"
. "8283972241375657056057490261407972968652414535100474821663704844031998900088952434506585412"
. "2758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828"
. "4891288314260769004224219022671055626321111109370544217506941658960408071984038509624554443"
. "6298123098787992724428490918884580156166097919133875499200524063689912560717606058861164671"
. "0940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";

$series = new Series($digits);
$this->assertEquals(23514624000, $series->largestProduct(13));
}

/**
* uuid: 76dcc407-21e9-424c-a98e-609f269622b5
* @testdox reports zero if the only digits are zero
*/
public function testReportsZeroIfTheOnlyDigitsAreZero(): void
{
$series = new Series("0000");
$this->assertEquals(0, $series->largestProduct(2));
}

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

/**
* uuid: 5d81aaf7-4f67-4125-bf33-11493cc7eab7
* @testdox rejects span longer than string length
*/
public function testRejectsSpanLongerThanStringLength(): void
{
$this->expectException(InvalidArgumentException::class);
Expand All @@ -118,6 +118,30 @@ public function testRejectsSpanLongerThanStringLength(): void
$series->largestProduct(4);
}

/**
* uuid: 06bc8b90-0c51-4c54-ac22-3ec3893a079e
* @testdox reports 1 for empty string and empty product (0 span)
*/
public function testReportsOneForEmptyStringAndEmptyProductSpanZero(): void
{
$series = new Series("");
$this->assertEquals(1, $series->largestProduct(0));
}

/**
* uuid: 3ec0d92e-f2e2-4090-a380-70afee02f4c0
* @testdox reports 1 for nonempty string and empty product (0 span)
*/
public function testReportsOneForNonemptyStringAndEmptyProductSpanZero(): void
{
$series = new Series("123");
$this->assertEquals(1, $series->largestProduct(0));
}

/**
* uuid: 6d96c691-4374-4404-80ee-2ea8f3613dd4
* @testdox rejects empty string and nonzero span
*/
public function testRejectsEmptyStringAndNonzeroSpan(): void
{
$this->expectException(InvalidArgumentException::class);
Expand All @@ -126,6 +150,10 @@ public function testRejectsEmptyStringAndNonzeroSpan(): void
$series->largestProduct(1);
}

/**
* uuid: 7a38f2d6-3c35-45f6-8d6f-12e6e32d4d74
* @testdox rejects invalid character in digits
*/
public function testRejectsInvalidCharacterInDigits(): void
{
$this->expectException(InvalidArgumentException::class);
Expand All @@ -134,6 +162,10 @@ public function testRejectsInvalidCharacterInDigits(): void
$series->largestProduct(2);
}

/**
* uuid: c859f34a-9bfe-4897-9c2f-6d7f8598e7f0
* @testdox rejects negative span
*/
public function testRejectsNegativeSpan(): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down

0 comments on commit 8257e2b

Please sign in to comment.