From d29d884ffd1fd0f41be6d98a0e29e70091f9cf83 Mon Sep 17 00:00:00 2001 From: leon Date: Sat, 29 Apr 2017 10:35:03 +0100 Subject: [PATCH 01/18] add ignore file --- .gitignore | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bc488d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,50 @@ +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + From e55e7de9e063176a49f91dccba97454e0cccc8e5 Mon Sep 17 00:00:00 2001 From: leon Date: Sat, 29 Apr 2017 10:35:49 +0100 Subject: [PATCH 02/18] edit median --- src/Median/Median.php | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Median/Median.php b/src/Median/Median.php index 9d45469..e4f4b24 100644 --- a/src/Median/Median.php +++ b/src/Median/Median.php @@ -6,9 +6,9 @@ class Median protected $array; /** - * Class constructor - * - * @param array $array + * Median constructor. + * @param $array + * @throws \Exception */ public function __construct($array) { @@ -42,10 +42,24 @@ public function average() */ public function median() { - if (count($this->array) %2 == 0) { + if (count($this->array) % 2 == 0) { + $count = count($this->array); + echo "Count: {$count}/n"; return ($this->array[(count($this->array)/2 - 1)]); } else { - return $this->array[floor(count($this->array)/2)]; + // get the upper/lower of the 2 middle values in the array + $lower_middle_of_array = floor(count($this->array) / 2); + echo $lower_middle_of_array; + $upper_middle_of_array = $lower_middle_of_array + 1; + echo $upper_middle_of_array; + $average_of_upper_and_lower = ($lower_middle_of_array + $upper_middle_of_array)/2; + + return $average_of_upper_and_lower; } } } + +$test_array = array(1, 2, 3, 4, 5, 6, 7, 8); +$median = new Median($test_array); + +echo "Median: {$median->median()}\n"; \ No newline at end of file From a3a95e98a06d4da01e0e5430a778a5d39d7bf015 Mon Sep 17 00:00:00 2001 From: leon Date: Sat, 29 Apr 2017 10:36:22 +0100 Subject: [PATCH 03/18] add idea folder --- .idea/median.iml | 18 +++++++++++++ .idea/misc.xml | 68 +++++++++++++++++++++++++++++++++++++++++++++++ .idea/modules.xml | 8 ++++++ .idea/vcs.xml | 6 +++++ 4 files changed, 100 insertions(+) create mode 100644 .idea/median.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/median.iml b/.idea/median.iml new file mode 100644 index 0000000..b313593 --- /dev/null +++ b/.idea/median.iml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..c96e21d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5784aab --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file From c9d21e39116dd79018f49d906afaab3ef314e98b Mon Sep 17 00:00:00 2001 From: leon Date: Sat, 29 Apr 2017 10:39:08 +0100 Subject: [PATCH 04/18] fix median --- src/Median/Median.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Median/Median.php b/src/Median/Median.php index e4f4b24..eecee6e 100644 --- a/src/Median/Median.php +++ b/src/Median/Median.php @@ -42,16 +42,12 @@ public function average() */ public function median() { - if (count($this->array) % 2 == 0) { - $count = count($this->array); - echo "Count: {$count}/n"; - return ($this->array[(count($this->array)/2 - 1)]); + if (count($this->array) % 2 != 0) { + return ($this->array[(count($this->array)/2)]); } else { // get the upper/lower of the 2 middle values in the array $lower_middle_of_array = floor(count($this->array) / 2); - echo $lower_middle_of_array; $upper_middle_of_array = $lower_middle_of_array + 1; - echo $upper_middle_of_array; $average_of_upper_and_lower = ($lower_middle_of_array + $upper_middle_of_array)/2; return $average_of_upper_and_lower; @@ -59,7 +55,7 @@ public function median() } } -$test_array = array(1, 2, 3, 4, 5, 6, 7, 8); +$test_array = array(1, 2, 3, 4, 5, 6); $median = new Median($test_array); echo "Median: {$median->median()}\n"; \ No newline at end of file From c43781aeb01798acc02d67d26d18358be1abd1b8 Mon Sep 17 00:00:00 2001 From: leon Date: Sat, 29 Apr 2017 10:39:41 +0100 Subject: [PATCH 05/18] clean up test code --- src/Median/Median.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Median/Median.php b/src/Median/Median.php index eecee6e..6233d06 100644 --- a/src/Median/Median.php +++ b/src/Median/Median.php @@ -54,8 +54,3 @@ public function median() } } } - -$test_array = array(1, 2, 3, 4, 5, 6); -$median = new Median($test_array); - -echo "Median: {$median->median()}\n"; \ No newline at end of file From b87d216c5fd354f795e81dbc832d9e08737b67a3 Mon Sep 17 00:00:00 2001 From: leon Date: Sat, 29 Apr 2017 14:33:54 +0100 Subject: [PATCH 06/18] fix median --- src/Median/Median.php | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/Median/Median.php b/src/Median/Median.php index 6233d06..a6d1fe5 100644 --- a/src/Median/Median.php +++ b/src/Median/Median.php @@ -10,7 +10,7 @@ class Median * @param $array * @throws \Exception */ - public function __construct($array) + public function __construct() { $this->array = $array; @@ -37,20 +37,27 @@ public function average() /** * Returns the median of all array values - * - * @return mixed + * @param $array + * @return float|int */ - public function median() + public static function median($array) { - if (count($this->array) % 2 != 0) { - return ($this->array[(count($this->array)/2)]); + if (count($array) % 2 != 0) { + $midpoint_in_array = (int) floor((count($array) / 2)); + return ($array[$midpoint_in_array]); } else { - // get the upper/lower of the 2 middle values in the array - $lower_middle_of_array = floor(count($this->array) / 2); - $upper_middle_of_array = $lower_middle_of_array + 1; - $average_of_upper_and_lower = ($lower_middle_of_array + $upper_middle_of_array)/2; + $lower_midpoint_of_array = (int) floor(count($array) / 2); + $upper_midpoint_of_array = $lower_midpoint_of_array + 1; - return $average_of_upper_and_lower; + $average_of_upper_and_lower_midpoints = ($array[$lower_midpoint_of_array] + $array[$upper_midpoint_of_array]) / 2; + + return $average_of_upper_and_lower_midpoints; } } } + +$test_array_1 = [1, 2, 3, 10, 5, 6, 1, 3, 4]; +$test_array_2 = [1, 2, 3, 10, 5, 6, 1, 3]; + +echo "Median: " . Median::median($test_array_1) . "\n"; +echo "Median: " . Median::median($test_array_2) . "\n"; \ No newline at end of file From 51e51daccc3b63b36b4cc11b6cf352889d270f6f Mon Sep 17 00:00:00 2001 From: leon Date: Sat, 29 Apr 2017 14:37:05 +0100 Subject: [PATCH 07/18] fix median - correct key index --- src/Median/Median.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Median/Median.php b/src/Median/Median.php index a6d1fe5..a52beed 100644 --- a/src/Median/Median.php +++ b/src/Median/Median.php @@ -46,7 +46,7 @@ public static function median($array) $midpoint_in_array = (int) floor((count($array) / 2)); return ($array[$midpoint_in_array]); } else { - $lower_midpoint_of_array = (int) floor(count($array) / 2); + $lower_midpoint_of_array = (int) (floor(count($array) / 2) - 1); $upper_midpoint_of_array = $lower_midpoint_of_array + 1; $average_of_upper_and_lower_midpoints = ($array[$lower_midpoint_of_array] + $array[$upper_midpoint_of_array]) / 2; @@ -56,8 +56,8 @@ public static function median($array) } } -$test_array_1 = [1, 2, 3, 10, 5, 6, 1, 3, 4]; -$test_array_2 = [1, 2, 3, 10, 5, 6, 1, 3]; +$test_array_1 = [1, 2, 3, 10, 5, 6, 1, 3]; +$test_array_2 = [1, 2, 3, 10, 5, 6, 1, 3, 4]; echo "Median: " . Median::median($test_array_1) . "\n"; echo "Median: " . Median::median($test_array_2) . "\n"; \ No newline at end of file From 771a8651feaadfcea02208022fc0182b7040a522 Mon Sep 17 00:00:00 2001 From: leon Date: Sat, 29 Apr 2017 14:39:13 +0100 Subject: [PATCH 08/18] refactor --- src/Median/Median.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Median/Median.php b/src/Median/Median.php index a52beed..4132d87 100644 --- a/src/Median/Median.php +++ b/src/Median/Median.php @@ -46,8 +46,8 @@ public static function median($array) $midpoint_in_array = (int) floor((count($array) / 2)); return ($array[$midpoint_in_array]); } else { - $lower_midpoint_of_array = (int) (floor(count($array) / 2) - 1); - $upper_midpoint_of_array = $lower_midpoint_of_array + 1; + $upper_midpoint_of_array = (int) floor(count($array) / 2); + $lower_midpoint_of_array = $upper_midpoint_of_array - 1; $average_of_upper_and_lower_midpoints = ($array[$lower_midpoint_of_array] + $array[$upper_midpoint_of_array]) / 2; @@ -56,8 +56,8 @@ public static function median($array) } } -$test_array_1 = [1, 2, 3, 10, 5, 6, 1, 3]; -$test_array_2 = [1, 2, 3, 10, 5, 6, 1, 3, 4]; +$test_array = [1, 2, 3, 10, 5, 6, 1, 3]; +echo "Median: " . Median::median($test_array) . "\n"; +$test_array = [1, 2, 3, 10, 5, 6, 1, 3, 4]; +echo "Median: " . Median::median($test_array) . "\n"; -echo "Median: " . Median::median($test_array_1) . "\n"; -echo "Median: " . Median::median($test_array_2) . "\n"; \ No newline at end of file From 67a81104f133e5c422506001224ab1fafc32ab4e Mon Sep 17 00:00:00 2001 From: leon Date: Sat, 29 Apr 2017 14:40:36 +0100 Subject: [PATCH 09/18] refactor --- src/Median/Median.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Median/Median.php b/src/Median/Median.php index 4132d87..3dde305 100644 --- a/src/Median/Median.php +++ b/src/Median/Median.php @@ -42,14 +42,12 @@ public function average() */ public static function median($array) { + $midpoint_in_array = (int) floor((count($array) / 2)); if (count($array) % 2 != 0) { - $midpoint_in_array = (int) floor((count($array) / 2)); return ($array[$midpoint_in_array]); } else { - $upper_midpoint_of_array = (int) floor(count($array) / 2); - $lower_midpoint_of_array = $upper_midpoint_of_array - 1; - - $average_of_upper_and_lower_midpoints = ($array[$lower_midpoint_of_array] + $array[$upper_midpoint_of_array]) / 2; + $lower_midpoint_in_array = $midpoint_in_array - 1; + $average_of_upper_and_lower_midpoints = ($array[$lower_midpoint_in_array] + $array[$midpoint_in_array]) / 2; return $average_of_upper_and_lower_midpoints; } From 32e929878f1b719c74a96410d46f1c42980eccff Mon Sep 17 00:00:00 2001 From: leon Date: Sat, 29 Apr 2017 14:46:59 +0100 Subject: [PATCH 10/18] refactor to include sort --- src/Median/Median.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Median/Median.php b/src/Median/Median.php index 3dde305..3a88de8 100644 --- a/src/Median/Median.php +++ b/src/Median/Median.php @@ -10,7 +10,7 @@ class Median * @param $array * @throws \Exception */ - public function __construct() + public function __construct($array) { $this->array = $array; @@ -37,11 +37,12 @@ public function average() /** * Returns the median of all array values - * @param $array * @return float|int */ - public static function median($array) + public function median() { + $array = $this->array; + print_r($array); $midpoint_in_array = (int) floor((count($array) / 2)); if (count($array) % 2 != 0) { return ($array[$midpoint_in_array]); @@ -54,8 +55,7 @@ public static function median($array) } } -$test_array = [1, 2, 3, 10, 5, 6, 1, 3]; -echo "Median: " . Median::median($test_array) . "\n"; -$test_array = [1, 2, 3, 10, 5, 6, 1, 3, 4]; -echo "Median: " . Median::median($test_array) . "\n"; +$test_array = [1, 2, 3, 10, 5, 6, 1, 4]; +$median = new Median($test_array); +echo "Median: " . $median->median() . "\n"; From b9efff44ec817ec45876d5e07ab09d4fa9f6225b Mon Sep 17 00:00:00 2001 From: leon Date: Sat, 29 Apr 2017 14:49:30 +0100 Subject: [PATCH 11/18] remove test code --- src/Median/Median.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Median/Median.php b/src/Median/Median.php index 3a88de8..e0dc41f 100644 --- a/src/Median/Median.php +++ b/src/Median/Median.php @@ -42,7 +42,6 @@ public function average() public function median() { $array = $this->array; - print_r($array); $midpoint_in_array = (int) floor((count($array) / 2)); if (count($array) % 2 != 0) { return ($array[$midpoint_in_array]); @@ -54,8 +53,3 @@ public function median() } } } - -$test_array = [1, 2, 3, 10, 5, 6, 1, 4]; -$median = new Median($test_array); -echo "Median: " . $median->median() . "\n"; - From 1fb124d06587045c1a7ddcb0aececc05b9463b9b Mon Sep 17 00:00:00 2001 From: leon Date: Sat, 29 Apr 2017 17:20:46 +0100 Subject: [PATCH 12/18] update ignore file --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index bc488d7..24b9c0b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 +.idea/codeStyleSettings.xml + # User-specific stuff: .idea/**/workspace.xml .idea/**/tasks.xml From 4413c89f7e676fa70da03e67fff4c8d207efceac Mon Sep 17 00:00:00 2001 From: leon Date: Sat, 29 Apr 2017 17:21:40 +0100 Subject: [PATCH 13/18] add tests for even numbered arrays --- spec/Median/MedianSpec.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/spec/Median/MedianSpec.php b/spec/Median/MedianSpec.php index 70f428b..0eedf5b 100644 --- a/spec/Median/MedianSpec.php +++ b/spec/Median/MedianSpec.php @@ -57,4 +57,25 @@ function it_should_return_a_median_of_9_for_1_4_7_9_11_100_250_3_14() $this->beConstructedWith($array); $this->median($array)->shouldEqual(9); } + + function it_should_return_a_median_of_3point5_for_1_2_3_4_5_6() + { + $array = [1,2,3,4,5,6]; + $this->beConstructedWith($array); + $this->median($array)->shouldEqual(3.5); + } + + function it_should_return_a_median_of_4point5_for_1_2_3_4_5_6_7_8() + { + $array = [1,2,3,4,5,6,7,8]; + $this->beConstructedWith($array); + $this->median($array)->shouldEqual(4.5); + } + + function it_should_return_a_median_of_56_for_66_13_46_378_336_26_4_77_5_92() + { + $array = [66,13,46,378,336,26,4,77,5,92]; + $this->beConstructedWith($array); + $this->median($array)->shouldEqual(56); + } } From 9336ddd84fde1883b523490689f780eda0ddcea1 Mon Sep 17 00:00:00 2001 From: leon Date: Sat, 29 Apr 2017 21:47:56 +0100 Subject: [PATCH 14/18] refactor - variable names --- src/Median/Median.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Median/Median.php b/src/Median/Median.php index e0dc41f..a57883f 100644 --- a/src/Median/Median.php +++ b/src/Median/Median.php @@ -41,15 +41,14 @@ public function average() */ public function median() { - $array = $this->array; - $midpoint_in_array = (int) floor((count($array) / 2)); - if (count($array) % 2 != 0) { - return ($array[$midpoint_in_array]); + $array_midpoint = (int) floor((count($this->array) / 2)); + if (count($this->array) % 2 != 0) { + return ($this->array[$array_midpoint]); } else { - $lower_midpoint_in_array = $midpoint_in_array - 1; - $average_of_upper_and_lower_midpoints = ($array[$lower_midpoint_in_array] + $array[$midpoint_in_array]) / 2; + $array_midpoint_lower = $array_midpoint - 1; + $average_of_midpoints = ($this->array[$array_midpoint_lower] + $this->array[$array_midpoint]) / 2; - return $average_of_upper_and_lower_midpoints; + return $average_of_midpoints; } } } From 0e0e1a5e70ec5912e66f04840331658e9fa7991d Mon Sep 17 00:00:00 2001 From: leon Date: Sat, 29 Apr 2017 21:52:46 +0100 Subject: [PATCH 15/18] edit ignore file - remove idea folder --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 24b9c0b..2f46bb1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 +.idea/ +.idea/* .idea/codeStyleSettings.xml # User-specific stuff: From ddbc7ae6b3f2f23d67c5265377ffbd2e98aebed8 Mon Sep 17 00:00:00 2001 From: leon Date: Sat, 29 Apr 2017 21:54:38 +0100 Subject: [PATCH 16/18] remove idea folder --- .idea/median.iml | 18 ------------- .idea/misc.xml | 68 ----------------------------------------------- .idea/modules.xml | 8 ------ .idea/vcs.xml | 6 ----- 4 files changed, 100 deletions(-) delete mode 100644 .idea/median.iml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/median.iml b/.idea/median.iml deleted file mode 100644 index b313593..0000000 --- a/.idea/median.iml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index c96e21d..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,68 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 5784aab..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From ac54008168204c2219234475faef01c49d5fd8f6 Mon Sep 17 00:00:00 2001 From: leon Date: Sun, 30 Apr 2017 13:03:28 +0100 Subject: [PATCH 17/18] refactor - change variable case --- src/Median/Median.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Median/Median.php b/src/Median/Median.php index a57883f..0a6e5d4 100644 --- a/src/Median/Median.php +++ b/src/Median/Median.php @@ -41,14 +41,14 @@ public function average() */ public function median() { - $array_midpoint = (int) floor((count($this->array) / 2)); + $arrayMidpoint = (int) floor((count($this->array) / 2)); if (count($this->array) % 2 != 0) { - return ($this->array[$array_midpoint]); + return ($this->array[$arrayMidpoint]); } else { - $array_midpoint_lower = $array_midpoint - 1; - $average_of_midpoints = ($this->array[$array_midpoint_lower] + $this->array[$array_midpoint]) / 2; + $arrayMidpointLower = $arrayMidpoint - 1; + $averageOfMidpoints = ($this->array[$arrayMidpointLower] + $this->array[$arrayMidpoint]) / 2; - return $average_of_midpoints; + return $averageOfMidpoints; } } } From 730a4133f63fb4d58475041c990626395ccd0168 Mon Sep 17 00:00:00 2001 From: leon Date: Sun, 30 Apr 2017 13:08:02 +0100 Subject: [PATCH 18/18] refactor - simplify --- src/Median/Median.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Median/Median.php b/src/Median/Median.php index 0a6e5d4..6b3b6be 100644 --- a/src/Median/Median.php +++ b/src/Median/Median.php @@ -46,9 +46,7 @@ public function median() return ($this->array[$arrayMidpoint]); } else { $arrayMidpointLower = $arrayMidpoint - 1; - $averageOfMidpoints = ($this->array[$arrayMidpointLower] + $this->array[$arrayMidpoint]) / 2; - - return $averageOfMidpoints; + return ($this->array[$arrayMidpointLower] + $this->array[$arrayMidpoint]) / 2; } } }