-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst
More file actions
113 lines (85 loc) · 2.22 KB
/
first
File metadata and controls
113 lines (85 loc) · 2.22 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from copy import deepcopy
def check2(arr):
balance = 0
size = len(arr)
for i in arr[::-1]:
if i=='(':
print ("yes")
def checkBalance(arr,i):
balance = -1
for i in range(0, i):
if arr[i] == '(':
balance += 1
if arr[i] == ')':
balance -= 1
if balance < 0:
return False
else:
return True
s = "??(?)?"
brackets = []
for i in s:
brackets.append(i)
first = deepcopy(brackets)
print ("K: ", first)
counter_open = counter_close = n = 0
qindex =[]
for i in brackets:
if i == '(':
counter_open += 1
if i == ')':
counter_close += 1
if i == '?':
qindex.append(n)
n += 1
print(counter_open, "opened | ", counter_close, "closed | ", "all: ", n, '\n')
if n % 2 == 1 or counter_open > n/2 or counter_close > n/2:
print("NO WAY")
exit()
while True:
if counter_open + counter_close == n:
break
if counter_open < n/2 and '?' in first:
j = first.index('?')
first[j] = '('
counter_open += 1
continue
if counter_open > counter_close:
j = first.index('?')
first[j] = ')'
counter_close += 1
continue
print(first)
print(qindex)
temp = deepcopy(first)
print (temp)
counter_open = counter_close = 0
i = len(temp)
while i != 0:
i-=1
if i in qindex and temp[i] == '(' and checkBalance(temp, i) is True:
print ("Nice")
# REPLACE IF ALL IS RIGHT
temp[i] = ')'
temp [i:n-1] = []
for r in temp:
if r == '(':
counter_open += 1
if r == ')':
counter_close += 1
if r == '?':
qindex.append(n)
while True:
if counter_open + counter_close == n:
break
if counter_open < n / 2 and i in qindex:
temp.append('(')
counter_open += 1
continue
if counter_open > counter_close and i in qindex:
temp.append(')')
counter_close += 1
continue
print (temp)
i = len(temp)-1
counter_open = counter_close = 0