Skip to content

Commit 484a389

Browse files
committed
Code challenge 13
1 parent 7cc64e1 commit 484a389

File tree

6 files changed

+155
-1
lines changed

6 files changed

+155
-1
lines changed

Diff for: README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@
2020

2121
[PseudoQueue](python/pseudo_queue/README.md)
2222

23-
[Animal Shelter](python/stack_queue_animal_shelter/README.md)
23+
[Animal Shelter](python/stack_queue_animal_shelter/README.md)
24+
25+
[Stack Queue Brackets](python/stack_queue_brackets/README.md)

Diff for: python/stack_queue_brackets/README.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Stack + Queue Brackets
2+
<!-- Description of the challenge -->
3+
Write a function called validate brackets
4+
**Arguments**: string
5+
**Return**: boolean
6+
- representing whether or not the brackets in the string are balanced
7+
8+
_There are 3 types of brackets:_
9+
10+
- Round Brackets : ()
11+
- Square Brackets : []
12+
- Curly Brackets : {}
13+
14+
- _This will use code from [Stack](python/stacks_and_queues/stack.py)_
15+
16+
## Approach & Efficiency
17+
<!-- What approach did you take? Why? What is the Big O space/time for this approach? -->
18+
19+
I enumerated the string characters so that the node being pushed to a stack would contain the data and the index of the character. Essentially, the logic is to push to the stack for an opening bracket and then pop for a closing bracket. Then, I used conditional logic to make sure that the index of the closing bracket was not before the opening bracket, the stack is not empty when trying to pop from it upon coming across a closing bracket, and making sure that the brackets match the appropriate bracket type.
20+
21+
22+
**Time: O(1)**
23+
- because only deals with one node at a time for all of these methods, since the nodes being accessed are only at the top of the stack.
24+
- However, this method overall may use also 0(n^2), because I am calling on two iterative methods of a dictionary to access the keys and values.
25+
26+
**Space: O(n)**
27+
- For the space of the character string list and the space of the Stack individually, O(2n) for both of those combined in memory.
28+
29+
## Solution
30+
<!-- Show how to run your code, and examples of it in action -->
31+
32+
Terminal command for general main module: python3 -m stack_queue_brackets.stack_queue_brackets
33+
34+
Terminal command for testing in pytest: python3 -m pytest stack_queue_brackets/
35+
36+
![Test code in main module: collected 9 items
37+
stack_queue_brackets/stack_queue_brackets.py ..... 100%, 9 passed in 0.01s](python/stack_queue_brackets/tests_passing.png)
38+
39+
### Code Links
40+
41+
[Stack Queue Brackets](python/stack_queue_brackets/stack_queue_brackets.py)
42+
43+
[Tests of Stack Queue Brackets](python/stack_queue_brackets/test_stack_queue_brackets.py)

Diff for: python/stack_queue_brackets/__init__.py

Whitespace-only changes.

Diff for: python/stack_queue_brackets/stack_queue_brackets.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from stacks_and_queues.stack import Stack
2+
3+
4+
class Node3:
5+
def __init__(self, data, index):
6+
self.data = data
7+
self.index = index
8+
self.next = None
9+
10+
11+
def multi_bracket_validation(validation_input: str) -> bool:
12+
stack = Stack()
13+
14+
pairs = {
15+
'[': ']',
16+
'{': '}',
17+
'(': ')'
18+
}
19+
20+
for index, char in enumerate(validation_input):
21+
if char in pairs.keys():
22+
new_node = Node3(char, index)
23+
stack.push(new_node)
24+
if char in pairs.values():
25+
try:
26+
top_data = stack.peek()
27+
if index < top_data.index or not top_data:
28+
return False
29+
elif char != pairs[top_data.data]:
30+
return False
31+
else:
32+
stack.pop()
33+
except Exception as e:
34+
if str(e) == "Stack is empty":
35+
pass
36+
else:
37+
raise e
38+
39+
return stack.is_empty()
40+
41+
42+
if __name__ == '__main__':
43+
44+
print(multi_bracket_validation("{}(){}]"))
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import pytest
2+
from stack_queue_brackets.stack_queue_brackets import multi_bracket_validation
3+
4+
5+
# @pytest.mark.skip("TODO")
6+
def test_validates_two_square_brackets():
7+
actual = multi_bracket_validation("[]")
8+
expected = True
9+
assert actual == expected
10+
11+
12+
# @pytest.mark.skip("TODO")
13+
def test_fails_two_square_brackets_flipped():
14+
actual = multi_bracket_validation("][")
15+
expected = False
16+
assert actual == expected
17+
18+
19+
# @pytest.mark.skip("TODO")
20+
def test_validates_two_braces():
21+
actual = multi_bracket_validation("{}")
22+
expected = True
23+
assert actual == expected
24+
25+
26+
# @pytest.mark.skip("TODO")
27+
def test_fails_two_braces_flipped():
28+
actual = multi_bracket_validation("}{")
29+
expected = False
30+
assert actual == expected
31+
32+
33+
# @pytest.mark.skip("TODO")
34+
def test_validates_two_parentheses():
35+
actual = multi_bracket_validation("()")
36+
expected = True
37+
assert actual == expected
38+
39+
40+
# @pytest.mark.skip("TODO")
41+
def test_fails_two_parentheses_flipped():
42+
actual = multi_bracket_validation(")(")
43+
expected = False
44+
assert actual == expected
45+
46+
47+
# @pytest.mark.skip("TODO")
48+
def test_multi():
49+
actual = multi_bracket_validation("{}(){}")
50+
expected = True
51+
assert actual == expected
52+
53+
54+
# @pytest.mark.skip("TODO")
55+
def test_nested():
56+
actual = multi_bracket_validation("{([])}")
57+
expected = True
58+
assert actual == expected
59+
60+
61+
# @pytest.mark.skip("TODO")
62+
def test_mismatched():
63+
actual = multi_bracket_validation("[}")
64+
expected = False
65+
assert actual == expected

Diff for: python/stack_queue_brackets/tests_passing.png

31.6 KB
Loading

0 commit comments

Comments
 (0)