Skip to content

Commit 9468d74

Browse files
author
Steven Landau
committed
added challenge 10
1 parent d1f02b0 commit 9468d74

File tree

8 files changed

+120
-0
lines changed

8 files changed

+120
-0
lines changed

challenge_10/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Valid Closers
2+
=======
3+
Idea
4+
----
5+
This problem is a classic. Given a string that contains various amounts of { [ or (, your job is to determine if each of these are closed successfully. Every { must have a }, every [ must have a ], and every ( must have a ). Any characters can appear between any of these. If the string does have valid closers, the program should return True, otherwise it should return false.
6+
7+
Your solution should use a maximum of O(N) space and run in O(N) time (max).
8+
9+
What is the best data structure for this problem?
10+
11+
Please make your program so that it takes input from standard input. System.in for you java folks and input() for you pythonistas.
12+
Testing
13+
------
14+
Instead of describing each test case, I'm just going to list them out here since I'm sure you can imagine what they might be.
15+
16+
{{{{{{{{{adfkjaefia}}}}}}} should return False
17+
18+
{{{{{{{{{[[[[[[kadfa{{{{{{{((({daljfdaf({{{[]}}kaldjfs})})))}}}}}}}]]]]]]}kjfela}}}}}}}} Should return True
19+
20+
{{{[}}}}dafda Should return False
21+
22+
{{{{{{{{{}}}}}}}}} Should return True
23+
24+
[[[[[[[[[kafjalfeianfailfeja;fjai;efa;sfj]]]]]]]]]kjajdain Should return True
25+
26+
< blank > should return True
27+
28+
((((((fjdalfeja((((alefjalisj(())))))))))))d Should return True
29+
30+
)))(((d Should return False
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python3
2+
# @author slandau3
3+
4+
5+
def valid_closers(string_with_brackets_in_it_or_maybe_not_idk_i_coudnt_think_of_a_name: str) -> bool:
6+
stack = [] # initialize an empty list (which can easily be used as a stack in python)
7+
8+
closers = [']', ')', '}']
9+
openers = ['{', '(', '[']
10+
for char in string_with_brackets_in_it_or_maybe_not_idk_i_coudnt_think_of_a_name:
11+
if len(stack) == 0 and char in closers:
12+
# This if statement will return false if there is no open bracket to match the closing bracket.
13+
# More specifically it returns false if the list is null and we have recieved a closer
14+
return False
15+
16+
if char in openers:
17+
stack.append(char)
18+
19+
elif char == '}':
20+
if stack.pop() != '{':
21+
return False
22+
23+
elif char == ']':
24+
if stack.pop() != '[':
25+
return False
26+
27+
elif char == ')':
28+
if stack.pop() != '(':
29+
return False
30+
31+
return True if len(stack) == 0 else False
32+
33+
34+
if __name__ == '__main__':
35+
print(valid_closers('{{{{{{{{{[[[[[[kadfa{{{{{{{((({daljfdaf({{{[]}}kaldjfs})})))}}}}}}}]]]]]]}kjfela}}}}}}}}'))

challenge_10/python/slandau3/tests.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python3
2+
# @author slandau3
3+
4+
import unittest
5+
6+
from closers import valid_closers
7+
8+
class Test(unittest.TestCase):
9+
10+
def test1(self):
11+
self.assertEqual(valid_closers('{{{{{{{{{adfkjaefia}}}}}}}'), False)
12+
13+
def test2(self):
14+
self.assertEqual(valid_closers('{{{{{{{{{[[[[[[kadfa{{{{{{{((({daljfdaf({{{[]}}kaldjfs})})))}}}}}}}]]]]]]}kjfela}}}}}}}}'), True)
15+
16+
def test3(self):
17+
self.assertEqual(valid_closers('{{{[}}}}dafda'), False)
18+
19+
def test4(self):
20+
self.assertEqual(valid_closers('{{{{{{{{{}}}}}}}}}'), True)
21+
22+
def test5(self):
23+
self.assertEqual(valid_closers('[[[[[[[[[kafjalfeianfailfeja;fjai;efa;sfj]]]]]]]]]kjajdain'), True)
24+
25+
def test6(self):
26+
self.assertEqual(valid_closers(''), True)
27+
28+
def test7(self):
29+
self.assertEqual(valid_closers('((((((fjdalfeja((((alefjalisj(())))))))))))d'), True)
30+
31+
def test8(self):
32+
self.assertEqual(valid_closers(')))(((d'), False)
33+
34+
35+
def quickie(): # quick function I wrote to generate correct strings
36+
a = input()
37+
output = ""
38+
for char in a:
39+
if char == '{':
40+
output = '}' + output
41+
elif char == '(':
42+
output = ')' + output
43+
elif char == '[':
44+
output = ']' + output
45+
46+
print(a + output)
47+
48+
49+
if __name__ == '__main__':
50+
unittest.main()

challenge_10/tests/test1

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{{}}}

challenge_10/tests/test2

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{[[((ajfdafje))3jkad]jd]da}d}

challenge_10/tests/test3

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

challenge_10/tests/test4

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
()

challenge_10/tests/test5

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dajkf[afda]dafdsa

0 commit comments

Comments
 (0)