Files
freecodecamp--freecodecamp/curriculum/challenges/english/blocks/daily-coding-challenges-python/69c5f3d787b1725d5f00c8ba.md
T
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

106 lines
2.3 KiB
Markdown

---
id: 69c5f3d787b1725d5f00c8ba
title: "Challenge 256: Closest Time Direction"
challengeType: 29
dashedName: challenge-256
---
# --description--
Given two times, determine whether you can get from the first to the second faster by moving forward or backward.
- Times are given in 24-hour format (`"HH:MM"`)
- The clock wraps around (23:59 goes to 00:00 when moving forward, and 00:00 goes to 23:59 when moving backwards)
Return:
- `"forward"` if moving forward is shorter
- `"backward"` if moving backward is shorter
- `"equal"` if both directions take the same amount of time
# --hints--
`get_direction("10:00", "12:00")` should return `"forward"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_direction("10:00", "12:00"), "forward")`)
}})
```
`get_direction("11:00", "05:00")` should return `"backward"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_direction("11:00", "05:00"), "backward")`)
}})
```
`get_direction("00:00", "12:00")` should return `"equal"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_direction("00:00", "12:00"), "equal")`)
}})
```
`get_direction("15:45", "01:10")` should return `"forward"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_direction("15:45", "01:10"), "forward")`)
}})
```
`get_direction("03:30", "19:50")` should return `"backward"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_direction("03:30", "19:50"), "backward")`)
}})
```
`get_direction("06:30", "18:30")` should return `"equal"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_direction("06:30", "18:30"), "equal")`)
}})
```
# --seed--
## --seed-contents--
```py
def get_direction(time1, time2):
return time1
```
# --solutions--
```py
def get_direction(time1, time2):
def to_minutes(t):
h, m = map(int, t.split(':'))
return h * 60 + m
start = to_minutes(time1)
end = to_minutes(time2)
forward = (end - start + 1440) % 1440
backward = (start - end + 1440) % 1440
if forward < backward:
return 'forward'
if backward < forward:
return 'backward'
return 'equal'
```