PY
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
#This code is updated. Old code can be found in comments.
from collections import deque
"""
bigFromSide will return big element (val)
from either side of deque.
If deque has only one element remove and return it.
Else compare first element with last one.(without removing them)
Only the element which is great will be removed and returned.
"""
def bigFromSide(dq):
if(len(dq)==1):
return dq.pop()
elif dq[0]>dq[-1]:
return dq.popleft()
else:
return dq.pop()
"""
canStack checks whether a deque can be stacked.
preVal = is initialized to Infinity so that its value is very high.
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run