From 67451d9b088358364b0d75a378b7324ecdb5bba2 Mon Sep 17 00:00:00 2001 From: Tom Krepp Date: Tue, 15 Aug 2017 21:49:33 -0600 Subject: [PATCH 1/2] Completed Code Test, complete with code and a whole lot of comments --- EquilibriumIndex.php | 46 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/EquilibriumIndex.php b/EquilibriumIndex.php index 020dd2b..97f3128 100644 --- a/EquilibriumIndex.php +++ b/EquilibriumIndex.php @@ -1,9 +1,49 @@ $i; $y--) { + $high += $arr[$y]; + echo "high: $y\n"; + } + + # Checks if the sum of the high numbers is equal to the sum of the low numbers. If the sums are equal - the current index is confirmed as an equilibrium index. + if($high === $low) { + array_push($output,$i); + } + } + + # Prints message if no equilibrium indices are found + if(count($output) == 0) { + echo "\nNo equilibrium indices found"; + return; + } return $output; } - + +?> From cd66a13d165aae8f8886b564cd89862d3b14eb2d Mon Sep 17 00:00:00 2001 From: Tom Krepp Date: Tue, 15 Aug 2017 21:57:27 -0600 Subject: [PATCH 2/2] Spacing and Tabs fixed --- EquilibriumIndex.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/EquilibriumIndex.php b/EquilibriumIndex.php index 97f3128..a057a54 100644 --- a/EquilibriumIndex.php +++ b/EquilibriumIndex.php @@ -12,38 +12,38 @@ * @throws Exception If element in array is not an integer */ function getEquilibriums($arr) { - $output = array(); + $output = array(); - # the sum of both the low & high number groups are set to 0, at the start of the outer loop + # the sum of both the low & high number groups are set to 0, at the start of the outer loop for($i = 0; $i < count($arr); $i++) { $low = 0; $high = 0; - echo "\n\n** Loop #$i **\n"; + echo "\n\n** Loop #$i **\n"; - # Starting at the zero index of the array, this loop adds the sum of elements at lower indices - for($x = 0; $x < $i; $x++) { + # Starting at the zero index of the array, this loop adds the sum of elements at lower indices + for($x = 0; $x < $i; $x++) { $low += $arr[$x]; - echo "low: $x\n"; + echo "low: $x\n"; } - # Starting at the last index of the array, this loop adds the sum of elements at higher indices + # Starting at the last index of the array, this loop adds the sum of elements at higher indices for($y = count($arr) - 1; $y > $i; $y--) { $high += $arr[$y]; - echo "high: $y\n"; + echo "high: $y\n"; } - # Checks if the sum of the high numbers is equal to the sum of the low numbers. If the sums are equal - the current index is confirmed as an equilibrium index. + # Checks if the sum of the high numbers is equal to the sum of the low numbers. If the sums are equal - the current index is confirmed as an equilibrium index. if($high === $low) { array_push($output,$i); } } - # Prints message if no equilibrium indices are found - if(count($output) == 0) { - echo "\nNo equilibrium indices found"; - return; - } - return $output; + # Prints message if no equilibrium indices are found + if(count($output) == 0) { + echo "\nNo equilibrium indices found"; + return; + } + return $output; } ?>