0
What is Case 4 of Kotlin - Parking Fee?
Hello, I have been trying to get all Cases of the Kotlin Parking Fee. Ever number I know (0-5 / 5-24 / 24 upwards) runs through the code except Case 4. Do somebody notice the missing numbers or the mistake I made? https://code.sololearn.com/c8Xr4fXr7IYi/?ref=app
7 Réponses
+ 2
Matthias Tschandl for numbers less than 5 , you have else part also executing ,to prevent that put the whole while loop and if else in a else clause.
if(hours <= 5){
total += hours*1
}
else{
while(hours >= 48){
hours -= 24
total += 15
}
if(hours >= 24){
hours -= 24
total += 15.0
total += hours*0.5
}
else{
hours -= 5
total += 5.0
total += hours*0.5
}
}
println(total)
0
Can u copy paste or write shortly about "parking fee"?
And especially about case 4
0
Deprion
https://www.sololearn.com/coach/1129?ref=app
and case 4 is hidden
0
Matthias Tschandl
If hours is more than 48 then your condition should be like this:
if (hours > 48) {
total = 15 * (hours / 24) + 0.5 * (hours % 24)
}
Notes : No need to use while loop just above condition is enough.
Your program should look like this:
if(hours <= 5) {
total = hours * 1.0
} else if (hours > 5 && hours < 24) {
total = 5 + 0.5 * (hours - 5)
} else {
total = 15 * (hours / 24) + 0.5 * (hours % 24);
}
Notes : I didn't check hours > 48 in last else part because it should work if hours is more or equal to 24 or more than 48
0
I don't know what is test case 4, but I just changed my three outputs for 1 "total" output and number of first5hours to the 1.0 and test case 4 pass... maybe it is checking if your output is double even if you multiply the whole numbers
0
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double = 0.0
val first5hours = 1.0
val perHour = 0.5
val for24Hours = 15.0
if ( hours in 1..5){
total = hours * first5hours
} else if( hours in 6..23){
total = (hours - 5)*perHour + (5*first5hours)
} else if(hours >= 24){
var leftHours = hours%24
total = (hours-leftHours)/24 * for24Hours + (leftHours * perHour)
//for 50 hours of parking((50 - (50%24))/24 * 15 + ((50%24) * 0.5))
println(total)
}
0
Here is the most elegant solution I can figure out:
//Created by Jacob Lynn 12/6/2021
fun main(args: Array<String>) {
var hours = 48
//We do not need to initialize total as it will be initialized the first time it is utilized, however since I am incrementing rem I need to initialize it at the beginning
var total: Double
var rem: Double = 0.0
/*The problem can be focused on a set of conditions that if one is satisfied the result can be found.*/
//First we need to find how periods of 24 hours the car was parked, then with the remaining hours multiple by 50 cents.
if (hours >= 24){
rem = Math.floor(hours.toDouble()/24) * 15
hours = hours%24
total = rem + (hours*0.5)
println("End of 24 IF statement subtotal:$rem hours:$hours")
}
//Very straight forward, if not 24 check if more than 5. Charging a block amount for the first 5 then .5 for any remaining hours
else if (hours >= 5){
rem+= 5
hours-= 5
total = rem + (hours*0.5)
println("End of 5 IF statement subtotal:$rem hours:$hours total:$total")
}
else {
total = rem + (hours*1)
}
//The println can be a powerful debugging tool
println("subtotal:$rem hours:$hours total:$total")
}
Actual Code Below
----------------------------------------------------------------
fun main(args: Array<String>) {
var hours = readLine()!!.toInt()
var total: Double
var rem: Double = 0.0
if (hours >= 24){
rem = Math.floor(hours.toDouble()/24) * 15
hours = hours%24
total = rem + (hours*0.5)
}
else if (hours >= 5){
rem+= 5
hours-= 5
total = rem + (hours*0.5)
}
else {
total = rem + (hours*1)
}
println(total)
}