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.
I've developed this algorithm in Kotlin. I think by using a map reduces the complexity and makes the code more readable and flexible. Regarding the when clause it may not be the most dynamic option but it seems to me the most performant.
privatevalmap=mapOf(1to"one",2to"two",3to"three",4to"four",5to"five",6to"six",7to"seven",8to"eigth",9to"nine",10to"ten",11to"eleven",12to"twelve",13to"thirteen",14to"fourteen",15to"quarter",16to"sixteen",17to"seventeen",18to"eighteen",19to"nineteen",20to"twenty",21to"twenty one",22to"twenty two",23to"twenty three",24to"twenty four",25to"twenty five",26to"twenty six",27to"twenty seven",28to"twenty eight",29to"twenty nine",30to"half",)funtimeInWords(h:Int,m:Int):String{returnwhen{m==0->map.get(h)+" o' clock"m==1->map.get(1)+" minute past "+map.get(h)m==15||m==30->map.get(m)+" past "+map.get(h)m<30->map.get(m)+" minutes past "+map.get(h)m==45->map.get(15)+" to "+map.get(h+1)m==59->map.get(1)+" minutes to "+map.get(h+1)else->map.get(60-m)+" minutes to "+map.get(h+1)}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Time in Words
You are viewing a single comment's thread. Return to all comments →
I've developed this algorithm in Kotlin. I think by using a map reduces the complexity and makes the code more readable and flexible. Regarding the when clause it may not be the most dynamic option but it seems to me the most performant.