From 1aaa8656b1b45fe69e7bf4cdfebc6360f21c978e Mon Sep 17 00:00:00 2001 From: caoglish Date: Fri, 23 Jan 2015 09:54:55 +1100 Subject: [PATCH 1/8] 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 2/8] - 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 3/8] - 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 4/8] - 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 5/8] 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 6/8] - 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 7/8] - 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 8/8] - 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]; };