Preparing git repo for final project

This commit is contained in:
2023-07-09 11:19:26 +02:00
parent 6a38966eef
commit 63d06d6b35
67 changed files with 1587 additions and 0 deletions

26
problems/pset8/jar/jar.py Normal file
View 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

View 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()