Preparing git repo for final project
This commit is contained in:
102
problems/pset7/working/test_working.py
Normal file
102
problems/pset7/working/test_working.py
Normal file
@@ -0,0 +1,102 @@
|
||||
import pytest
|
||||
|
||||
from working import convert
|
||||
|
||||
|
||||
def main():
|
||||
test_missing_to()
|
||||
test_out_of_range_times()
|
||||
test_exception()
|
||||
test_hours_off_by_one()
|
||||
|
||||
|
||||
def test_missing_to():
|
||||
try:
|
||||
convert('09:00 AM - 05:00 PM')
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
raise ValueError("Missing to")
|
||||
try:
|
||||
convert('09:00 AM 05:00 PM')
|
||||
except Exception as e:
|
||||
assert isinstance(e, ValueError)
|
||||
|
||||
|
||||
def test_out_of_range_times():
|
||||
try:
|
||||
convert('09:00 AM to 05:61 PM')
|
||||
except Exception as e:
|
||||
assert isinstance(e, ValueError)
|
||||
try:
|
||||
convert('31:00 AM to 05:00 PM')
|
||||
except Exception as e:
|
||||
assert isinstance(e, ValueError)
|
||||
try:
|
||||
convert('13:00 PM to 8:00 AM')
|
||||
except Exception as e:
|
||||
assert isinstance(e, ValueError)
|
||||
try:
|
||||
convert('13:00PM to 8:00 AM')
|
||||
except Exception as e:
|
||||
assert isinstance(e, ValueError)
|
||||
try:
|
||||
convert('13:00 to 8:00')
|
||||
except Exception as e:
|
||||
assert isinstance(e, ValueError)
|
||||
try:
|
||||
convert("09:00 AM - 17:00 PM")
|
||||
except Exception as e:
|
||||
assert isinstance(e, ValueError)
|
||||
try:
|
||||
convert("9:00 AM 5:00 PM")
|
||||
except Exception as e:
|
||||
assert isinstance(e, ValueError)
|
||||
try:
|
||||
convert("9:60 AM to 5:60 PM")
|
||||
except Exception as e:
|
||||
assert isinstance(e, ValueError)
|
||||
try:
|
||||
convert("9 AM - 5 PM")
|
||||
except Exception as e:
|
||||
assert isinstance(e, ValueError)
|
||||
try:
|
||||
convert("9 AM5 PM")
|
||||
except Exception as e:
|
||||
assert isinstance(e, ValueError)
|
||||
try:
|
||||
convert("9 AM 5 PM")
|
||||
except Exception as e:
|
||||
assert isinstance(e, ValueError)
|
||||
with pytest.raises(ValueError):
|
||||
convert("9:72 to 6:30")
|
||||
|
||||
|
||||
def test_exception():
|
||||
with pytest.raises(ValueError):
|
||||
convert("09:00 AM - 17:00 PM")
|
||||
with pytest.raises(ValueError):
|
||||
convert("9:00 AM 5:00 PM")
|
||||
with pytest.raises(ValueError):
|
||||
convert("9:60 AM to 5:60 PM")
|
||||
with pytest.raises(ValueError):
|
||||
convert("9 AM - 5 PM")
|
||||
with pytest.raises(ValueError):
|
||||
convert("9 AM5 PM")
|
||||
with pytest.raises(ValueError):
|
||||
convert("9 AM 5 PM")
|
||||
with pytest.raises(ValueError):
|
||||
convert("9:72 to 6:30")
|
||||
|
||||
|
||||
def test_hours_off_by_one():
|
||||
assert convert('12:00 AM to 12:00 PM') == "00:00 to 12:00"
|
||||
assert convert("9 AM to 5 PM") == "09:00 to 17:00"
|
||||
assert convert("8 PM to 8 AM") == "20:00 to 08:00"
|
||||
assert convert("8:00 PM to 8:00 AM") == "20:00 to 08:00"
|
||||
assert convert("12 AM to 12 PM") == "00:00 to 12:00"
|
||||
assert convert("12:00 AM to 12:00 PM") == "00:00 to 12:00"
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
39
problems/pset7/working/working.py
Normal file
39
problems/pset7/working/working.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import re
|
||||
|
||||
|
||||
american_time_regex = re.compile(r"^\s*(\d+)(?:\:(\d{2}))? ([AP])M to (\d+)(?:\:(\d{2}))? ([AP])\s*M$")
|
||||
|
||||
|
||||
def main():
|
||||
print(convert(input("Hours: ")))
|
||||
|
||||
|
||||
def convert(s):
|
||||
time = american_time_regex.match(s)
|
||||
if time is None:
|
||||
raise ValueError("Invalid input time")
|
||||
if len(time.groups()) == 4:
|
||||
start_h, start_ampm, end_h, end_ampm = time.groups()
|
||||
start_m, end_m = 0, 0
|
||||
else:
|
||||
start_h, start_m, start_ampm, end_h, end_m, end_ampm = time.groups()
|
||||
start_m, end_m = map(lambda x: int(x) if x else 0, (start_m, end_m))
|
||||
start_h, end_h = map(int, (start_h, end_h))
|
||||
if start_h > 12 or end_h > 12:
|
||||
raise ValueError
|
||||
if (start_ampm == 'P' and start_h != 12) or (start_ampm == 'A' and start_h == 12):
|
||||
start_h += 12
|
||||
if (end_ampm == 'P' and end_h != 12) or (end_ampm == 'A' and end_h == 12):
|
||||
end_h += 12
|
||||
if start_h > 24 or end_h > 24 or start_m > 59 or end_m > 59:
|
||||
raise ValueError
|
||||
start_h = start_h % 24
|
||||
end_h = end_h % 24
|
||||
return f"{start_h:0>2}:{start_m:0>2} to {end_h:0>2}:{end_m:0>2}"
|
||||
|
||||
|
||||
...
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user