Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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

21 changes: 21 additions & 0 deletions spec/Median/MedianSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
17 changes: 9 additions & 8 deletions src/Median/Median.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class Median
protected $array;

/**
* Class constructor
*
* @param array $array
* Median constructor.
* @param $array
* @throws \Exception
*/
public function __construct($array)
{
Expand Down Expand Up @@ -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;
}
}
}