+ 1

Can you find why my code didn't pass a particular test case?

This problem is from the Kotlin course here on Sololearn. At the end of each chapter, there is a programming challenge (a small project) that you need to solve in order to complete the course. Unfortunately, I have been unable to solve this particular programming challenge throughout the course. My code did not pass test case number 4, and I'm unsure about the problem since the test case is hidden. I hope you can help me figure out what is missing in my code. Thank you. Problem: You are making a car parking software that needs to calculate and output the amount due based on the number of hours the car was parked. The fee is calculated based on the following price structure: - the first 5 hours are billed at $1 per hour. - after that, each hour is billed at $0.5 per hour. - for each 24 hours, there is a flat fee of $15. This means, that, for example, if a car parked for 26 hours, the bill should be 15+(2*0.5) = 16.0, because it was parked for 24 hours plus 2 additional hours. Sample Input: 8 Sample Output: 6.5 Explanation: The first 5 hours are billed at $1/hour, which results in $5. After that, the next 3 hours are billed at $0.5/hour = $1.5. So, the total would be $5+$1.5 = $6.5 My code: fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 if (hours >= 24) { val rem = hours % 24 total += 15.0 * (hours/24) + (rem * 0.5) } else if (hours >= 5) { val rem = hours - 5 total += 5.0 + (rem * 0.5) } else { total += 0.5 * hours } println(total) }

2nd Jul 2023, 1:31 PM
Dominic Dofredo
Dominic Dofredo - avatar
2 Réponses
+ 4
else part hours * 1.0 else if part hours > 5
2nd Jul 2023, 2:14 PM
A͢J
A͢J - avatar
+ 5
"The first 5 hours are billed at $1 per hour." Check your last `else` branch and compare. For example, what should be the output, when the input is 4?
2nd Jul 2023, 2:18 PM
Tibor Santa
Tibor Santa - avatar