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

114 lines
2.7 KiB
Markdown

---
id: 6a26df3e2988bcdded204896
title: "Challenge 360: Spoken Duration"
challengeType: 29
dashedName: challenge-360
---
# --description--
Given a number of seconds, return the duration in spoken English.
- Break the duration into hours, minutes, and seconds.
- Skip any zero values.
- Use singular or plural as appropriate (`"1 hour"`, `"2 hours"`).
- If present, join the last two units with `"and"`, and the second and third to last units with a comma (`"1 hour, 2 minutes and 3 seconds"`).
# --hints--
`get_spoken_duration(3723)` should return `"1 hour, 2 minutes and 3 seconds"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_spoken_duration(3723), "1 hour, 2 minutes and 3 seconds")`)
}})
```
`get_spoken_duration(7295)` should return `"2 hours, 1 minute and 35 seconds"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_spoken_duration(7295), "2 hours, 1 minute and 35 seconds")`)
}})
```
`get_spoken_duration(8521)` should return `"2 hours, 22 minutes and 1 second"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_spoken_duration(8521), "2 hours, 22 minutes and 1 second")`)
}})
```
`get_spoken_duration(435)` should return `"7 minutes and 15 seconds"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_spoken_duration(435), "7 minutes and 15 seconds")`)
}})
```
`get_spoken_duration(14455)` should return `"4 hours and 55 seconds"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_spoken_duration(14455), "4 hours and 55 seconds")`)
}})
```
`get_spoken_duration(72000)` should return `"20 hours"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_spoken_duration(72000), "20 hours")`)
}})
```
`get_spoken_duration(1)` should return `"1 second"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_spoken_duration(1), "1 second")`)
}})
```
# --seed--
## --seed-contents--
```py
def get_spoken_duration(seconds):
return seconds
```
# --solutions--
```py
def get_spoken_duration(seconds):
hours = seconds // 3600
minutes = (seconds % 3600) // 60
secs = seconds % 60
parts = []
if hours:
parts.append(f"{hours} {'hour' if hours == 1 else 'hours'}")
if minutes:
parts.append(f"{minutes} {'minute' if minutes == 1 else 'minutes'}")
if secs:
parts.append(f"{secs} {'second' if secs == 1 else 'seconds'}")
if len(parts) == 1:
return parts[0]
if len(parts) == 2:
return f"{parts[0]} and {parts[1]}"
return f"{parts[0]}, {parts[1]} and {parts[2]}"
```