-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCTF--Coderbyte--Bracket_Combinations.txt
55 lines (42 loc) · 1.53 KB
/
CTF--Coderbyte--Bracket_Combinations.txt
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
### Bracket Combinations
"""Have the function BracketCombinations(num) read num which will be an integer greater than or equal to zero, and return the number of valid combinations that can be formed with num pairs of parentheses. For example, if the input is 3, then the possible combinations of 3 pairs of parenthesis, namely: ()()(), are ()()(), ()(()), (())(), ((())), and (()()). There are 5 total combinations when the input is 3, so your program should return 5. """
def BracketCombinations(num):
final = []
finalSet = set()
pp = ['()']
def on_side(pp):
p1 = '()'
answer = []
for p in pp:
answer.append(p+p1)
answer.append(p1+p)
return list(set(answer))
def around(pp):
pl = '('
pr = ')'
answer = []
for p in pp:
answer.append(pl+p+pr)
return list(set(answer))
def in_side(pp):
answer = []
for i in pp:
for j in range(len(i)):
if i[j:j+2] == '()':
answer.append(i[:j+1]+'()'+i[j+1:])
return list(set(answer))
if num == 1:
return len(pp)
else:
for i in range(1, num):
final.clear()
finalSet.clear()
final.append((on_side(pp)))
final.append((around(pp)))
final.append((in_side(pp)))
for i in final:
for j in i:
finalSet.add(j)
pp.clear()
pp = list(finalSet)
return len(list(finalSet))