-
Notifications
You must be signed in to change notification settings - Fork 164
/
07_Nesting.py
64 lines (46 loc) · 2.04 KB
/
07_Nesting.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
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
57
58
59
60
61
62
63
64
"""
# Nesting
https://app.codility.com/programmers/lessons/7-stacks_and_queues/nesting/
A string S consisting of N characters is called properly nested if:
S is empty;
S has the form "(U)" where U is a properly nested string;
S has the form "VW" where V and W are properly nested strings.
For example, string "(()(())())" is properly nested but string "())" isn't.
Write a function:
def solution(S)
that, given a string S consisting of N characters, returns 1 if string S is properly nested and 0 otherwise.
For example, given S = "(()(())())", the function should return 1 and given S = "())", the function should return 0, as explained above.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [0..1,000,000];
string S is made only of the characters '(' and/or ')'.
Copyright 2009–2023 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
#---------------
# Solution
This is exactly the same problem and solution as "L7_Brackets", so I just cut/pasted
that solution and it worked perfectly.
Loop over the incoming chars.
If we get an opening bracket, push the closing bracket on to a stack.
If we get a closing bracket pop one off the stack, if it doesn't match: fail.
If we get a closing bracket and the stack is empty: fail.
If the string ends and the stack is empty: success.
O(N) https://app.codility.com/demo/results/trainingFVQXB8-TZB/
"""
import unittest
def solution(S: str) -> int:
brackets = {"[": "]", "(": ")", "{": "}"}
stack = []
for char in S:
if char in brackets.keys():
stack.append(brackets[char])
elif char in brackets.values():
if len(stack) == 0 or stack.pop() != char:
return 0
return 1 if len(stack) == 0 else 0
class TestExercise(unittest.TestCase):
"""
"""
def test_examples(self):
self.assertEqual(solution("())"), 0)
self.assertEqual(solution("(()(())())"), 1)
if __name__ == "__main__":
unittest.main()