-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacks.py
More file actions
56 lines (51 loc) · 1.79 KB
/
Copy pathstacks.py
File metadata and controls
56 lines (51 loc) · 1.79 KB
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
# Fish STACK EXERCISE - solution for https://app.codility.com/programmers/lessons/7-stacks_and_queues/fish/
def solution(A:list[int], B:list[int]):
stack = []
for i in range(len(A)):
if i == 0 or B[i] == 1:
stack.append((A[i], B[i]))
else:
while stack and B[i] == 0 and stack[-1][1] == 1:
if stack[-1][0] <= A[i]:
stack.pop()
else:
break
else:
stack.append((A[i], B[i]))
return len(stack)
# StoneWall STACK EXERCISE - solution for https://app.codility.com/programmers/lessons/7-stacks_and_queues/stone_wall/
def solution(H:list[int]):
stack = []
res = 0
for i in range(len(H)):
if i == 0:
stack.append(H[i])
elif stack and H[i] > stack[-1]:
stack.append(H[i])
else:
while stack:
if stack[-1] == H[i]:
break
elif stack[-1] > H[i]:
stack.pop()
res += 1
else:
stack.append(H[i])
break
else:
stack.append(H[i])
return len(stack) + res
# Brackets STACK EXERCISE - solution for https://app.codility.com/programmers/lessons/7-stacks_and_queues/brackets/ and https://app.codility.com/programmers/lessons/7-stacks_and_queues/nesting/
def solution(S:str):
if S == "":
return 1
stack = []
for c in S:
if c in ["(", "{", "["]:
stack.append(c)
else:
if stack and ((c == ")" and stack[-1] == "(") or (c == "}" and stack[-1] == "{") or (c == "]" and stack[-1] == "[")):
stack.pop()
else:
return 0
return 1 if not stack else 0