Last active
June 20, 2017 07:11
-
-
Save effective-light/9e63625b370fac616bcb3fe0efa4125a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from stack import Stack | |
def size(stk: Stack) -> int: | |
""" Return the number of items on Stack stk, *without* modifying stk. | |
(It’s OK if the contents of stk are modified during the execution of this | |
function, as long as everything is restored before the function returns.) | |
>>> st = Stack() | |
>>> st.push(3); st.push(2); st.push(1) | |
>>> size(st) | |
3 | |
>>> st.pop() | |
1 | |
""" | |
s = 0 | |
temp_stk = Stack() | |
while not stk.is_empty(): | |
temp_stk.push(stk.pop()) | |
s += 1 | |
while not temp_stk.is_empty(): | |
stk.push(temp_stk.pop()) | |
return s |
This file contains hidden or 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
def fib(n: int) -> int: | |
""" Return the nth fibonacci number. | |
Where the fibonacci numbers are defined as: 1, 1, 2, 3, 5, 8, 13 | |
(each number is the sum of he two previous numbers) | |
>>> fib(4) | |
3 | |
""" | |
return 1 if n <= 2 else fib(n - 1) + fib(n - 2) | |
This file contains hidden or 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
class BuildingCodeViolationError(Exception): | |
pass | |
class InvalidBusinessError(Exception): | |
pass | |
class BuildingCreationException(Exception): | |
pass | |
class Room: | |
def __init__(self, name: str, square_footage: float): | |
self.name = name | |
self.square_footage = square_footage | |
class Building: | |
def __init__(self, address: str, rooms: list): | |
self.address = address | |
self.rooms = rooms | |
def __repr__(self): | |
s = 0 | |
for room in self.rooms: | |
s += room.square_footage | |
return s | |
def rename_room(self, old: str, new: str): | |
for room in self.rooms: | |
if room.name == old: | |
room.name = new | |
return | |
raise BuildingCreationException() | |
class House(Building): | |
def __init__(self, addr: str, rms: list): | |
if len(rms) > 10: | |
raise BuildingCodeViolationError() | |
Building.__init__(self, addr, rms) | |
def __repr__(self): | |
print("Welcome to our house") | |
for room in self.rooms: | |
print("{:s}, {:d}".format(room.name, room.square_footage)) | |
class Business(Building): | |
def __init__(self, addr: str, rms: list): | |
for room in rms: | |
if room.square_footage < 100 \ | |
or room.name == "Bedroom": | |
raise InvalidBusinessError() | |
Building.__init__(self, addr, rms) | |
def change_square_footage(self, n, new_footage): | |
for room in self.rooms: | |
if room.name == n: | |
room.square_footage = new_footage | |
return | |
raise BuildingCreationException() | |
if __name__ == "__main__": | |
try: | |
build = input("Type of building?\n") | |
address = input("Address of building?\n") | |
num = int(input("Number of rooms?\n")) | |
rooms = [] | |
while num > 0: | |
name = input("Name of room?\n") | |
square_footage = float(input("square_footage of room {:s}?\n" | |
.format(name))) | |
rooms.append(Room(name, square_footage)) | |
building = {"building": Building(address, rooms), | |
"house": House(address, rooms), | |
"business": Business(address, rooms) | |
}.get(build.lower()) | |
except BuildingCreationException: | |
print("oops...") | |
except (BuildingCodeViolationError, InvalidBusinessError): | |
print("you can’t do that") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment