Skip to content

Commit f9be4df

Browse files
committed
pset7 q3 code
1 parent 435106b commit f9be4df

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

week7/working/test_working.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
from working import convert
3+
4+
def test_validate_correct():
5+
assert convert("9 AM to 5 PM") == "09:00 to 17:00"
6+
assert convert("9:00 AM to 5:30 PM") == "09:00 to 17:30"
7+
assert convert("9:00 PM to 5:30 AM") == "21:00 to 05:30"
8+
9+
def test_value_error_to():
10+
with pytest.raises(ValueError):
11+
assert convert("9:00 AM lol 5:30 PM")
12+
13+
def test_value_error_range():
14+
with pytest.raises(ValueError):
15+
assert convert("13:00 AM to 69:30 PM")

week7/working/working.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import re
2+
3+
4+
def main():
5+
print(convert(input("Hours: ")))
6+
7+
8+
def convert(s):
9+
if x := re.search(r"^(1?\d)(:[0-5]\d)? (AM|PM) to (1?\d)(:[0-5]\d)? (AM|PM)$", s):
10+
if x.group(3) == "AM" and x.group(6) == "PM":
11+
if 0 < int(x.group(1)) < 13 and 0 < int(x.group(4)) < 13:
12+
p = x.group(2)
13+
q = x.group(5)
14+
if not p: p = ":00"
15+
if not q: q = ":00"
16+
if (x.group(1) and x.group(4)) == "12":
17+
return f"00{p} to {x.group(4)}{q}"
18+
return f"{x.group(1).zfill(2)}{p} to {int(x.group(4))+12}{q}"
19+
pass
20+
elif x.group(3) == "PM" and x.group(6) == "AM":
21+
if 0 < int(x.group(1)) < 13 and 0 < int(x.group(4)) < 13:
22+
p = x.group(2)
23+
q = x.group(5)
24+
if not p: p = ":00"
25+
if not q: q = ":00"
26+
return f"{int(x.group(1))+12}{p} to {x.group(4).zfill(2)}{q}"
27+
pass
28+
raise ValueError("lol")
29+
else:
30+
raise ValueError("lol")
31+
32+
33+
if __name__ == "__main__":
34+
main()

0 commit comments

Comments
 (0)