We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
/*
* Complete the 'timeConversion' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/
fun timeConversion(s: String): String {
var ret =""
// Write your code here
if(s.contains("PM")){
var time = s.split(":")
var cal = time[0].toInt()
if(cal<12){
cal = cal+12
}
var calctime = cal.toString()+":"+time[1]+":"+time[2]
var calctimewithoutPm = calctime.split("PM")
ret = calctimewithoutPm[0].toString()
}
else{
var timeWithoutAm = s.split("AM")
var cal =timeWithoutAm[0]
var time =cal.toString().split(":")
var zero = time[0]
if(time[0].toInt()>=12){
zero = ((zero.toInt())-12).toString()
if(zero.toInt()==0){
zero="00"
}
}
var calctime = zero.toString()+":"+time[1]+":"+time[2]
// var calctimewithoutPm = calctime.split("PM")
ret = calctime
}
return ret
}
fun main(args: Array) {
val s = readLine()!!
val result = timeConversion(s)
println(result)
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Time Conversion
You are viewing a single comment's thread. Return to all comments →
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 'timeConversion' function below. * * The function is expected to return a STRING. * The function accepts STRING s as parameter. */
fun timeConversion(s: String): String { var ret ="" // Write your code here if(s.contains("PM")){ var time = s.split(":")
} else{ var timeWithoutAm = s.split("AM") var cal =timeWithoutAm[0] var time =cal.toString().split(":") var zero = time[0] if(time[0].toInt()>=12){ zero = ((zero.toInt())-12).toString() if(zero.toInt()==0){ zero="00" } }
} return ret }
fun main(args: Array) { val s = readLine()!!
}