Last active
April 13, 2019 19:47
-
-
Save wesscoby/12f8cc181a296f04a9cc1e30d33a3040 to your computer and use it in GitHub Desktop.
Codewars Kata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* You are the "computer expert" of a local Athletic Association (C.A.A.). Many teams of runners come to compete. | |
* Each time, you get a string of all race results of every team who has run. | |
* For example, here is string showing the individual results of a team of 5 runners: "01|15|59, 1|47|6, 01|17|20, 1|32|34, 2|3|17" | |
* Each part of the string is of the form: h|m|s where h,m,s (h for hour, m for minutes, s for seconds) are positive or null integers, | |
* (represented as strings) with one or two digits. There are no traps in this format. | |
* To compare the results of the teams you are asked for giving three statistics; range, average and median | |
* | |
* Range: difference between the lowest and highest values. In {4, 6, 9, 3, 7}, the lowest value is 3 and highest is 9 so the range is 9 - 3 = 6 | |
* | |
* Mean or Average: To calculate the mean, add together all of the numbers in the set and then divide the sum by the total count of the numbers | |
* | |
* Median: In statistics, the median is the number separating the higher half of a data sample from the lower half. | |
* The median of a finite list of numbers can be found by arranging all the observations from the lowest value to the highest value and | |
* picking the middle one (e.g. the median of {3, 3, 5, 9, 11} is 5) when there is an odd number of observations. | |
* If there is an even number of observations, then there is no single middle value; the median is then defined to be the mean of the two | |
* middle values (the median of {3, 5, 6, 9} is (5 + 6) / 2 = 5.5). | |
* | |
* Your task is to return a string giving these 3 values. For the example given above, the string result will be: | |
* "Range: 00|47|18 Average: 01|35|15 Median: 01|32|34" of the form: "Range: hh|mm|ss Average: hh|mm|ss Median: hh|mm|ss" | |
* where hh, mm, ss are integers (represented by strings) with each 2 digits | |
* | |
* Remarks: | |
* 1. If a result in seconds is ab.xy..., it will be given truncated as ab. | |
* 2. If the given string is "" you will return "". | |
* | |
*/ | |
// Adding zeros in the right place to properly format time strings | |
function add_leading_zero ($number) { | |
return ($number <= 9) ? '0' . $number : $number; | |
}; | |
// convert time from hh|mm|ss to seconds and vice versa | |
function two_way_time_convert ($time) { | |
// check for hh|mm|ss $time format | |
if(preg_match('/(\w{1,2})\|(\w{1,2})\|(\w{1,2})/', $time)) { | |
// convert from hh|mm|ss to seconds | |
$timeArray = explode("|", $time); | |
return ($timeArray[0] * 3600) + ($timeArray[1] * 60) + $timeArray[2]; | |
} else { | |
// convert from seconds to hh|mm|ss | |
$hr = floor($time / 3600); | |
$min = floor(($time - ($hr * 3600)) / 60); | |
$sec = $time - ($hr * 3600) - ($min * 60); | |
return add_leading_zero($hr) . '|' . add_leading_zero($min) . '|' . add_leading_zero($sec); | |
} | |
}; | |
// median function | |
function medianValue($given_array) { | |
// checking if length of array is even | |
if (count($given_array) % 2 === 0) { | |
// find the average of the 2 center numbers | |
$half = floor((count($given_array) / 2)); | |
$med1 = $given_array[$half - 1]; | |
$med2 = $given_array[$half]; | |
return two_way_time_convert((($med1 + $med2) / 2)); | |
} else { | |
// return the number at the center | |
$half = floor(count($given_array) / 2); | |
return two_way_time_convert($given_array[$half]); | |
} | |
} | |
// Main function | |
function stats($stat_string) { | |
// Check string length | |
if (strlen($stat_string) === 0) { | |
return ""; | |
} else { | |
$time_array = explode(",", $stat_string); | |
$time_array_converted = []; | |
foreach ($time_array as $time_string) { | |
array_push($time_array_converted, two_way_time_convert($time_string)); | |
} | |
sort($time_array_converted); | |
$mean = two_way_time_convert(floor(array_sum($time_array_converted) / count($time_array_converted))); | |
$range = two_way_time_convert(max($time_array_converted) - min($time_array_converted)); | |
$median = medianValue($time_array_converted); | |
return "Range: $range Average: $mean Median: $median"; | |
} | |
} | |
echo stats("01|15|59, 1|47|6, 01|17|20, 1|32|34, 2|3|17"); | |
// Output: Range: 00|47|18 Average: 01|35|15 Median: 01|32|34 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment