4.4 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 69d03e549613fbdbe21d11e0 | Challenge 271: Medication Reminder | 29 | challenge-271 |
--description--
Given an array of medications and a string representing the current time, return the next medication you need to take and how long until you need to take it.
- Each medication is in the format
[name, lastTaken], wherenameis the name of the medication andlastTakenis the time it was last taken. - All given times will be in
"HH:MM"(24-hour) format.
Use the following medication schedule:
| Name | Schedule |
|---|---|
| Deployxitrin | 08:00, 16:00 |
| Debuggamanizole | 07:00, 13:00, 21:00 |
| Mergeflictamine | every 4 hours |
Return a string in the format "{name} in Hh Mm". For example, "Debuggamanizole in 2h 0m" or "Deployxitrin in 1h 5m".
--hints--
medication_reminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "07:00"], ["Mergeflictamine", "10:00"]], "11:00") should return "Debuggamanizole in 2h 0m".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(medication_reminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "07:00"], ["Mergeflictamine", "10:00"]], "11:00"), "Debuggamanizole in 2h 0m")`)
}})
medication_reminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "13:00"], ["Mergeflictamine", "14:00"]], "14:55") should return "Deployxitrin in 1h 5m".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(medication_reminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "13:00"], ["Mergeflictamine", "14:00"]], "14:55"), "Deployxitrin in 1h 5m")`)
}})
medication_reminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "13:00"], ["Mergeflictamine", "14:00"]], "17:15") should return "Mergeflictamine in 0h 45m".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(medication_reminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "13:00"], ["Mergeflictamine", "14:00"]], "17:15"), "Mergeflictamine in 0h 45m")`)
}})
medication_reminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "07:00"], ["Mergeflictamine", "09:00"]], "12:59") should return "Debuggamanizole in 0h 1m".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(medication_reminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "07:00"], ["Mergeflictamine", "09:00"]], "12:59"), "Debuggamanizole in 0h 1m")`)
}})
medication_reminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "21:00"], ["Mergeflictamine", "03:00"]], "06:55") should return "Debuggamanizole in 0h 5m".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(medication_reminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "21:00"], ["Mergeflictamine", "03:00"]], "06:55"), "Debuggamanizole in 0h 5m")`)
}})
medication_reminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "07:00"], ["Mergeflictamine", "07:30"]], "08:00") should return "Mergeflictamine in 3h 30m".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(medication_reminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "07:00"], ["Mergeflictamine", "07:30"]], "08:00"), "Mergeflictamine in 3h 30m")`)
}})
--seed--
--seed-contents--
def medication_reminder(medications, current_time):
return medications
--solutions--
def medication_reminder(medications, current_time):
schedule = {
'Deployxitrin': {'type': 'fixed', 'times': ['08:00', '16:00']},
'Debuggamanizole': {'type': 'fixed', 'times': ['07:00', '13:00', '21:00']},
'Mergeflictamine': {'type': 'interval', 'hours': 4},
}
def to_minutes(t):
h, m = map(int, t.split(':'))
return h * 60 + m
current = to_minutes(current_time)
earliest = float('inf')
next_med = ''
for name, last_taken in medications:
med = schedule[name]
if med['type'] == 'fixed':
future_times = [to_minutes(t) for t in med['times'] if to_minutes(t) > current]
if not future_times:
continue
next_due = min(future_times)
else:
next_due = to_minutes(last_taken) + med['hours'] * 60
diff = next_due - current
if 0 < diff < earliest:
earliest = diff
next_med = name
h = earliest // 60
m = earliest % 60
return f"{next_med} in {h}h {m}m"