-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOOPSolution.py
87 lines (68 loc) · 2.46 KB
/
OOPSolution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from enum import Enum
from collections import defaultdict
class Bag(Enum):
SMALL = (5, 0.25)
MEDIUM = (10, 0.5)
LARGE = (15, 1)
def __init__(self, price, size):
self.price = price
self.size = size
# # Alternatively something like this
# class Bag:
# def __init__(self, name, size, price):
# self.name = name
# self.size = size
# self.price = price
class Van:
def __init__(self, id, maxCapacity):
self.id = id
self.maxCapacity = maxCapacity
self.currCapacity = 0
self.pickupCount = 0
def canFit(self, bag):
return self.currCapacity + bag.size <= self.maxCapacity
def addBag(self, bag):
self.currCapacity += bag.size
self.pickupCount += 1
def getGasCost(self):
return 4 * (0.1 * self.pickupCount * (self.currCapacity + self.maxCapacity))
class Laundry:
def __init__(self):
self.revenue = 0
self.vans = [Van("Eric's Van", 10)]
def singlePickup(self, address, bag):
# Part 2
if self.van.currCapacity + bag.size <= self.van.maxCapacity:
self.van.currCapacity += bag.size
self.revenue += bag.price
else:
raise Exception("Max capacity reached, sad.")
def pickup(self, address, bag):
# Part 3
for van in self.vans:
if van.canFit(bag):
van.addBag(bag)
self.revenue += bag.price
return
def getNetIncome(self):
gas_cost = sum(van.getGasCost() for van in self.vans)
return self.revenue - gas_cost
class LaundryCorp:
def __init__(self):
self.franchises = defaultdict(Laundry)
def addFranchise(self, name, laundry):
self.franchises[name] = laundry
def getLargestRevenueFranchise(self):
return max(self.franchises, key=lambda name: self.franchises[name].revenue, default="")
if __name__ == "__main__":
# Some tests, not necessary for actual interview
clean4impact = Laundry()
clean4impact.pickup("H4I Road", Bag.SMALL)
assert(clean4impact.revenue == 5)
assert(clean4impact.vans[0].currCapacity == 0.25)
assert(round(clean4impact.getNetIncome(), 2) == 0.9)
cornell = Laundry()
hack4impact = LaundryCorp()
hack4impact.addFranchise("UIUC", clean4impact)
hack4impact.addFranchise("Cornell", cornell)
assert(hack4impact.getLargestRevenueFranchise() == "UIUC")