forked from learning-zone/python-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid_parenthesis_permutations.py
43 lines (29 loc) · 1.29 KB
/
valid_parenthesis_permutations.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
# Given a num, get all the valid parenthesis permutations.
# Observations:
# - All permutations start with opens
# - Base case: if num of open used == num of closed used
# - Num of opens used up, then next to the end must be closed
import unittest
def valid_parens_perms(num):
result = []
def recurse(substr, left, right):
if left == 0 and right == 0:
result.append(substr)
return
elif left == 0:
recurse(substr + ')', left, right - 1)
elif left < right:
recurse(substr + '(', left - 1, right)
recurse(substr + ')', left, right - 1)
elif left == right:
recurse(substr + '(', left - 1, right)
recurse('', num, num)
return result
class Testing(unittest.TestCase):
def test_valid_parens_perms(self):
self.assertEqual(valid_parens_perms(1), ['()'])
self.assertEqual(valid_parens_perms(2), ['(())', '()()'])
self.assertEqual(valid_parens_perms(3), ['((()))', '(()())', '(())()', '()(())', '()()()'])
self.assertEqual(valid_parens_perms(4), ['(((())))', '((()()))', '((())())', '((()))()', '(()(()))', '(()()())', '(()())()', '(())(())', '(())()()', '()((()))', '()(()())', '()(())()', '()()(())', '()()()()'])
if __name__ == '__main__':
unittest.main()