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
116 lines
2.8 KiB
Markdown
116 lines
2.8 KiB
Markdown
---
|
|
id: 6a2037a68a0bc2aef0006003
|
|
title: "Challenge 341: Birthday Countdown"
|
|
challengeType: 29
|
|
dashedName: challenge-341
|
|
---
|
|
|
|
# --description--
|
|
|
|
Given today's date and a birthday, return the number of days until the person's next birthday.
|
|
|
|
- Today's date is given as a string in `"YYYY-MM-DD"` format, with leading zeros, for example: `"2026-07-16"`.
|
|
- The birthday is given as a string in `"M/D"` format, without leading zeros, for example: `"9/7"`.
|
|
- If today is their birthday, return the number of days until their next birthday (not `0`).
|
|
- Leap years should be accounted for.
|
|
|
|
# --hints--
|
|
|
|
`days_until_birthday("2026-07-16", "9/7")` should return `53`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(days_until_birthday("2026-07-16", "9/7"), 53)`)
|
|
}})
|
|
```
|
|
|
|
`days_until_birthday("2026-07-16", "3/22")` should return `249`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(days_until_birthday("2026-07-16", "3/22"), 249)`)
|
|
}})
|
|
```
|
|
|
|
`days_until_birthday("2026-07-16", "7/16")` should return `365`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(days_until_birthday("2026-07-16", "7/16"), 365)`)
|
|
}})
|
|
```
|
|
|
|
`days_until_birthday("2024-02-28", "3/1")` should return `2`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(days_until_birthday("2024-02-28", "3/1"), 2)`)
|
|
}})
|
|
```
|
|
|
|
`days_until_birthday("2023-04-24", "12/30")` should return `250`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(days_until_birthday("2023-04-24", "12/30"), 250)`)
|
|
}})
|
|
```
|
|
|
|
`days_until_birthday("2024-03-01", "2/29")` should return `1460`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(days_until_birthday("2024-03-01", "2/29"), 1460)`)
|
|
}})
|
|
```
|
|
|
|
`days_until_birthday("2096-03-01", "2/29")` should return `2920`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(days_until_birthday("2096-03-01", "2/29"), 2920)`)
|
|
}})
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```py
|
|
def days_until_birthday(today, birthday):
|
|
|
|
return today
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```py
|
|
def days_until_birthday(today, birthday):
|
|
from datetime import date
|
|
|
|
year, month, day = [int(x) for x in today.split("-")]
|
|
b_month, b_day = [int(x) for x in birthday.split("/")]
|
|
|
|
today_date = date(year, month, day)
|
|
|
|
def is_leap_year(y):
|
|
return (y % 4 == 0 and y % 100 != 0) or y % 400 == 0
|
|
|
|
def next_valid_birthday(from_year):
|
|
for y in range(from_year, from_year + 9):
|
|
if b_month == 2 and b_day == 29 and not is_leap_year(y):
|
|
continue
|
|
d = date(y, b_month, b_day)
|
|
if d > today_date:
|
|
return d
|
|
|
|
return (next_valid_birthday(year) - today_date).days
|
|
```
|