-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import sys | ||
|
||
class Jar: | ||
def __init__(self, capacity=12): | ||
if capacity > 0: | ||
self._capacity = capacity | ||
else: | ||
raise ValueError | ||
self.cookies = 0 | ||
|
||
|
||
def __str__(self): | ||
return f'{"🍪"*self.cookies}' | ||
|
||
def deposit(self, n): | ||
if n+self.cookies <= self.capacity: | ||
self.cookies += n | ||
else: | ||
raise ValueError | ||
|
||
def withdraw(self, n): | ||
if n <= self.cookies: | ||
self.cookies -= n | ||
else: | ||
raise ValueError | ||
|
||
@property | ||
def capacity(self): | ||
return self._capacity | ||
|
||
@property | ||
def size(self): | ||
return self.cookies | ||
|
||
def main(): | ||
j = Jar() | ||
|
||
print(j) | ||
|
||
if __name__=='__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from jar import Jar | ||
import pytest | ||
|
||
|
||
def test_init(): | ||
with pytest.raises(ValueError): | ||
jar = Jar(-1) | ||
|
||
|
||
def test_init_correct(): | ||
jar = Jar(10) | ||
assert str(jar) == '' | ||
|
||
|
||
def test_str(): | ||
jar = Jar() | ||
assert str(jar) == '' | ||
|
||
jar.deposit(1) | ||
assert str(jar) == '🍪' | ||
|
||
jar.deposit(11) | ||
assert str(jar) == '🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪' | ||
|
||
|
||
def test_deposit(): | ||
jar = Jar(10) | ||
|
||
jar.deposit(10) | ||
assert str(jar) == '🍪🍪🍪🍪🍪🍪🍪🍪🍪🍪' | ||
|
||
with pytest.raises(ValueError): | ||
jar.deposit(1) | ||
|
||
|
||
def test_withdraw(): | ||
jar = Jar(10) | ||
|
||
jar.deposit(8) | ||
jar.withdraw(2) | ||
assert str(jar) == '🍪🍪🍪🍪🍪🍪' | ||
|
||
with pytest.raises(ValueError): | ||
jar.deposit(8) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from datetime import date | ||
import inflect | ||
import sys | ||
import re | ||
|
||
def main(): | ||
inpt=input('Date of Birth: ') | ||
print(get_min(inpt)) | ||
|
||
|
||
def get_min(inpt): | ||
|
||
# gets dates from input using regex | ||
if search := re.search(r'^(\d{4})-(\d{2})-(\d{2})$',inpt): | ||
inpt=list(search.groups()) | ||
else: | ||
sys.exit('Invalid date') | ||
|
||
# converts str into int and checks valid date | ||
inpt_bday = convert_and_check(inpt) | ||
|
||
# today's date | ||
today = date.today() | ||
|
||
# bday | ||
bday = date(inpt_bday[0], inpt_bday[1], inpt_bday[2]) | ||
|
||
# computes no of days | ||
diff = bday - today | ||
no_of_days = -int(diff.days) | ||
|
||
# computes days into minutes | ||
minutes = no_of_days * 24 * 60 | ||
|
||
# converts into words | ||
inf = inflect.engine() | ||
min_words = inf.number_to_words(minutes) | ||
|
||
# remove 'and' and capitalize | ||
|
||
min_words = min_words.replace(' and','').capitalize() | ||
|
||
# return | ||
return min_words + ' minutes' | ||
|
||
|
||
def convert_and_check(day): | ||
# convert str to int | ||
day = list(map(int, day)) | ||
|
||
# check valid date and month | ||
if day[1] < 0 or day[1] > 12: | ||
sys.exit('Invalid date') | ||
|
||
elif day[2] < 0 or day[2] > 31: | ||
sys.exit('Invalid date') | ||
|
||
return day | ||
|
||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from seasons import get_min as f | ||
import pytest | ||
|
||
def test_input(): | ||
with pytest.raises(TypeError): | ||
f(12-12-2012) == 'Invalid date' | ||
|
||
# commented not required for submit | ||
"""'def test_inpt(): | ||
assert f(2012-13-12) == 'Invalid date' | ||
assert f(2012-12-32) == 'Invalid date' | ||
assert f(2012-32-12) == 'Invalid date' | ||
def test_format(): | ||
assert f(12-12-12) == 'Invalid date' | ||
assert f(2012-322-12) == 'Invalid date' | ||
assert f(323-1-1) == 'Invalid date'""" |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from fpdf import FPDF | ||
|
||
|
||
def main(): | ||
name = input('Name: ') | ||
|
||
# creates pdf of A4 format and set default unit to 'mm' and orientation to 'Portrait' | ||
pdf = FPDF(orientation="P", unit="mm", format="A4") | ||
pdf.add_page() | ||
|
||
# add header | ||
pdf.set_font('helvetica', 'B', 30) | ||
pdf.cell(200, 50, 'CS50 Shirtificate', new_x="LMARGIN", new_y="NEXT", align='C') | ||
|
||
# add shirt | ||
pdf.image('shirtificate.png', x= 20, y= 80, w=170) | ||
|
||
# print in shirt | ||
pdf.set_text_color(r=255, g=255, b=255) | ||
pdf.cell(200, 150, f'{name} took CS50', new_x="LMARGIN", new_y="NEXT", align='C') | ||
|
||
# save pdf | ||
pdf.output('shirtificate.pdf') | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |