From e9968a3304e6423ba04831179ce6f0c7d483b926 Mon Sep 17 00:00:00 2001 From: Adam Campbell Date: Wed, 4 Sep 2024 07:27:32 -0500 Subject: [PATCH 1/3] Update algo --- src/Illuminate/Support/Number.php | 8 ++++---- tests/Support/SupportNumberTest.php | 11 ++++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Support/Number.php b/src/Illuminate/Support/Number.php index 0b52a7912ae9..d3ddab3a6e12 100644 --- a/src/Illuminate/Support/Number.php +++ b/src/Illuminate/Support/Number.php @@ -240,18 +240,18 @@ public static function clamp(int|float $number, int|float $min, int|float $max) * @param int|float $offset * @return array */ - public static function pairs(int|float $to, int|float $by, int|float $offset = 1) + public static function pairs(int|float $to, int|float $by, int|float $start = 0, int|float $offset = 1) { $output = []; - for ($lower = 0; $lower < $to; $lower += $by) { - $upper = $lower + $by; + for ($lower = $start; $lower < $to; $lower += $by) { + $upper = $lower + $by - $offset; if ($upper > $to) { $upper = $to; } - $output[] = [$lower + $offset, $upper]; + $output[] = [$lower, $upper]; } return $output; diff --git a/tests/Support/SupportNumberTest.php b/tests/Support/SupportNumberTest.php index cf1796bfaa58..605742a8a762 100644 --- a/tests/Support/SupportNumberTest.php +++ b/tests/Support/SupportNumberTest.php @@ -289,8 +289,13 @@ public function testSummarize() public function testPairs() { - $this->assertSame([[1, 10], [11, 20], [21, 25]], Number::pairs(25, 10)); - $this->assertSame([[0, 10], [10, 20], [20, 25]], Number::pairs(25, 10, 0)); - $this->assertSame([[0, 2.5], [2.5, 5.0], [5.0, 7.5], [7.5, 10.0]], Number::pairs(10, 2.5, 0)); + $this->assertSame([[0, 10], [10, 20], [20, 25]], Number::pairs(25, 10, 0, 0)); + $this->assertSame([[0, 9], [10, 19], [20, 25]], Number::pairs(25, 10, 0, 1)); + $this->assertSame([[1, 11], [11, 21], [21, 25]], Number::pairs(25, 10, 1, 0)); + $this->assertSame([[1, 10], [11, 20], [21, 25]], Number::pairs(25, 10, 1, 1)); + $this->assertSame([[0, 2.5], [2.5, 5.0], [5.0, 7.5], [7.5, 10.0]], Number::pairs(10, 2.5, 0, 0)); + $this->assertSame([[0, 2.0], [2.5, 4.5], [5.0, 7.0], [7.5, 9.5]], Number::pairs(10, 2.5, 0, 0.5)); + $this->assertSame([[0.5, 3.0], [3.0, 5.5], [5.5, 8.0], [8.0, 10]], Number::pairs(10, 2.5, 0.5, 0)); + $this->assertSame([[0.5, 2.5], [3.0, 5.0], [5.5, 7.5], [8.0, 10.0]], Number::pairs(10, 2.5, 0.5, 0.5)); } } From d95f0f77f8021c4e9e47a1aa6a8a008315e9848e Mon Sep 17 00:00:00 2001 From: Adam Campbell Date: Wed, 4 Sep 2024 07:31:11 -0500 Subject: [PATCH 2/3] Update Number.php --- src/Illuminate/Support/Number.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Support/Number.php b/src/Illuminate/Support/Number.php index d3ddab3a6e12..3161b38dac3f 100644 --- a/src/Illuminate/Support/Number.php +++ b/src/Illuminate/Support/Number.php @@ -235,9 +235,10 @@ public static function clamp(int|float $number, int|float $min, int|float $max) /** * Split the given number into pairs of min/max values. * - * @param int|float $to - * @param int|float $by - * @param int|float $offset + * @param int|float $to + * @param int|float $by + * @param int|float $start + * @param int|float $offset * @return array */ public static function pairs(int|float $to, int|float $by, int|float $start = 0, int|float $offset = 1) From e33da1fe4caf23ddbea30cf4d07b9ca780f654f2 Mon Sep 17 00:00:00 2001 From: Adam Campbell Date: Wed, 4 Sep 2024 07:48:44 -0500 Subject: [PATCH 3/3] Update SupportNumberTest.php --- tests/Support/SupportNumberTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/Support/SupportNumberTest.php b/tests/Support/SupportNumberTest.php index 605742a8a762..9f2f7cb2b55b 100644 --- a/tests/Support/SupportNumberTest.php +++ b/tests/Support/SupportNumberTest.php @@ -293,6 +293,10 @@ public function testPairs() $this->assertSame([[0, 9], [10, 19], [20, 25]], Number::pairs(25, 10, 0, 1)); $this->assertSame([[1, 11], [11, 21], [21, 25]], Number::pairs(25, 10, 1, 0)); $this->assertSame([[1, 10], [11, 20], [21, 25]], Number::pairs(25, 10, 1, 1)); + $this->assertSame([[0, 1000], [1000, 2000], [2000, 2500]], Number::pairs(2500, 1000, 0, 0)); + $this->assertSame([[0, 999], [1000, 1999], [2000, 2500]], Number::pairs(2500, 1000, 0, 1)); + $this->assertSame([[1, 1001], [1001, 2001], [2001, 2500]], Number::pairs(2500, 1000, 1, 0)); + $this->assertSame([[1, 1000], [1001, 2000], [2001, 2500]], Number::pairs(2500, 1000, 1, 1)); $this->assertSame([[0, 2.5], [2.5, 5.0], [5.0, 7.5], [7.5, 10.0]], Number::pairs(10, 2.5, 0, 0)); $this->assertSame([[0, 2.0], [2.5, 4.5], [5.0, 7.0], [7.5, 9.5]], Number::pairs(10, 2.5, 0, 0.5)); $this->assertSame([[0.5, 3.0], [3.0, 5.5], [5.5, 8.0], [8.0, 10]], Number::pairs(10, 2.5, 0.5, 0));