Skip to content

Commit 3db5fa8

Browse files
authored
Create apply-discount-every-n-orders.py
1 parent e73ebd3 commit 3db5fa8

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Time: ctor: O(m), m is the number of all products
2+
# getBill: O(p), p is the number of products to bill
3+
# Space: O(m)
4+
5+
class Cashier(object):
6+
7+
def __init__(self, n, discount, products, prices):
8+
"""
9+
:type n: int
10+
:type discount: int
11+
:type products: List[int]
12+
:type prices: List[int]
13+
"""
14+
self.__n = n
15+
self.__discount = discount
16+
self.__curr = 0
17+
self.__lookup = {p : prices[i] for i, p in enumerate(products)}
18+
19+
def getBill(self, product, amount):
20+
"""
21+
:type product: List[int]
22+
:type amount: List[int]
23+
:rtype: float
24+
"""
25+
self.__curr = (self.__curr+1) % self.__n
26+
result = 0.0
27+
for i, p in enumerate(product):
28+
result += self.__lookup[p]*amount[i]
29+
return result * (1.0 - self.__discount/100.0 if self.__curr == 0 else 1.0)

0 commit comments

Comments
 (0)