From 0813eb56d9197513e9a6043e51d2882ef39fe2cb Mon Sep 17 00:00:00 2001 From: Ivans Grjaznihs Date: Tue, 13 Sep 2022 07:26:24 -0400 Subject: [PATCH 01/10] create unit tests --- homeworkTest.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 homeworkTest.php diff --git a/homeworkTest.php b/homeworkTest.php new file mode 100644 index 00000000..9a62616e --- /dev/null +++ b/homeworkTest.php @@ -0,0 +1,32 @@ +assertTrue(true); + return new HomeWork(); + } + + /** + * @depends testInclude + * @dataProvider taskProvider + */ + public function testTask($value, $expection, $obj) { + $this->assertSame($expection, $obj->task($value)); + } + + public function taskProvider() + { + return [ + 'number is multiple of 3' => [3, 'Foo'], + 'number is multiple of 5' => [5, 'Bar'], + 'number has several multiples' => [15, 'Foo, Bar'], + 'number has not multiples' => [11, 11] + ]; + } +} + +?> From 47a68293803d3e02e23aa1a5cca942f589df87f6 Mon Sep 17 00:00:00 2001 From: Ivans Grjaznihs Date: Tue, 13 Sep 2022 07:42:37 -0400 Subject: [PATCH 02/10] compite step 1 --- homework.php | 19 +++++++++++++++++++ homeworkTest.php | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 homework.php diff --git a/homework.php b/homework.php new file mode 100644 index 00000000..e74fca8c --- /dev/null +++ b/homework.php @@ -0,0 +1,19 @@ + \ No newline at end of file diff --git a/homeworkTest.php b/homeworkTest.php index 9a62616e..9e9ed90a 100644 --- a/homeworkTest.php +++ b/homeworkTest.php @@ -24,7 +24,7 @@ public function taskProvider() 'number is multiple of 3' => [3, 'Foo'], 'number is multiple of 5' => [5, 'Bar'], 'number has several multiples' => [15, 'Foo, Bar'], - 'number has not multiples' => [11, 11] + 'number has not multiples' => [11, '11'] ]; } } From 3d73973d6eddafe9341cd75faef3e84486e15f8c Mon Sep 17 00:00:00 2001 From: Ivans Grjaznihs Date: Tue, 13 Sep 2022 12:41:50 -0400 Subject: [PATCH 03/10] update tests (step 2) --- homeworkTest.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/homeworkTest.php b/homeworkTest.php index 9e9ed90a..76be90b3 100644 --- a/homeworkTest.php +++ b/homeworkTest.php @@ -15,7 +15,15 @@ public function testInclude() * @dataProvider taskProvider */ public function testTask($value, $expection, $obj) { - $this->assertSame($expection, $obj->task($value)); + $this->assertSame($expection, $obj->Task($value)); + } + + /** + * @depends testInclude + */ + public function testNegativeIntegerException($obj) { + $this->expectException(InvalidArgumentException::class); + $obj->Task(-1); } public function taskProvider() @@ -23,7 +31,11 @@ public function taskProvider() return [ 'number is multiple of 3' => [3, 'Foo'], 'number is multiple of 5' => [5, 'Bar'], - 'number has several multiples' => [15, 'Foo, Bar'], + 'number is multiple of 7' => [7, 'Qix'], + 'number is multiple of 3 and 5' => [15, 'Foo, Bar'], + 'number is multiple of 3 and 7' => [21, 'Foo, Qix'], + 'number is multiple of 5 and 7' => [35, 'Bar, Qix'], + 'number is multiple of 3, 5 and 7' => [105, 'Foo, Bar, Qix'], 'number has not multiples' => [11, '11'] ]; } From 1a1f73ca9b0481ba0fb7e2cd12d1fb9b02cbff0c Mon Sep 17 00:00:00 2001 From: Ivans Grjaznihs Date: Tue, 13 Sep 2022 12:59:03 -0400 Subject: [PATCH 04/10] homework step 2 --- homework.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/homework.php b/homework.php index e74fca8c..cc8ea739 100644 --- a/homework.php +++ b/homework.php @@ -3,12 +3,20 @@ class HomeWork { function Task(int $value) : string { + if ($value <= 0) + throw new InvalidArgumentException('Input value must be positive integer'); + $res = []; - if ($value % 3 === 0) - $res[] = 'Foo'; - if ($value % 5 === 0) - $res[] = 'Bar'; + $multiples = [ + [3, 'Foo'], + [5, 'Bar'], + [7, 'Qix'], + ]; + + foreach($multiples as [$multiple, $text]) + if ($value % $multiple === 0) + $res[] = $text; if (count($res)) return join(', ', $res); From 1610bf1754918e1aeb9863472d48967f52a9ffa7 Mon Sep 17 00:00:00 2001 From: Ivans Grjaznihs Date: Wed, 14 Sep 2022 06:11:32 -0400 Subject: [PATCH 05/10] update tests (step 3) --- homeworkTest.php | 135 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 126 insertions(+), 9 deletions(-) diff --git a/homeworkTest.php b/homeworkTest.php index 76be90b3..363e6d19 100644 --- a/homeworkTest.php +++ b/homeworkTest.php @@ -12,12 +12,31 @@ public function testInclude() /** * @depends testInclude - * @dataProvider taskProvider + * @dataProvider taskMultipleProvider + * @dataProvider taskContainProvider + * @dataProvider taskBothFunctionalityProvider + * @dataProvider taskOtherProvider */ public function testTask($value, $expection, $obj) { $this->assertSame($expection, $obj->Task($value)); } + /** + * @depends testInclude + * @dataProvider taskMultipleProvider + */ + public function testMultipleCheckTask($value, $expection, $obj) { + $this->assertSame($expection, $obj->MultipleCheckTask($value)); + } + + /** + * @depends testInclude + * @dataProvider taskContainProvider + */ + public function testContainCheckTask($value, $expection, $obj) { + $this->assertSame($expection, $obj->ContainCheckTask($value)); + } + /** * @depends testInclude */ @@ -26,19 +45,117 @@ public function testNegativeIntegerException($obj) { $obj->Task(-1); } - public function taskProvider() + public function taskMultipleProvider() + { + return [ + 'number is multiple of 3 and without contains' => [6, 'Foo'], + 'number is multiple of 5 and without contains' => [10, 'Bar'], + 'number is multiple of 7 and without contains' => [14, 'Qix'], + 'number is multiple of 3 and 5 and without contains' => [60, 'Foo, Bar'], + 'number is multiple of 3 and 7 and without contains' => [21, 'Foo, Qix'], + 'number is multiple of 5 and 7 and without contains' => [140, 'Bar, Qix'], + 'number is multiple of 3, 5 and 7 and without contains' => [210, 'Foo, Bar, Qix'], + ]; + } + + public function taskContainProvider() + { + return [ + + 'number contains 3 and without multiples' => [13, 'Foo'], + 'number contains two 3 and without multiples' => [313, 'Foo, Foo'], + + 'number contains 5 and without multiples' => [51, 'Bar'], + 'number contains two 5 and without multiples' => [551, 'Bar, Bar'], + + 'number contains 7 and without multiples' => [17, 'Qix'], + 'number contains two 7 and without multiples' => [277, 'Qix, Qix'], + + 'number contains 3 and 5 and without multiples' => [53, 'Bar, Foo'], + 'number contains several 3 and 5 and without multiples' => [353, 'Foo, Bar, Foo'], + + 'number contains 3 and 7 and without multiples' => [73, 'Qix, Foo'], + 'number contains several 3 and 7 and without multiples' => [373, 'Foo, Qix, Foo'], + + 'number contains 5 and 7 and without multiples' => [57, 'Bar, Qix'], + 'number contains several 5 and 7 and without multiples' => [373, 'Foo, Qix, Foo'], + + 'number contains 3, 5 and 7 and without multiples' => [5537, 'Bar, Bar, Foo, Qix'], + ]; + } + + + public function taskBothFunctionalityProvider() + { + return [ + + 'number contains 3 and is multiple of 3' => [3, 'Foo, Foo'], + 'number contains 3 and is multiple of 5' => [130, 'Bar, Foo'], + 'number contains 3 and is multiple of 7' => [203, 'Qix, Foo'], + 'number contains 3 and is multiple of 3 and 5' => [30, 'Foo, Bar, Foo'], + 'number contains 3 and is multiple of 3 and 7' => [63, 'Foo, Qix, Foo'], + 'number contains 3 and is multiple of 5 and 7' => [2030, 'Bar, Qix, Foo'], + 'number contains 3 and is multiple of 3, 5 and 7' => [630, 'Foo, Bar, Qix, Foo'], + + 'number contains 5 and is multiple of 3' => [51, 'Foo, Bar'], + 'number contains 5 and is multiple of 5' => [5, 'Bar, Bar'], + 'number contains 5 and is multiple of 7' => [56, 'Qix, Bar'], + 'number contains 5 and is multiple of 3 and 5' => [15, 'Foo, Bar, Bar'], + 'number contains 5 and is multiple of 3 and 7' => [252, 'Foo, Qix, Bar'], + 'number contains 5 and is multiple of 5 and 7' => [245, 'Bar, Qix, Bar'], + 'number contains 5 and is multiple of 3, 5 and 7' => [105, 'Foo, Bar, Qix, Bar'], + + 'number contains 7 and is multiple of 3' => [27, 'Foo, Qix'], + 'number contains 7 and is multiple of 5' => [170, 'Bar, Qix'], + 'number contains 7 and is multiple of 7' => [7, 'Qix, Qix'], + 'number contains 7 and is multiple of 3 and 5' => [720, 'Foo, Bar, Qix'], + 'number contains 7 and is multiple of 3 and 7' => [147, 'Foo, Qix, Qix'], + 'number contains 7 and is multiple of 5 and 7' => [70, 'Bar, Qix, Qix'], + 'number contains 7 and is multiple of 3, 5 and 7' => [1470, 'Foo, Bar, Qix, Qix'], + + + 'number contains 3, 5 and is multiple of 3' => [153, 'Foo, Bar, Foo'], + 'number contains 3, 5 and is multiple of 5' => [235, 'Bar, Foo, Bar'], + 'number contains 3, 5 and is multiple of 7' => [532, 'Qix, Bar, Foo'], + 'number contains 3, 5 and is multiple of 3 and 5' => [135, 'Foo, Bar, Foo, Bar'], + 'number contains 3, 5 and is multiple of 3 and 7' => [1533, 'Foo, Qix, Bar, Foo. Foo'], + 'number contains 3, 5 and is multiple of 5 and 7' => [35, 'Bar, Qix, Foo, Bar'], + 'number contains 3, 5 and is multiple of 3, 5 and 7' => [315, 'Foo, Bar, Qix, Foo, Bar'], + + 'number contains 3, 7 and is multiple of 3' => [237, 'Foo, Foo, Qix'], + 'number contains 3, 7 and is multiple of 5' => [370, 'Bar, Foo, Qix'], + 'number contains 3, 7 and is multiple of 7' => [371, 'Qix, Foo, Qix'], + 'number contains 3, 7 and is multiple of 3 and 5' => [2370, 'Foo, Bar, Foo, Qix'], + 'number contains 3, 7 and is multiple of 3 and 7' => [273, 'Foo, Qix, Qix, Foo'], + 'number contains 3, 7 and is multiple of 5 and 7' => [3710, 'Bar, Qix, Foo, Qix'], + 'number contains 3, 7 and is multiple of 3, 5 and 7' => [2730, 'Foo, Bar, Qix, Qix, Foo'], + + 'number contains 5, 7 and is multiple of 3' => [57, 'Foo, Bar, Qix'], + 'number contains 5, 7 and is multiple of 5' => [275, 'Bar, Qix, Bar'], + 'number contains 5, 7 and is multiple of 7' => [574, 'Qix, Bar, Qix'], + 'number contains 5, 7 and is multiple of 3 and 5' => [75, 'Foo, Bar, Qix, Bar'], + 'number contains 5, 7 and is multiple of 3 and 7' => [567, 'Foo, Qix, Bar, Qix'], + 'number contains 5, 7 and is multiple of 5 and 7' => [175, 'Bar, Qix, Qix, Bar'], + 'number contains 5, 7 and is multiple of 3, 5 and 7' => [1785, 'Foo, Bar, Qix, Qix, Bar'], + + 'number contains 3, 5, 7 and is multiple of 3' => [537, 'Foo, Bar, Foo, Qix'], + 'number contains 3, 5, 7 and is multiple of 5' => [1375, 'Bar, Foo, Qix, Bar'], + 'number contains 3, 5, 7 and is multiple of 7' => [3157, 'Qix, Foo, Bar, Qix'], + 'number contains 3, 5, 7 and is multiple of 3 and 5' => [375, 'Foo, Bar, Foo, Qix, Bar'], + 'number contains 3, 5, 7 and is multiple of 3 and 7' => [357, 'Foo, Qix, Foo, Bar, Qix'], + 'number contains 3, 5, 7 and is multiple of 5 and 7' => [3745, 'Bar, Qix, Foo, Qix, Bar'], + 'number contains 3, 5, 7 and is multiple of 3, 5 and 7' => [735, 'Foo, Bar, Qix, Qix, Foo, Bar'] + ]; + } + + + public function taskOtherProvider() { return [ - 'number is multiple of 3' => [3, 'Foo'], - 'number is multiple of 5' => [5, 'Bar'], - 'number is multiple of 7' => [7, 'Qix'], - 'number is multiple of 3 and 5' => [15, 'Foo, Bar'], - 'number is multiple of 3 and 7' => [21, 'Foo, Qix'], - 'number is multiple of 5 and 7' => [35, 'Bar, Qix'], - 'number is multiple of 3, 5 and 7' => [105, 'Foo, Bar, Qix'], 'number has not multiples' => [11, '11'] ]; } + } ?> From d0c4b486e1972835c67d84235e7f2234153cdec1 Mon Sep 17 00:00:00 2001 From: Ivans Grjaznihs Date: Wed, 14 Sep 2022 07:02:08 -0400 Subject: [PATCH 06/10] complite step 3 --- homework.php | 54 +++++++++++++++++++++++++++++++++++++++++------- homeworkTest.php | 8 +++---- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/homework.php b/homework.php index cc8ea739..0b4c5d23 100644 --- a/homework.php +++ b/homework.php @@ -1,6 +1,38 @@ 'Foo', + 5 => 'Bar', + 7 => 'Qix', + ]; + + function MultipleCheckTask(int $value) : string + { + $res = []; + + foreach(self::MULTIPLES as $multiple => $text) + if ($value % $multiple === 0) + $res[] = $text; + + return join(', ', $res); + } + + function ContainCheckTask(int $value) : string + { + $res = []; + + while($value > 0) + { + [$value, $key] = [intdiv($value, 10), $value % 10]; + + if (array_key_exists($key, self::MULTIPLES)) + $res[] = self::MULTIPLES[$key]; + } + + return join(', ', array_reverse($res)); + } + function Task(int $value) : string { if ($value <= 0) @@ -8,20 +40,26 @@ function Task(int $value) : string $res = []; - $multiples = [ - [3, 'Foo'], - [5, 'Bar'], - [7, 'Qix'], - ]; + $checkFunctions = ['MultipleCheckTask', 'ContainCheckTask']; - foreach($multiples as [$multiple, $text]) - if ($value % $multiple === 0) - $res[] = $text; + foreach($checkFunctions as $func) + { + $ret = $this->$func($value); + if($ret !== '') + $res[] = $ret; + } if (count($res)) return join(', ', $res); else return (string)$value; } + + } + +// $hw = new HomeWork(); +// // echo($hw->ContainCheckTask(3).PHP_EOL); +// // echo($hw->MultipleCheckTask(3).PHP_EOL); +// echo($hw->Task(3).PHP_EOL); ?> \ No newline at end of file diff --git a/homeworkTest.php b/homeworkTest.php index 363e6d19..e67033d9 100644 --- a/homeworkTest.php +++ b/homeworkTest.php @@ -65,7 +65,7 @@ public function taskContainProvider() 'number contains 3 and without multiples' => [13, 'Foo'], 'number contains two 3 and without multiples' => [313, 'Foo, Foo'], - 'number contains 5 and without multiples' => [51, 'Bar'], + 'number contains 5 and without multiples' => [52, 'Bar'], 'number contains two 5 and without multiples' => [551, 'Bar, Bar'], 'number contains 7 and without multiples' => [17, 'Qix'], @@ -77,10 +77,10 @@ public function taskContainProvider() 'number contains 3 and 7 and without multiples' => [73, 'Qix, Foo'], 'number contains several 3 and 7 and without multiples' => [373, 'Foo, Qix, Foo'], - 'number contains 5 and 7 and without multiples' => [57, 'Bar, Qix'], + 'number contains 5 and 7 and without multiples' => [157, 'Bar, Qix'], 'number contains several 5 and 7 and without multiples' => [373, 'Foo, Qix, Foo'], - 'number contains 3, 5 and 7 and without multiples' => [5537, 'Bar, Bar, Foo, Qix'], + 'number contains 3, 5 and 7 and without multiples' => [5371, 'Bar, Foo, Qix'], ]; } @@ -118,7 +118,7 @@ public function taskBothFunctionalityProvider() 'number contains 3, 5 and is multiple of 5' => [235, 'Bar, Foo, Bar'], 'number contains 3, 5 and is multiple of 7' => [532, 'Qix, Bar, Foo'], 'number contains 3, 5 and is multiple of 3 and 5' => [135, 'Foo, Bar, Foo, Bar'], - 'number contains 3, 5 and is multiple of 3 and 7' => [1533, 'Foo, Qix, Bar, Foo. Foo'], + 'number contains 3, 5 and is multiple of 3 and 7' => [1533, 'Foo, Qix, Bar, Foo, Foo'], 'number contains 3, 5 and is multiple of 5 and 7' => [35, 'Bar, Qix, Foo, Bar'], 'number contains 3, 5 and is multiple of 3, 5 and 7' => [315, 'Foo, Bar, Qix, Foo, Bar'], From 41448673aefac9624db32bb23111ab5e73b99432 Mon Sep 17 00:00:00 2001 From: Ivans Grjaznihs Date: Thu, 15 Sep 2022 06:09:56 -0400 Subject: [PATCH 07/10] changed tests (step 4) --- homeworkTest.php | 164 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 150 insertions(+), 14 deletions(-) diff --git a/homeworkTest.php b/homeworkTest.php index e67033d9..6fb2dd8b 100644 --- a/homeworkTest.php +++ b/homeworkTest.php @@ -12,29 +12,60 @@ public function testInclude() /** * @depends testInclude - * @dataProvider taskMultipleProvider - * @dataProvider taskContainProvider - * @dataProvider taskBothFunctionalityProvider + * @dataProvider taskFooBarQixMultipleProvider + * @dataProvider taskFooBarQixContainProvider + * @dataProvider taskFooBarQixBothFunctionalityProvider * @dataProvider taskOtherProvider */ - public function testTask($value, $expection, $obj) { + public function testFooBarQixTask($value, $expection, $obj) { $this->assertSame($expection, $obj->Task($value)); } /** * @depends testInclude - * @dataProvider taskMultipleProvider + * @dataProvider taskFooBarQixMultipleProvider */ - public function testMultipleCheckTask($value, $expection, $obj) { - $this->assertSame($expection, $obj->MultipleCheckTask($value)); + public function testFooBarQixMultipleCheckTask($value, $expection, $obj) { + $arr = $obj->MultipleCheckTask($value, [3 => 'Foo', 5 => 'Bar', 7 => 'Qix']); + $this->assertSame($expection, join(', ', $arr)); } /** * @depends testInclude - * @dataProvider taskContainProvider + * @dataProvider taskFooBarQixContainProvider */ - public function testContainCheckTask($value, $expection, $obj) { - $this->assertSame($expection, $obj->ContainCheckTask($value)); + public function testFooBarQixContainCheckTask($value, $expection, $obj) { + $arr = $obj->ContainCheckTask($value, [3 => 'Foo', 5 => 'Bar', 7 => 'Qix']); + $this->assertSame($expection, join(', ', $arr)); + } + + /** + * @depends testInclude + * @dataProvider taskInfQixFooMultipleProvider + * @dataProvider taskInfQixFooContainProvider + * @dataProvider taskInfQixFooBothFunctionalityProvider + * @dataProvider taskOtherProvider + */ + public function testInfQixFooTask($value, $expection, $obj) { + $this->assertSame($expection, $obj->NewTask($value)); + } + + /** + * @depends testInclude + * @dataProvider taskInfQixFooMultipleProvider + */ + public function testInfQixFooMultipleCheckTask($value, $expection, $obj) { + $arr = $obj->MultipleCheckTask($value, [8 => 'Inf', 7 => 'Qix', 3 => 'Foo']); + $this->assertSame($expection, join('; ', $arr)); + } + + /** + * @depends testInclude + * @dataProvider taskInfQixFooContainProvider + */ + public function testInfQixFooContainCheckTask($value, $expection, $obj) { + $arr = $obj->ContainCheckTask($value, [8 => 'Inf', 7 => 'Qix', 3 => 'Foo']); + $this->assertSame($expection, join('; ', $arr)); } /** @@ -45,7 +76,7 @@ public function testNegativeIntegerException($obj) { $obj->Task(-1); } - public function taskMultipleProvider() + public function taskFooBarQixMultipleProvider() { return [ 'number is multiple of 3 and without contains' => [6, 'Foo'], @@ -58,7 +89,21 @@ public function taskMultipleProvider() ]; } - public function taskContainProvider() + + public function taskInfQixFooMultipleProvider() + { + return [ + 'number is multiple of 3 and without contains' => [6, 'Foo'], + 'number is multiple of 8 and without contains' => [16, 'Inf'], + 'number is multiple of 7 and without contains' => [14, 'Qix'], + 'number is multiple of 3 and 8 and without contains' => [24, 'Inf; Foo'], + 'number is multiple of 3 and 7 and without contains' => [21, 'Qix; Foo'], + 'number is multiple of 8 and 7 and without contains' => [56, 'Inf; Qix'], + 'number is multiple of 3, 8 and 7 and without contains' => [504, 'Inf; Qix; Foo'], + ]; + } + + public function taskFooBarQixContainProvider() { return [ @@ -78,14 +123,41 @@ public function taskContainProvider() 'number contains several 3 and 7 and without multiples' => [373, 'Foo, Qix, Foo'], 'number contains 5 and 7 and without multiples' => [157, 'Bar, Qix'], - 'number contains several 5 and 7 and without multiples' => [373, 'Foo, Qix, Foo'], + 'number contains several 5 and 7 and without multiples' => [757, 'Qix, Bar, Qix'], 'number contains 3, 5 and 7 and without multiples' => [5371, 'Bar, Foo, Qix'], ]; } + public function taskInfQixFooContainProvider() + { + return [ + + 'number contains 3 and without multiples' => [13, 'Foo'], + 'number contains two 3 and without multiples' => [313, 'Foo; Foo'], + + 'number contains 8 and without multiples' => [82, 'Inf'], + 'number contains two 8 and without multiples' => [881, 'Inf; Inf'], + + 'number contains 7 and without multiples' => [17, 'Qix'], + 'number contains two 7 and without multiples' => [277, 'Qix; Qix'], + + 'number contains 3 and 8 and without multiples' => [83, 'Inf; Foo'], + 'number contains several 3 and 8 and without multiples' => [383, 'Foo; Inf; Foo'], + + 'number contains 3 and 7 and without multiples' => [73, 'Qix; Foo'], + 'number contains several 3 and 7 and without multiples' => [373, 'Foo; Qix; Foo'], + + 'number contains 8 and 7 and without multiples' => [187, 'Inf; Qix'], + 'number contains several 8 and 7 and without multiples' => [787, 'Qix; Inf; Qix'], + + 'number contains 3, 8 and 7 and without multiples' => [8371, 'Inf; Foo; Qix'], + ]; + } + + - public function taskBothFunctionalityProvider() + public function taskFooBarQixBothFunctionalityProvider() { return [ @@ -149,6 +221,70 @@ public function taskBothFunctionalityProvider() } + public function taskInfQixFooBothFunctionalityProvider() + { + return [ + + 'number contains 3 and is multiple of 3' => [3, 'Foo; Foo'], + 'number contains 3 and is multiple of 8' => [136, 'Inf; Foo'], + 'number contains 3 and is multiple of 7' => [203, 'Qix; Foo'], + 'number contains 3 and is multiple of 3 and 8' => [312, 'Inf; Foo; Foo'], + 'number contains 3 and is multiple of 3 and 7' => [63, 'Qix; Foo; Foo'], + 'number contains 3 and is multiple of 8 and 7' => [392, 'Inf; Qix; Foo'], + 'number contains 3 and is multiple of 3, 8 and 7' => [1344, 'Inf; Qix; Foo; Foo'], + + 'number contains 8 and is multiple of 3' => [18, 'Foo; Inf'], + 'number contains 8 and is multiple of 8' => [8, 'Inf; Inf'], + 'number contains 8 and is multiple of 7' => [28, 'Qix; Inf'], + 'number contains 8 and is multiple of 3 and 8' => [48, 'Inf; Foo; Inf'], + 'number contains 8 and is multiple of 3 and 7' => [84, 'Qix; Foo; Inf'], + 'number contains 8 and is multiple of 8 and 7' => [280, 'Inf; Qix; Inf'], + 'number contains 8 and is multiple of 3, 8 and 7' => [840, 'Inf; Qix; Foo; Inf'], + + 'number contains 7 and is multiple of 3' => [27, 'Foo; Qix'], + 'number contains 7 and is multiple of 8' => [176, 'Inf; Qix'], + 'number contains 7 and is multiple of 7' => [7, 'Qix; Qix'], + 'number contains 7 and is multiple of 3 and 8' => [72, 'Inf; Foo; Qix'], + 'number contains 7 and is multiple of 3 and 7' => [147, 'Qix; Foo; Qix'], + 'number contains 7 and is multiple of 8 and 7' => [1792, 'Inf; Qix; Qix'], + 'number contains 7 and is multiple of 3, 8 and 7' => [672, 'Inf; Qix; Foo; Qix'], + + + 'number contains 3, 8 and is multiple of 3' => [183, 'Foo; Inf; Foo'], + 'number contains 3, 8 and is multiple of 8' => [328, 'Inf; Foo; Inf'], + 'number contains 3, 8 and is multiple of 7' => [238, 'Qix; Foo; Inf'], + 'number contains 3, 8 and is multiple of 3 and 8' => [384, 'Inf; Foo; Foo; Inf'], + 'number contains 3, 8 and is multiple of 3 and 7' => [483, 'Qix; Foo; Inf; Foo'], + 'number contains 3, 8 and is multiple of 8 and 7' => [3080, 'Inf; Qix; Foo; Inf'], + 'number contains 3, 8 and is multiple of 3, 8 and 7' => [3528, 'Inf; Qix; Foo; Foo; Inf'], + + 'number contains 3, 7 and is multiple of 3' => [237, 'Foo; Foo; Qix'], + 'number contains 3, 7 and is multiple of 8' => [376, 'Inf; Foo; Qix'], + 'number contains 3, 7 and is multiple of 7' => [371, 'Qix; Foo; Qix'], + 'number contains 3, 7 and is multiple of 3 and 8' => [2376, 'Inf; Foo; Foo; Qix'], + 'number contains 3, 7 and is multiple of 3 and 7' => [357, 'Qix; Foo; Foo; Qix'], + 'number contains 3, 7 and is multiple of 8 and 7' => [1736, 'Inf; Qix; Qix; Foo'], + 'number contains 3, 7 and is multiple of 3, 8 and 7' => [5376, 'Inf; Qix; Foo; Foo; Qix'], + + 'number contains 8, 7 and is multiple of 3' => [87, 'Foo; Inf; Qix'], + 'number contains 8, 7 and is multiple of 8' => [872, 'Inf; Inf; Qix'], + 'number contains 8, 7 and is multiple of 7' => [287, 'Qix; Inf; Qix'], + 'number contains 8, 7 and is multiple of 3 and 8' => [768, 'Inf; Foo; Qix; Inf'], + 'number contains 8, 7 and is multiple of 3 and 7' => [798, 'Qix; Foo; Qix; Inf'], + 'number contains 8, 7 and is multiple of 8 and 7' => [728, 'Inf; Qix; Qix; Inf'], + 'number contains 8, 7 and is multiple of 3, 8 and 7' => [4872, 'Inf; Qix; Foo; Inf; Qix'], + + 'number contains 3, 8, 7 and is multiple of 3' => [387, 'Foo; Foo; Inf; Qix'], + 'number contains 3, 8, 7 and is multiple of 8' => [3872, 'Inf; Foo; Inf; Qix'], + 'number contains 3, 8, 7 and is multiple of 7' => [2387, 'Qix; Foo; Inf; Qix'], + 'number contains 3, 8, 7 and is multiple of 3 and 8' => [3768, 'Inf; Foo; Foo; Qix; Inf'], + 'number contains 3, 8, 7 and is multiple of 3 and 7' => [378, 'Qix; Foo; Foo; Qix; Inf'], + 'number contains 3, 8, 7 and is multiple of 8 and 7' => [27328, 'Inf; Qix; Qix; Foo; Inf'], + 'number contains 3, 8, 7 and is multiple of 3, 8 and 7' => [8736, 'Inf; Qix; Foo; Inf; Qix; Foo'] + ]; + } + + public function taskOtherProvider() { return [ From bc68d1a69907b1592dacd092a88da86539f15468 Mon Sep 17 00:00:00 2001 From: Ivans Grjaznihs Date: Thu, 15 Sep 2022 07:01:34 -0400 Subject: [PATCH 08/10] completed step 4 --- homework.php | 52 +++++++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/homework.php b/homework.php index 0b4c5d23..25e3a4a3 100644 --- a/homework.php +++ b/homework.php @@ -1,24 +1,18 @@ 'Foo', - 5 => 'Bar', - 7 => 'Qix', - ]; - - function MultipleCheckTask(int $value) : string + function MultipleCheckTask(int $value, array $arr) : array { $res = []; - foreach(self::MULTIPLES as $multiple => $text) + foreach($arr as $multiple => $text) if ($value % $multiple === 0) $res[] = $text; - return join(', ', $res); + return $res; } - function ContainCheckTask(int $value) : string + function ContainCheckTask(int $value, array $arr) : array { $res = []; @@ -26,14 +20,33 @@ function ContainCheckTask(int $value) : string { [$value, $key] = [intdiv($value, 10), $value % 10]; - if (array_key_exists($key, self::MULTIPLES)) - $res[] = self::MULTIPLES[$key]; + if (array_key_exists($key, $arr)) + $res[] = $arr[$key]; } - return join(', ', array_reverse($res)); + return array_reverse($res); } + + function Task(int $value) : string + { + $rules = [3 => 'Foo', 5 => 'Bar', 7 => 'Qix']; + $delimiter = ', '; + return $this->MainProcess($value, $rules, $delimiter); + } + + function NewTask(int $value) : string + { + $rules = [8 => 'Inf', 7 => 'Qix', 3 => 'Foo']; + $delimiter = '; '; + return $this->MainProcess($value, $rules, $delimiter); + } + + + + + function MainProcess(int $value, array $rules, string $delimiter) : string { if ($value <= 0) throw new InvalidArgumentException('Input value must be positive integer'); @@ -44,22 +57,15 @@ function Task(int $value) : string foreach($checkFunctions as $func) { - $ret = $this->$func($value); - if($ret !== '') - $res[] = $ret; + $ret = $this->$func($value, $rules); + $res = [...$res, ...$ret]; } if (count($res)) - return join(', ', $res); + return join($delimiter, $res); else return (string)$value; } - - } -// $hw = new HomeWork(); -// // echo($hw->ContainCheckTask(3).PHP_EOL); -// // echo($hw->MultipleCheckTask(3).PHP_EOL); -// echo($hw->Task(3).PHP_EOL); ?> \ No newline at end of file From 2fe362f5dae97cad6c5834e7379da2b3c7068d9a Mon Sep 17 00:00:00 2001 From: Ivans Grjaznihs Date: Fri, 16 Sep 2022 03:11:14 -0400 Subject: [PATCH 09/10] updated tests (step 5) --- homeworkTest.php | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/homeworkTest.php b/homeworkTest.php index 6fb2dd8b..a767cfe7 100644 --- a/homeworkTest.php +++ b/homeworkTest.php @@ -44,6 +44,7 @@ public function testFooBarQixContainCheckTask($value, $expection, $obj) { * @dataProvider taskInfQixFooMultipleProvider * @dataProvider taskInfQixFooContainProvider * @dataProvider taskInfQixFooBothFunctionalityProvider + * @dataProvider taskInfQixFooDigitSumProvider * @dataProvider taskOtherProvider */ public function testInfQixFooTask($value, $expection, $obj) { @@ -68,6 +69,14 @@ public function testInfQixFooContainCheckTask($value, $expection, $obj) { $this->assertSame($expection, join('; ', $arr)); } + /** + * @depends testInclude + * @dataProvider taskDigitSumProvider + */ + public function testSumDigits($value, $expection, $obj) { + $this->assertSame($expection, $obj->SumDigits($value)); + } + /** * @depends testInclude */ @@ -76,6 +85,8 @@ public function testNegativeIntegerException($obj) { $obj->Task(-1); } + + public function taskFooBarQixMultipleProvider() { return [ @@ -139,8 +150,8 @@ public function taskInfQixFooContainProvider() 'number contains 8 and without multiples' => [82, 'Inf'], 'number contains two 8 and without multiples' => [881, 'Inf; Inf'], - 'number contains 7 and without multiples' => [17, 'Qix'], - 'number contains two 7 and without multiples' => [277, 'Qix; Qix'], + 'number contains 7 and without multiples' => [47, 'Qix'], + 'number contains two 7 and without multiples' => [577, 'Qix; Qix'], 'number contains 3 and 8 and without multiples' => [83, 'Inf; Foo'], 'number contains several 3 and 8 and without multiples' => [383, 'Foo; Inf; Foo'], @@ -148,7 +159,7 @@ public function taskInfQixFooContainProvider() 'number contains 3 and 7 and without multiples' => [73, 'Qix; Foo'], 'number contains several 3 and 7 and without multiples' => [373, 'Foo; Qix; Foo'], - 'number contains 8 and 7 and without multiples' => [187, 'Inf; Qix'], + 'number contains 8 and 7 and without multiples' => [487, 'Inf; Qix'], 'number contains several 8 and 7 and without multiples' => [787, 'Qix; Inf; Qix'], 'number contains 3, 8 and 7 and without multiples' => [8371, 'Inf; Foo; Qix'], @@ -234,7 +245,7 @@ public function taskInfQixFooBothFunctionalityProvider() 'number contains 3 and is multiple of 3, 8 and 7' => [1344, 'Inf; Qix; Foo; Foo'], 'number contains 8 and is multiple of 3' => [18, 'Foo; Inf'], - 'number contains 8 and is multiple of 8' => [8, 'Inf; Inf'], + 'number contains 8 and is multiple of 8' => [248, 'Inf; Inf'], 'number contains 8 and is multiple of 7' => [28, 'Qix; Inf'], 'number contains 8 and is multiple of 3 and 8' => [48, 'Inf; Foo; Inf'], 'number contains 8 and is multiple of 3 and 7' => [84, 'Qix; Foo; Inf'], @@ -259,7 +270,7 @@ public function taskInfQixFooBothFunctionalityProvider() 'number contains 3, 8 and is multiple of 3, 8 and 7' => [3528, 'Inf; Qix; Foo; Foo; Inf'], 'number contains 3, 7 and is multiple of 3' => [237, 'Foo; Foo; Qix'], - 'number contains 3, 7 and is multiple of 8' => [376, 'Inf; Foo; Qix'], + 'number contains 3, 7 and is multiple of 8' => [1376, 'Inf; Foo; Qix'], 'number contains 3, 7 and is multiple of 7' => [371, 'Qix; Foo; Qix'], 'number contains 3, 7 and is multiple of 3 and 8' => [2376, 'Inf; Foo; Foo; Qix'], 'number contains 3, 7 and is multiple of 3 and 7' => [357, 'Qix; Foo; Foo; Qix'], @@ -270,20 +281,39 @@ public function taskInfQixFooBothFunctionalityProvider() 'number contains 8, 7 and is multiple of 8' => [872, 'Inf; Inf; Qix'], 'number contains 8, 7 and is multiple of 7' => [287, 'Qix; Inf; Qix'], 'number contains 8, 7 and is multiple of 3 and 8' => [768, 'Inf; Foo; Qix; Inf'], - 'number contains 8, 7 and is multiple of 3 and 7' => [798, 'Qix; Foo; Qix; Inf'], + 'number contains 8, 7 and is multiple of 3 and 7' => [1785, 'Qix; Foo; Qix; Inf'], 'number contains 8, 7 and is multiple of 8 and 7' => [728, 'Inf; Qix; Qix; Inf'], 'number contains 8, 7 and is multiple of 3, 8 and 7' => [4872, 'Inf; Qix; Foo; Inf; Qix'], 'number contains 3, 8, 7 and is multiple of 3' => [387, 'Foo; Foo; Inf; Qix'], 'number contains 3, 8, 7 and is multiple of 8' => [3872, 'Inf; Foo; Inf; Qix'], 'number contains 3, 8, 7 and is multiple of 7' => [2387, 'Qix; Foo; Inf; Qix'], - 'number contains 3, 8, 7 and is multiple of 3 and 8' => [3768, 'Inf; Foo; Foo; Qix; Inf'], + 'number contains 3, 8, 7 and is multiple of 3 and 8' => [13728, 'Inf; Foo; Foo; Qix; Inf'], 'number contains 3, 8, 7 and is multiple of 3 and 7' => [378, 'Qix; Foo; Foo; Qix; Inf'], 'number contains 3, 8, 7 and is multiple of 8 and 7' => [27328, 'Inf; Qix; Qix; Foo; Inf'], - 'number contains 3, 8, 7 and is multiple of 3, 8 and 7' => [8736, 'Inf; Qix; Foo; Inf; Qix; Foo'] + 'number contains 3, 8, 7 and is multiple of 3, 8 and 7' => [33768, 'Inf; Qix; Foo; Foo; Foo; Qix; Inf'] ]; } + public function taskInfQixFooDigitSumProvider() + { + return [ + 'sum of all digits is multiple of 8 (1 digit)' => [8, 'Inf; InfInf'], + 'sum of all digits is multiple of 8 (2 digits)' => [17, 'QixInf'], + 'sum of all digits is multiple of 8 (3 digits)' => [376, 'Inf; Foo; QixInf'], + 'sum of all digits is multiple of 8 (4 digits)' => [3768, 'Inf; Foo; Foo; Qix; InfInf'], + ]; + } + + public function taskDigitSumProvider() + { + return [ + '1 digit' => [4, 4], + '2 digits' => [43, 7], + '3 digits' => [435, 12], + '4 digits' => [4359, 21], + ]; + } public function taskOtherProvider() { From 448fbad2df68dcdc1a713a59a75cbfdc7464e3ff Mon Sep 17 00:00:00 2001 From: Ivans Grjaznihs Date: Fri, 16 Sep 2022 03:18:14 -0400 Subject: [PATCH 10/10] full complited homework --- homework.php | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/homework.php b/homework.php index 25e3a4a3..1fa22ba8 100644 --- a/homework.php +++ b/homework.php @@ -27,26 +27,54 @@ function ContainCheckTask(int $value, array $arr) : array return array_reverse($res); } - + function SumDigits(int $value) : int + { + $sum = 0; + while($value > 0) + { + $sum += $value % 10; + $value = intdiv($value, 10); + } + return $sum; + } + function Task(int $value) : string { $rules = [3 => 'Foo', 5 => 'Bar', 7 => 'Qix']; $delimiter = ', '; - return $this->MainProcess($value, $rules, $delimiter); + + $res = $this->MainProcess($value, $rules); + + if (count($res)) + return join($delimiter, $res); + else + return (string)$value; } function NewTask(int $value) : string { $rules = [8 => 'Inf', 7 => 'Qix', 3 => 'Foo']; $delimiter = '; '; - return $this->MainProcess($value, $rules, $delimiter); + + + $res = $this->MainProcess($value, $rules); + + if (count($res)) + { + $ret = join($delimiter, $res); + if ($this->SumDigits($value) % 8 === 0) + $ret = $ret . 'Inf'; + return $ret; + } + else + return (string)$value; } - function MainProcess(int $value, array $rules, string $delimiter) : string + function MainProcess(int $value, array $rules) : array { if ($value <= 0) throw new InvalidArgumentException('Input value must be positive integer'); @@ -61,10 +89,7 @@ function MainProcess(int $value, array $rules, string $delimiter) : string $res = [...$res, ...$ret]; } - if (count($res)) - return join($delimiter, $res); - else - return (string)$value; + return $res; } }