Skip to content

Commit 1bc888f

Browse files
committed
use record ~ PUT & get_last ~ GET APIS
1 parent 79c8dfe commit 1bc888f

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

DailyCodingProblem/recordLastNOrderIDs.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,34 @@
99
than or equal to N.
1010
3. You should be as efficient with time and space as possible.
1111
12-
"""
12+
13+
Approach: Stack data structure seems like a good fit, let's try..
14+
15+
"""
16+
17+
def record(order_id):
18+
stack.append(order_id)
19+
return
20+
21+
def get_last(i):
22+
assert i <= N,"i is not less than or equal to N"
23+
ele = stack.pop(-1 * i)
24+
stack.insert(N - i,ele)
25+
return ele
26+
27+
if __name__ == '__main__':
28+
N = int(input())
29+
stack = []
30+
for n in range(N):
31+
order_id = input()
32+
record(order_id)
33+
while True:
34+
print ("\n Please enter break to get out of loop")
35+
index = input()
36+
if index == 'break':
37+
break
38+
try:
39+
index = int(index)
40+
except:
41+
raise Exception("The index you entered is not valid")
42+
print (get_last(index))

0 commit comments

Comments
 (0)