Preparing git repo for final project
This commit is contained in:
26
problems/pset8/jar/jar.py
Normal file
26
problems/pset8/jar/jar.py
Normal file
@@ -0,0 +1,26 @@
|
||||
class Jar:
|
||||
def __init__(self, capacity=12):
|
||||
if not isinstance(capacity, int) or capacity < 0:
|
||||
raise ValueError("Invalid capacity")
|
||||
self._capacity = capacity
|
||||
self._size = 0
|
||||
|
||||
def __str__(self):
|
||||
return '🍪' * self.size
|
||||
|
||||
def deposit(self, n):
|
||||
new_size = n + self.size
|
||||
if new_size > self.capacity or new_size < 0:
|
||||
raise ValueError("Exceeded capacity")
|
||||
self._size += n
|
||||
|
||||
def withdraw(self, n):
|
||||
return self.deposit(-n)
|
||||
|
||||
@property
|
||||
def capacity(self):
|
||||
return self._capacity
|
||||
|
||||
@property
|
||||
def size(self):
|
||||
return self._size
|
46
problems/pset8/jar/test_jar.py
Normal file
46
problems/pset8/jar/test_jar.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from jar import Jar
|
||||
|
||||
|
||||
def main():
|
||||
test_capacity()
|
||||
test_size()
|
||||
test_deposit()
|
||||
test_withdraw()
|
||||
|
||||
|
||||
def test_capacity():
|
||||
j = Jar(capacity=3)
|
||||
assert j.capacity == 3
|
||||
|
||||
|
||||
def test_size():
|
||||
j = Jar(capacity=5)
|
||||
assert j.size == 0
|
||||
j.deposit(3)
|
||||
j.withdraw(1)
|
||||
assert j.size == 2
|
||||
|
||||
|
||||
def test_deposit():
|
||||
j = Jar(capacity=5)
|
||||
j.deposit(3)
|
||||
try:
|
||||
j.deposit(3)
|
||||
raise Exception("Deposit n > capacity did not raise ValueError")
|
||||
except Exception as e:
|
||||
assert isinstance(e, ValueError)
|
||||
|
||||
|
||||
def test_withdraw():
|
||||
j = Jar(capacity=5)
|
||||
j.deposit(3)
|
||||
j.withdraw(2)
|
||||
try:
|
||||
j.withdraw(2)
|
||||
raise Exception("Withdraw n > size did not raise ValueError")
|
||||
except Exception as e:
|
||||
assert isinstance(e, ValueError)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
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()
|
BIN
problems/pset8/shirtificate/shirtificate.pdf
Normal file
BIN
problems/pset8/shirtificate/shirtificate.pdf
Normal file
Binary file not shown.
BIN
problems/pset8/shirtificate/shirtificate.png
Normal file
BIN
problems/pset8/shirtificate/shirtificate.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 60 KiB |
22
problems/pset8/shirtificate/shirtificate.py
Normal file
22
problems/pset8/shirtificate/shirtificate.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from fpdf import FPDF
|
||||
|
||||
|
||||
def main():
|
||||
username = input("What's your name?\t\t")
|
||||
pdf = FPDF(orientation="P", unit="mm", format="A4")
|
||||
pdf.set_auto_page_break(False)
|
||||
pdf.add_page()
|
||||
pdf.set_font("helvetica", "B", 45)
|
||||
pdf.cell(0, 10, 'CS50 Shirtificate', new_x="LMARGIN", new_y="NEXT", align='C')
|
||||
page_width = pdf.w
|
||||
total_horizontal_margin = 0.2 * page_width
|
||||
image_width = pdf.w-total_horizontal_margin
|
||||
pdf.image("shirtificate.png", x=total_horizontal_margin/2, y=50, w=image_width)
|
||||
pdf.set_font("helvetica", "B", 32)
|
||||
pdf.set_text_color(255, 255, 255)
|
||||
pdf.cell(0, 230, f'{username} took CS50', new_x="LMARGIN", new_y="NEXT", align='C')
|
||||
pdf.output("shirtificate.pdf")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Reference in New Issue
Block a user