-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#6_kyu_Pairing_brackets.py
77 lines (61 loc) · 2.02 KB
/
#6_kyu_Pairing_brackets.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
65
66
67
68
69
70
71
72
73
74
75
76
77
Description:
"""
Write a function which outputs the positions of matching bracket pairs. The output should be a dictionary with keys the positions of the open brackets '(' and values the corresponding positions of the closing brackets ')'.
For example: input = "(first)and(second)" should return {0:6, 10:17}
If brackets cannot be paired or if the order is invalid (e.g. ')(') return False. In this kata we care only about the positions of round brackets '()', other types of brackets should be ignored.
"""
My codes:
def bracket_pairs(string):
if string.count("(") != string.count(")"):
return False
elif string.count(")") == 0:
return {}
li1,li2 = [],[]
for i in range(len(string)):
if string[i] == "(":
li1.append(i)
elif string[i] == ")":
try:
li2.append([li1.pop(),i])
except:
return False
li2.sort(key = lambda x:x[0])
return {x[0]:x[1] for x in li2}
Others codes:
def bracket_pairs(string):
brackets = {}
open_brackets = []
for i, c in enumerate(string):
if c == '(':
open_brackets.append(i)
elif c == ')':
if not open_brackets:
return False
brackets[open_brackets.pop()] = i
return False if open_brackets else brackets
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[len(self.items)-1]
def size(self):
return len(self.items)
def bracket_pairs(s):
l = len(s);
stack = Stack(); i = 0; res = {}; flag = True
while i < l and flag:
if s[i] == "(": stack.push(i)
elif s[i] == ")":
if stack.is_empty(): flag = False
else:
a = stack.pop()
res[a] = i
i += 1
if flag and stack.is_empty(): return res
return False