Created
May 7, 2019 03:13
-
-
Save cdmunoz/b80f5e915a109800259aaedaf3f0b412 to your computer and use it in GitHub Desktop.
Hacker Rank's TimeInWords in Kotlin
This file contains hidden or 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
package hakerrank | |
import java.io.* | |
import java.math.* | |
import java.security.* | |
import java.text.* | |
import java.util.* | |
import java.util.concurrent.* | |
import java.util.function.* | |
import java.util.regex.* | |
import java.util.stream.* | |
import kotlin.collections.* | |
import kotlin.comparisons.* | |
import kotlin.io.* | |
import kotlin.jvm.* | |
import kotlin.jvm.functions.* | |
import kotlin.jvm.internal.* | |
import kotlin.ranges.* | |
import kotlin.sequences.* | |
import kotlin.text.* | |
// Complete the timeInWords function below. | |
fun timeInWords(h: Int, m: Int): String { | |
/* | |
* 5:00 -> five o' clock | |
* 5:01 -> one minute past five | |
* 5:10 -> ten minutes past five | |
* 5:15 -> quarter past five | |
* 5:30 -> half past five | |
* 5:40 -> twenty minutes to six | |
* 5:45 -> quarter to six | |
* 5:47 -> thirteen minutes to six | |
* 5:28 -> twenty eight minutes past five | |
* */ | |
return when (m) { | |
0 -> "${numberToTextName(h, true)} o' clock" | |
in 1..30 -> { | |
val minStr = if (m == 1) "minute" else "minutes" | |
val initialText = if (m == 15) "quarter" else if (m == 30) "half" else "${numberToTextName(m, false)} $minStr" | |
"$initialText past ${numberToTextName(h, true)}" | |
} | |
else -> { | |
val nextHourValue = 60 | |
val minsNextHour = if (nextHourValue - m == 1) "minute" else "minutes" | |
val initialText = if (nextHourValue - m == 15) "quarter" else "${numberToTextName(nextHourValue - m, false)} $minsNextHour" | |
val hourText = if (h == 12) 1 else (h + 1) | |
"$initialText to ${numberToTextName(hourText, true)}" | |
} | |
} | |
} | |
fun numberToTextName(number: Int, isHours: Boolean): String { | |
val hours = arrayOf("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve") | |
val minutes = arrayOf("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", | |
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", | |
"twenty one", "twenty two", "twenty three", "twenty four", "twenty five", "twenty six", "twenty seven", "twenty eight", "twenty nine", "thirty", | |
"thirty one", "thirty two", "thirty three", "thirty four", "thirty five", "thirty six", "thirty seven", "thirty eight", "thirty nine", "forty", | |
"forty one", "forty two", "forty three", "forty four", "forty five", "forty six", "forty seven", "forty eight", "forty nine", "fifty", | |
"fifty one", "fifty two", "fifty three", "fifty four", "fifty five", "fifty six", "fifty seven", "fifty eight", "fifty nine") | |
return if (isHours) hours[number - 1] else minutes[number - 1] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment