diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f46bb1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,54 @@ +# 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: +.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 + 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); + } } diff --git a/src/Median/Median.php b/src/Median/Median.php index 9d45469..6b3b6be 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) { @@ -37,15 +37,16 @@ public function average() /** * Returns the median of all array values - * - * @return mixed + * @return float|int */ public function median() { - if (count($this->array) %2 == 0) { - return ($this->array[(count($this->array)/2 - 1)]); + $arrayMidpoint = (int) floor((count($this->array) / 2)); + if (count($this->array) % 2 != 0) { + return ($this->array[$arrayMidpoint]); } else { - return $this->array[floor(count($this->array)/2)]; + $arrayMidpointLower = $arrayMidpoint - 1; + return ($this->array[$arrayMidpointLower] + $this->array[$arrayMidpoint]) / 2; } } }