0
Kotlin tutorial Parking Fee help
Hi making may was through the Kotlin tutorial I now struggle with the exercise Parking Fee I get 3/6 tests green. I rewrote my code couple of times but nothing solves all tests. Maybe some of you can give me a hint to my code. ``` import kotlin.math.min fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 total = when(hours) { in 1..5 -> hours * 1.0 in 5..24 -> min(15.0, 5 + ((hours - 5) * 0.5)) else -> 15.0 + ((hours - 24) * 0.5) } println(total) } ```
4 Answers
+ 3
Felix Eisenmenger
Note : Hours maybe more than 48 so there should be hours % 24 also in else part
else part should be like this:
15 * (hours / 24) + 0.5 * (hours % 24)
2nd case would be like this and donot use min
(hours - 5) * 0.5 + 5
--------+Complete+ Solution----------
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
total = when(hours) {
in 1..5 -> hours * 1.0
in 5..24 -> 5 + ((hours - 5) * 0.5)
else -> 15.0 * (hours / 24) + ((hours % 24) * 0.5)
}
println(total)
}
0
Thanks for your response. I thought only the first 24 will have a Flatrate of 15.
0
Try this code out:-
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
if(hours<=5){
total += hours
println(total)
}
else if(hours>5&&hours<24){
total += 5 + (hours - 5) * 0.5
println(total)
}
else if(hours==24){
total += 15
println(total)
}
else if(hours>24){
total += (hours/24)*15+(hours%24)*0.5
println(total)
}
}
0
I hope my code helps, let me know if you have questions
===
fun main(args: Array<String>) {
var hours = readLine()!!.toDouble()
var total: Double = 0.0
val days: Double
val remaining_hours: Double
if (hours >= 24) {
days = Math.floor(hours/24)
remaining_hours=hours - (days*24)
total= days*15 + (remaining_hours*.5)
}
else{
if (hours < 6){
total = hours
}
else{
total = 5+((hours-5)*.5)
}
}
println(total)
}
===