Skip to content

Instantly share code, notes, and snippets.

@funyin
Last active September 11, 2022 18:17
Show Gist options
  • Save funyin/0d36cd23ce6645682860ec55673e557f to your computer and use it in GitHub Desktop.
Save funyin/0d36cd23ce6645682860ec55673e557f to your computer and use it in GitHub Desktop.
Coding challenge - String representation of time in seconds

From an integer X representing a time duration in seconds produce a simplified string representation.
For example, given X=100, you should output:"1m40s"
Use the following abbreviation w,d,h,m,s to represent:

  • 1w is 1 week
  • 1d is 1 day
  • 1h is 1 hour
  • 1m is 1 minute
  • 1s is 1 second

Only the two largest non-zero integers should be used. Round up the second unit if necessary
to produce only two units even though this might mean the output represents slightly more time than X seconds

Write a function

 fun solution(X: Int): String  

that, given an integer X, returns a string representing the duration

Examples:

  1. Given X=100, return "1m40s"
  2. Given X=7263, return "2hm2m". (7263s=2h1m3s, but this uses too many units, so we round the
    second-largest unit up to 2h2m)
  3. Given X=3605, return 1h5s
fun main(){
print(solution(3600))
}
// output: 1h
fun solution(X: Int): String {
var secondsValue = TimeUnit.SECONDS()
var minutesValue = TimeUnit.MINUTES()
var hoursValue = TimeUnit.HOURS()
var daysValue = TimeUnit.DAYS()
var weeksValue = TimeUnit.WEEKS()
var remainder = X
do {
when {
remainder < 60 -> {
secondsValue = TimeUnit.SECONDS(remainder)
remainder = 0
}
remainder < 3600 -> {
minutesValue = TimeUnit.MINUTES(remainder / 60)
remainder %= 60
}
remainder < 86400 -> {
hoursValue = TimeUnit.HOURS(remainder / 3600)
remainder %= 3600
}
remainder < 604800 -> {
daysValue = TimeUnit.DAYS(remainder / 86400)
remainder %= 86400
}
else -> {
weeksValue = TimeUnit.WEEKS(remainder / 604800)
remainder %= 604800
}
}
} while (remainder != 0)
val order = arrayOf(weeksValue, daysValue, hoursValue, minutesValue, secondsValue)
order.sortBy { it.order }
val selection = order.filter { it.value != 0 }.toMutableList()
selection.getOrNull(2)?.let {
selection[1] = selection[1].mCopy(selection[1].value + 1)
}
return "${selection.first().run { printRepresentation() }}${
selection.getOrNull(1)?.run { printRepresentation() } ?: ""
}"
}
sealed class TimeUnit(open val value:Int, private val abbreviation:String,val order:Int){
fun printRepresentation():String = "$value$abbreviation"
abstract fun mCopy(value: Int = 0):TimeUnit
data class SECONDS(override val value: Int = 0): TimeUnit(value,"s",5) {
override fun mCopy(value: Int): TimeUnit {
return copy(value = value)
}
}
data class MINUTES(override val value: Int = 0): TimeUnit(value,"m",4){
override fun mCopy(value: Int): TimeUnit {
return copy(value = value)
}
}
data class HOURS(override var value: Int = 0): TimeUnit(value,"h",3){
override fun mCopy(value: Int): TimeUnit {
return copy(value = value)
}
}
data class DAYS(override val value: Int = 0): TimeUnit(value,"d",2){
override fun mCopy(value: Int): TimeUnit {
return copy(value = value)
}
}
data class WEEKS(override val value: Int = 0): TimeUnit(value,"w",1){
override fun mCopy(value: Int): TimeUnit {
return copy(value = value)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment