From 1aaa8656b1b45fe69e7bf4cdfebc6360f21c978e Mon Sep 17 00:00:00 2001 From: caoglish Date: Fri, 23 Jan 2015 09:54:55 +1100 Subject: [PATCH 01/12] improve the byte-format.js: using math instead of loop. --- solutions/javascript/byte-format.js | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/solutions/javascript/byte-format.js b/solutions/javascript/byte-format.js index 86df2d0..5d3bf46 100644 --- a/solutions/javascript/byte-format.js +++ b/solutions/javascript/byte-format.js @@ -1,16 +1,9 @@ module.exports = function (value, precision) { var suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; var factor = Math.pow(10, precision > 0 ? precision : 2); - var suffix = 0; - - while (value >= 1024 && suffixes[++suffix] && suffix < suffixes.length) { - value /= 1024; - } - - if (suffix >= suffixes.length) { - suffix = suffixes.length - 1; - } - - // Return the number rounded to precision. - return (Math.round(value * factor) / factor) + ' ' + suffixes[suffix]; + var suffix=Math.min (Math.floor(Math.log(value)/Math.log(1024)), + suffixes.length-1); + return (Math.round(value/Math.pow(1024,suffix) * factor) / factor) + + ' ' + + suffixes[suffix]; }; \ No newline at end of file From 629f3f86542fda32e1473906d1243738f744c9ac Mon Sep 17 00:00:00 2001 From: caoglish Date: Mon, 9 Jan 2017 14:59:39 +1100 Subject: [PATCH 02/12] - add phpunit test suites --- .gitignore | 1 + composer.json | 5 +++++ phpunit.xml | 6 ++++++ tests/php/factorialTest.php | 17 +++++++++++++++++ 4 files changed, 29 insertions(+) create mode 100644 composer.json create mode 100644 phpunit.xml create mode 100644 tests/php/factorialTest.php diff --git a/.gitignore b/.gitignore index a59ae71..a8dec1e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .idea node_modules *.o +vendor diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..65658fb --- /dev/null +++ b/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "phpunit/phpunit": "5.5.*" + } +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..ae3c20e --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,6 @@ + + + + tests/php + + \ No newline at end of file diff --git a/tests/php/factorialTest.php b/tests/php/factorialTest.php new file mode 100644 index 0000000..437a2f4 --- /dev/null +++ b/tests/php/factorialTest.php @@ -0,0 +1,17 @@ +assertEquals(1,factorial(1) ); + $this->assertEquals(2,factorial(2) ); + $this->assertEquals(6,factorial(3) ); + $this->assertEquals(120,factorial(5) ); + $this->assertEquals(2432902008176640000,factorial(20) ); + } +} +?> \ No newline at end of file From ac03e3d5c30ab6a82b444cad27281988218eacd2 Mon Sep 17 00:00:00 2001 From: caoglish Date: Tue, 10 Jan 2017 16:23:42 +1100 Subject: [PATCH 03/12] - update FactorialTest: format the source code; correct the test name. - add reveseWordsInStringTest regarding interveiw description. - update revese-words-in-string code: need to trim the string. --- solutions/php/reverse-words-in-string.php | 4 +--- tests/php/factorialTest.php | 15 +++++++-------- tests/php/reverseWordsInStringTest.php | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 tests/php/reverseWordsInStringTest.php diff --git a/solutions/php/reverse-words-in-string.php b/solutions/php/reverse-words-in-string.php index 53fd495..3ede846 100644 --- a/solutions/php/reverse-words-in-string.php +++ b/solutions/php/reverse-words-in-string.php @@ -2,7 +2,5 @@ function reverseWordsInStrings($string) { - return implode(array_reverse(preg_split('/\s+/', $string)), ' '); + return trim(implode(array_reverse(preg_split('/\s+/', $string)), ' ')); } - -?> \ No newline at end of file diff --git a/tests/php/factorialTest.php b/tests/php/factorialTest.php index 437a2f4..6544c0e 100644 --- a/tests/php/factorialTest.php +++ b/tests/php/factorialTest.php @@ -1,17 +1,16 @@ assertEquals(1,factorial(1) ); - $this->assertEquals(2,factorial(2) ); - $this->assertEquals(6,factorial(3) ); - $this->assertEquals(120,factorial(5) ); - $this->assertEquals(2432902008176640000,factorial(20) ); + $this->assertEquals(1, factorial(1)); + $this->assertEquals(2, factorial(2)); + $this->assertEquals(6, factorial(3)); + $this->assertEquals(120, factorial(5)); + $this->assertEquals(2432902008176640000, factorial(20)); } } -?> \ No newline at end of file diff --git a/tests/php/reverseWordsInStringTest.php b/tests/php/reverseWordsInStringTest.php new file mode 100644 index 0000000..100f648 --- /dev/null +++ b/tests/php/reverseWordsInStringTest.php @@ -0,0 +1,16 @@ +assertEquals("awesome! are Interviews", reverseWordsInStrings("Interviews are awesome!")); + $this->assertEquals("degree CS", reverseWordsInStrings(" CS degree")); + $this->assertEquals("degree CS", reverseWordsInStrings("CS degree")); + $this->assertEquals("degree CS", reverseWordsInStrings("CS degree ")); + $this->assertEquals("degree CS", reverseWordsInStrings(" CS degree ")); + } +} From 885612b52b68f8c549fa98f8b9a17742949eca8b Mon Sep 17 00:00:00 2001 From: caoglish Date: Tue, 10 Jan 2017 16:54:23 +1100 Subject: [PATCH 04/12] - add sum of array plus one test regarding the interview description. - update sum-of-array-plus-one: change the name; format the code. --- solutions/php/sum-of-array-plus-one.php | 15 +++++++-------- tests/php/sumOfArrayPlusOneTest.php | 13 +++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 tests/php/sumOfArrayPlusOneTest.php diff --git a/solutions/php/sum-of-array-plus-one.php b/solutions/php/sum-of-array-plus-one.php index 0110036..c3783f1 100644 --- a/solutions/php/sum-of-array-plus-one.php +++ b/solutions/php/sum-of-array-plus-one.php @@ -1,11 +1,10 @@ diff --git a/tests/php/sumOfArrayPlusOneTest.php b/tests/php/sumOfArrayPlusOneTest.php new file mode 100644 index 0000000..d9e7028 --- /dev/null +++ b/tests/php/sumOfArrayPlusOneTest.php @@ -0,0 +1,13 @@ +assertEquals(14, SumOfArrayPlusOne([1, 2, 3, 4])); + + } +} From 26703488dd3474e0b6cb7f61d9fd649b1b7b36d7 Mon Sep 17 00:00:00 2001 From: caoglish Date: Fri, 23 Jan 2015 09:54:55 +1100 Subject: [PATCH 05/12] improve the byte-format.js: using math instead of loop. --- solutions/javascript/byte-format.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/javascript/byte-format.js b/solutions/javascript/byte-format.js index 6708c7f..2c808ed 100644 --- a/solutions/javascript/byte-format.js +++ b/solutions/javascript/byte-format.js @@ -7,7 +7,7 @@ module.exports = function (value, precision) { ~~(Math.log(value) / Math.log(1024)), suffixes.length - 1 ); - var num = Math.ceil(value / Math.pow(1024, suffix) * factor) / factor; + var num = Math.ceil(value / Math.pow1(1024, suffix) * factor) / factor; return num + ' ' + suffixes[suffix]; }; From 81eeaa77fcb96bc22dd5318d91bbeb13553c0cd6 Mon Sep 17 00:00:00 2001 From: caoglish Date: Tue, 10 Jan 2017 16:23:42 +1100 Subject: [PATCH 06/12] - update FactorialTest: format the source code; correct the test name. - add reveseWordsInStringTest regarding interveiw description. - update revese-words-in-string code: need to trim the string. --- solutions/php/reverse-words-in-string.php | 4 +--- tests/php/factorialTest.php | 15 +++++++-------- tests/php/reverseWordsInStringTest.php | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 tests/php/reverseWordsInStringTest.php diff --git a/solutions/php/reverse-words-in-string.php b/solutions/php/reverse-words-in-string.php index 53fd495..3ede846 100644 --- a/solutions/php/reverse-words-in-string.php +++ b/solutions/php/reverse-words-in-string.php @@ -2,7 +2,5 @@ function reverseWordsInStrings($string) { - return implode(array_reverse(preg_split('/\s+/', $string)), ' '); + return trim(implode(array_reverse(preg_split('/\s+/', $string)), ' ')); } - -?> \ No newline at end of file diff --git a/tests/php/factorialTest.php b/tests/php/factorialTest.php index 437a2f4..6544c0e 100644 --- a/tests/php/factorialTest.php +++ b/tests/php/factorialTest.php @@ -1,17 +1,16 @@ assertEquals(1,factorial(1) ); - $this->assertEquals(2,factorial(2) ); - $this->assertEquals(6,factorial(3) ); - $this->assertEquals(120,factorial(5) ); - $this->assertEquals(2432902008176640000,factorial(20) ); + $this->assertEquals(1, factorial(1)); + $this->assertEquals(2, factorial(2)); + $this->assertEquals(6, factorial(3)); + $this->assertEquals(120, factorial(5)); + $this->assertEquals(2432902008176640000, factorial(20)); } } -?> \ No newline at end of file diff --git a/tests/php/reverseWordsInStringTest.php b/tests/php/reverseWordsInStringTest.php new file mode 100644 index 0000000..100f648 --- /dev/null +++ b/tests/php/reverseWordsInStringTest.php @@ -0,0 +1,16 @@ +assertEquals("awesome! are Interviews", reverseWordsInStrings("Interviews are awesome!")); + $this->assertEquals("degree CS", reverseWordsInStrings(" CS degree")); + $this->assertEquals("degree CS", reverseWordsInStrings("CS degree")); + $this->assertEquals("degree CS", reverseWordsInStrings("CS degree ")); + $this->assertEquals("degree CS", reverseWordsInStrings(" CS degree ")); + } +} From 8a3032fa2845ca78c7a8c9337f106f5ad3cb0ee6 Mon Sep 17 00:00:00 2001 From: caoglish Date: Tue, 10 Jan 2017 16:54:23 +1100 Subject: [PATCH 07/12] - add sum of array plus one test regarding the interview description. - update sum-of-array-plus-one: change the name; format the code. --- solutions/php/sum-of-array-plus-one.php | 15 +++++++-------- tests/php/sumOfArrayPlusOneTest.php | 13 +++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 tests/php/sumOfArrayPlusOneTest.php diff --git a/solutions/php/sum-of-array-plus-one.php b/solutions/php/sum-of-array-plus-one.php index 0110036..c3783f1 100644 --- a/solutions/php/sum-of-array-plus-one.php +++ b/solutions/php/sum-of-array-plus-one.php @@ -1,11 +1,10 @@ diff --git a/tests/php/sumOfArrayPlusOneTest.php b/tests/php/sumOfArrayPlusOneTest.php new file mode 100644 index 0000000..d9e7028 --- /dev/null +++ b/tests/php/sumOfArrayPlusOneTest.php @@ -0,0 +1,13 @@ +assertEquals(14, SumOfArrayPlusOne([1, 2, 3, 4])); + + } +} From 614d62be7e3fde4164bb240d636b5412cdee4340 Mon Sep 17 00:00:00 2001 From: caoglish Date: Wed, 11 Jan 2017 11:22:16 +1100 Subject: [PATCH 08/12] - roll back byte-format.js: fix one mistake which made during merging. --- solutions/javascript/byte-format.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/javascript/byte-format.js b/solutions/javascript/byte-format.js index 2c808ed..6708c7f 100644 --- a/solutions/javascript/byte-format.js +++ b/solutions/javascript/byte-format.js @@ -7,7 +7,7 @@ module.exports = function (value, precision) { ~~(Math.log(value) / Math.log(1024)), suffixes.length - 1 ); - var num = Math.ceil(value / Math.pow1(1024, suffix) * factor) / factor; + var num = Math.ceil(value / Math.pow(1024, suffix) * factor) / factor; return num + ' ' + suffixes[suffix]; }; From 6acf7b126ff90a976856ce33b0dd9d9009219fc7 Mon Sep 17 00:00:00 2001 From: caoglish Date: Wed, 11 Jan 2017 17:47:33 +1100 Subject: [PATCH 09/12] - add factorialReverse and factorialRecursive test --- tests/php/factorialTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/php/factorialTest.php b/tests/php/factorialTest.php index 6544c0e..f4e783e 100644 --- a/tests/php/factorialTest.php +++ b/tests/php/factorialTest.php @@ -13,4 +13,24 @@ public function test_factorial() $this->assertEquals(120, factorial(5)); $this->assertEquals(2432902008176640000, factorial(20)); } + + public function test_factorialReverse() + { + $this->assertEquals(1, factorialReverse(1)); + $this->assertEquals(2, factorialReverse(2)); + $this->assertEquals(6, factorialReverse(3)); + $this->assertEquals(120, factorialReverse(5)); + $this->assertEquals(2432902008176640000, factorialReverse(20)); + } + + public function test_factorialRecursive() + { + $this->assertEquals(1, factorialRecursive(1)); + $this->assertEquals(2, factorialRecursive(2)); + $this->assertEquals(6, factorialRecursive(3)); + $this->assertEquals(120, factorialRecursive(5)); + $this->assertEquals(2432902008176640000, factorialRecursive(20)); + } + + } From 36a590ff27f3c13abb04f5ff3e07a95ee5ab8e9c Mon Sep 17 00:00:00 2001 From: caoglish Date: Wed, 11 Jan 2017 17:51:27 +1100 Subject: [PATCH 10/12] - remove duplicated code in factorialTest --- tests/php/factorialTest.php | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/tests/php/factorialTest.php b/tests/php/factorialTest.php index f4e783e..b06e01e 100644 --- a/tests/php/factorialTest.php +++ b/tests/php/factorialTest.php @@ -7,30 +7,31 @@ class FactorialTest extends TestCase { public function test_factorial() { - $this->assertEquals(1, factorial(1)); - $this->assertEquals(2, factorial(2)); - $this->assertEquals(6, factorial(3)); - $this->assertEquals(120, factorial(5)); - $this->assertEquals(2432902008176640000, factorial(20)); + $this->verifyFactorial("factorial"); } public function test_factorialReverse() { - $this->assertEquals(1, factorialReverse(1)); - $this->assertEquals(2, factorialReverse(2)); - $this->assertEquals(6, factorialReverse(3)); - $this->assertEquals(120, factorialReverse(5)); - $this->assertEquals(2432902008176640000, factorialReverse(20)); + $this->verifyFactorial("factorialReverse"); } public function test_factorialRecursive() { - $this->assertEquals(1, factorialRecursive(1)); - $this->assertEquals(2, factorialRecursive(2)); - $this->assertEquals(6, factorialRecursive(3)); - $this->assertEquals(120, factorialRecursive(5)); - $this->assertEquals(2432902008176640000, factorialRecursive(20)); + $this->verifyFactorial("factorialRecursive"); } - + private function verifyFactorial($fn) + { + $testDataMap = [ + ["input" => 1, "expect" => 1], + ["input" => 2, "expect" => 2], + ["input" => 3, "expect" => 6], + ["input" => 5, "expect" => 120], + ["input" => 20, "expect" => 2432902008176640000], + ]; + + foreach ($testDataMap as $data) { + $this->assertEquals($data["expect"], call_user_func($fn, $data["input"])); + } + } } From c12ac87ecb2871dc3489e356d2a1189cd6fbcb4a Mon Sep 17 00:00:00 2001 From: caoglish Date: Wed, 11 Jan 2017 18:06:44 +1100 Subject: [PATCH 11/12] - format the factorial code in php --- solutions/php/factorial.php | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/solutions/php/factorial.php b/solutions/php/factorial.php index c2dfc48..2beb022 100644 --- a/solutions/php/factorial.php +++ b/solutions/php/factorial.php @@ -1,35 +1,36 @@ +} From 3e5238f23eec23a2c0b87b4352fe22338f15f0d8 Mon Sep 17 00:00:00 2001 From: caoglish Date: Wed, 11 Jan 2017 18:12:41 +1100 Subject: [PATCH 12/12] - add php unit test: IntegerLengthTest --- tests/php/IntegerLengthTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 tests/php/IntegerLengthTest.php diff --git a/tests/php/IntegerLengthTest.php b/tests/php/IntegerLengthTest.php new file mode 100644 index 0000000..f718471 --- /dev/null +++ b/tests/php/IntegerLengthTest.php @@ -0,0 +1,16 @@ +assertEquals(1, IntegerLength(1)); + $this->assertEquals(2, IntegerLength(22)); + $this->assertEquals(3, IntegerLength(333)); + $this->assertEquals(10, IntegerLength(1234567890)); + + } +}