We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 79c8dfe commit 1bc888fCopy full SHA for 1bc888f
1 file changed
DailyCodingProblem/recordLastNOrderIDs.py
@@ -9,4 +9,34 @@
9
than or equal to N.
10
3. You should be as efficient with time and space as possible.
11
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