Preparing git repo for final project
This commit is contained in:
39
problems/pset8/seasons/seasons.py
Normal file
39
problems/pset8/seasons/seasons.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import datetime
|
||||
import re
|
||||
import sys
|
||||
|
||||
import inflect
|
||||
|
||||
date_regex = re.compile(r"\d{4}-\d{2}-\d{2}")
|
||||
p = inflect.engine()
|
||||
|
||||
|
||||
def main():
|
||||
today = datetime.date.today()
|
||||
users_date = input("Date of Birth: ")
|
||||
try:
|
||||
users_date = parse_date(users_date)
|
||||
except ValueError:
|
||||
sys.exit(1)
|
||||
minutes = get_minutes(users_date, today)
|
||||
print(format_minutes(minutes))
|
||||
|
||||
|
||||
def get_minutes(start_date, end_date):
|
||||
minutes = (end_date - start_date).total_seconds() / 60
|
||||
return int(round(minutes, 0))
|
||||
|
||||
|
||||
def format_minutes(minutes: int) -> str:
|
||||
stringed_number = p.number_to_words(minutes, andword="").capitalize()
|
||||
return f"{stringed_number} minute{'s' if minutes > 1 else ''}"
|
||||
|
||||
|
||||
def parse_date(date_string):
|
||||
if not date_regex.match(date_string): # Acutally not necessary: strptime would raise ValueError anyway
|
||||
raise ValueError
|
||||
return datetime.datetime.strptime(date_string, '%Y-%m-%d').date()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
44
problems/pset8/seasons/test_seasons.py
Normal file
44
problems/pset8/seasons/test_seasons.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from seasons import format_minutes, parse_date
|
||||
|
||||
|
||||
def main():
|
||||
test_format_minutes()
|
||||
test_invalid_dates()
|
||||
test_known_intervals()
|
||||
|
||||
|
||||
def test_format_minutes():
|
||||
assert format_minutes(1) == 'One minute'
|
||||
assert format_minutes(2) == 'Two minutes'
|
||||
|
||||
|
||||
def test_invalid_dates():
|
||||
try:
|
||||
parse_date('91-5-9')
|
||||
raise Exception
|
||||
except Exception as e:
|
||||
assert isinstance(e, ValueError)
|
||||
try:
|
||||
parse_date('cacao')
|
||||
raise Exception
|
||||
except Exception as e:
|
||||
assert isinstance(e, ValueError)
|
||||
try:
|
||||
parse_date('1991-13-09')
|
||||
raise Exception
|
||||
except Exception as e:
|
||||
assert isinstance(e, ValueError)
|
||||
try:
|
||||
parse_date('1991-11-40')
|
||||
raise Exception
|
||||
except Exception as e:
|
||||
assert isinstance(e, ValueError)
|
||||
|
||||
|
||||
def test_known_intervals():
|
||||
assert format_minutes(525600) == "Five hundred twenty-five thousand, six hundred minutes"
|
||||
assert format_minutes(1051200) == "One million, fifty-one thousand, two hundred minutes"
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Reference in New Issue
Block a user