Last active
May 29, 2016 08:57
-
-
Save esase/571b2fe583b0bf6c498b40453209340c to your computer and use it in GitHub Desktop.
Transform the time in a string representation [problem at: https://www.hackerrank.com/challenges/the-time-in-words]
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 | |
function getTime($hour, $minute) | |
{ | |
$hour = (int) $hour; | |
$minute = (int) $minute; | |
// start time again | |
if ($hour == 12 && $minute > 30) { | |
$hour = 0; | |
} | |
$numbers = [ | |
1 => 'one', | |
2 => 'two', | |
3 => 'three', | |
4 => 'four', | |
5 => 'five', | |
6 => 'six', | |
7 => 'seven', | |
8 => 'eight', | |
9 => 'nine', | |
10 => 'ten', | |
11 => 'eleven', | |
12 => 'twelve', | |
13 => 'thirteen', | |
14 => 'fourteen', | |
16 => 'sixteen', | |
17 => 'seventeen', | |
18 => 'eighteen', | |
19 => 'nineteen', | |
20 => 'twenty', | |
21 => 'twenty one', | |
22 => 'twenty two', | |
23 => 'twenty three', | |
24 => 'twenty four', | |
25 => 'twenty five', | |
26 => 'twenty six', | |
27 => 'twenty seven', | |
28 => 'twenty eight', | |
29 => 'twenty nine' | |
]; | |
$defaultMinuteRanges = [ | |
0 => [ | |
'format' => '%s o\' clock', | |
'increaseHour' => false | |
], | |
15 => [ | |
'format' => 'quarter past %s', | |
'increaseHour' => false | |
], | |
30 => [ | |
'format' => 'half past %s', | |
'increaseHour' => false | |
], | |
45 => [ | |
'format' => 'quarter to %s', | |
'increaseHour' => true | |
] | |
]; | |
// check in default range | |
if (isset($defaultMinuteRanges[$minute])) { | |
$range = $defaultMinuteRanges[$minute]; | |
return !$range['increaseHour'] | |
? sprintf($range['format'], $numbers[$hour]) | |
: sprintf($range['format'], $numbers[$hour + 1]); | |
} | |
// dynamically proccess the time | |
$increaseHours = false; | |
// past time | |
if ($minute < 30) { | |
$format = $minute == 1 ? '%s minute past %s' : '%s minutes past %s'; | |
} | |
else { | |
// to time | |
$minute = 60 - $minute; | |
$format = $minute == 1 ? '%s minute to %s' : '%s minutes to %s'; | |
$increaseHours = true; | |
} | |
return !$increaseHours | |
? sprintf($format, $numbers[$minute], $numbers[$hour]) | |
: sprintf($format, $numbers[$minute], $numbers[$hour + 1]); | |
} | |
// tests | |
$testCases = [ | |
[ | |
'h' => '4', | |
'm' => '15', | |
'expect' => 'quarter past four' | |
], | |
[ | |
'h' => '12', | |
'm' => '59', | |
'expect' => 'one minute to one' | |
], | |
[ | |
'h' => '12', | |
'm' => '02', | |
'expect' => 'two minutes past twelve' | |
], | |
[ | |
'h' => '12', | |
'm' => '01', | |
'expect' => 'one minute past twelve' | |
], | |
[ | |
'h' => '12', | |
'm' => '45', | |
'expect' => 'quarter to one' | |
], | |
[ | |
'h' => '12', | |
'm' => '30', | |
'expect' => 'half past twelve' | |
], | |
[ | |
'h' => '5', | |
'm' => '59', | |
'expect' => 'one minute to six' | |
], | |
[ | |
'h' => '5', | |
'm' => '28', | |
'expect' => 'twenty eight minutes past five' | |
], | |
[ | |
'h' => '5', | |
'm' => '47', | |
'expect' => 'thirteen minutes to six' | |
], | |
[ | |
'h' => '5', | |
'm' => '45', | |
'expect' => 'quarter to six' | |
], | |
[ | |
'h' => '5', | |
'm' => '40', | |
'expect' => 'twenty minutes to six' | |
], | |
[ | |
'h' => '5', | |
'm' => '30', | |
'expect' => 'half past five' | |
], | |
[ | |
'h' => '5', | |
'm' => '10', | |
'expect' => 'ten minutes past five' | |
], | |
[ | |
'h' => '5', | |
'm' => '01', | |
'expect' => 'one minute past five' | |
], | |
[ | |
'h' => '5', | |
'm' => '00', | |
'expect' => 'five o\' clock' | |
] | |
]; | |
foreach ($testCases as $time) { | |
$result = getTime($time['h'], $time['m']); | |
if ($time['expect'] == $result) { | |
echo '<span style="color:green">passed:</span> ' . $time['expect'] . ' == ' . $result; | |
} | |
else { | |
echo '<span style="color:red">failed:</span> ' . $time['expect'] . ' <>' . $result; | |
} | |
echo '<br />'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment