Skip to content

Instantly share code, notes, and snippets.

@xymor
Created April 19, 2018 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xymor/9fbb7a057388b2d9c440444127c45ec7 to your computer and use it in GitHub Desktop.
Save xymor/9fbb7a057388b2d9c440444127c45ec7 to your computer and use it in GitHub Desktop.
The Time in Words from Hackerrank in kotlin
val dictionary = mapOf<Int,String>(
1 to "one",
2 to "two",
3 to "three",
4 to "four",
5 to "five",
6 to "six",
7 to "seven",
8 to "eight",
9 to "nine",
10 to "ten",
11 to "eleven",
12 to "twelve",
13 to "thirteen",
14 to "fourteen",
16 to "sixteen",
17 to "seventeen",
18 to "eighteen",
19 to "nineteen",
20 to "twenty",
21 to "twenty one",
22 to "twenty two",
23 to "twenty three",
24 to "twenty four",
25 to "twenty five",
26 to "twenty six",
27 to "twenty seven",
28 to "twenty eight",
29 to "twenty nine")
fun main(args: Array<String>) {
val hour = readLine()!!.toInt()
val minutes = readLine()!!.toInt()
when (minutes) {
15 -> println("quarter past " + dictionary[hour])
30 -> println("half past " + dictionary[hour])
45 -> println("quarter to " + dictionary[hour+1])
0 -> println("${dictionary[hour]} o' clock")
else -> {
if (minutes < 30) {
println(dictionary[minutes] + " minutes past " + dictionary[hour])
} else {
println(dictionary[60-minutes] + " minutes to " + dictionary[hour+1])
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment