curriculum/challenges/english/blocks/daily-coding-challenges-python/699c8e045ee7cb94ed2322d6.md
Given two strings representing the time you parked your car and the time you picked it up, calculate the parking fee.
"HH:MM" using a 24-hour clock. So "14:00" is 2pm for example.Fee rules:
Return the total cost in the format "$cost", "$5" for example.
calculate_parking_fee("09:00", "11:00") should return "$6".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(calculate_parking_fee("09:00", "11:00"), "$6")`)
}})
calculate_parking_fee("10:00", "10:30") should return "$5".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(calculate_parking_fee("10:00", "10:30"), "$5")`)
}})
calculate_parking_fee("08:10", "10:45") should return "$9".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(calculate_parking_fee("08:10", "10:45"), "$9")`)
}})
calculate_parking_fee("14:40", "23:10") should return "$27".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(calculate_parking_fee("14:40", "23:10"), "$27")`)
}})
calculate_parking_fee("18:15", "01:30") should return "$34".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(calculate_parking_fee("18:15", "01:30"), "$34")`)
}})
calculate_parking_fee("11:11", "11:10") should return "$82".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(calculate_parking_fee("11:11", "11:10"), "$82")`)
}})
def calculate_parking_fee(park_time, pickup_time):
return park_time
import math
def calculate_parking_fee(park_time, pickup_time):
def to_minutes(time_str):
hours, minutes = map(int, time_str.split(":"))
return hours * 60 + minutes
entry_minutes = to_minutes(park_time)
exit_minutes = to_minutes(pickup_time)
overnight = False
if exit_minutes < entry_minutes:
overnight = True
total_minutes = (24 * 60 - entry_minutes) + exit_minutes
else:
total_minutes = exit_minutes - entry_minutes
hours = math.ceil(total_minutes / 60)
cost = hours * 3
if overnight:
cost += 10
if cost < 5:
cost = 5
return f"${cost}"