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

93 lines
2.0 KiB
Markdown

---
id: 698a1a73ade5ac0e19180fa1
title: "Challenge 198: Business Day Count"
challengeType: 29
dashedName: challenge-198
---
# --description--
Given a start date and an end date, return the number of business days between the two.
- Given dates are in the format `"YYYY-MM-DD"`.
- Weekdays are business days (Monday through Friday).
- Weekends are not business days (Saturday and Sunday).
- Include both the start and end dates when counting.
# --hints--
`count_business_days("2026-02-24", "2026-02-26")` should return `3`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count_business_days("2026-02-24", "2026-02-26"), 3)`)
}})
```
`count_business_days("2026-02-24", "2026-02-28")` should return `4`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count_business_days("2026-02-24", "2026-02-28"), 4)`)
}})
```
`count_business_days("2026-02-21", "2026-03-01")` should return `5`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count_business_days("2026-02-21", "2026-03-01"), 5)`)
}})
```
`count_business_days("2026-03-08", "2026-03-17")` should return `7`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count_business_days("2026-03-08", "2026-03-17"), 7)`)
}})
```
`count_business_days("2026-02-24", "2027-02-24")` should return `262`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(count_business_days("2026-02-24", "2027-02-24"), 262)`)
}})
```
# --seed--
## --seed-contents--
```py
def count_business_days(start, end):
return start
```
# --solutions--
```py
from datetime import datetime, timedelta
def count_business_days(start, end):
start_date = datetime.strptime(start, "%Y-%m-%d")
end_date = datetime.strptime(end, "%Y-%m-%d")
count = 0
current = start_date
while current <= end_date:
if current.weekday() < 5:
count += 1
current += timedelta(days=1)
return count
```