Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

128 lines
4.4 KiB
Markdown

---
id: 69d03e549613fbdbe21d11e0
title: "Challenge 271: Medication Reminder"
challengeType: 29
dashedName: 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]`, where `name` is the name of the medication and `lastTaken` is 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"`.
```js
({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"`.
```js
({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"`.
```js
({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"`.
```js
({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"`.
```js
({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"`.
```js
({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--
```py
def medication_reminder(medications, current_time):
return medications
```
# --solutions--
```py
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"
```