-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.py
153 lines (123 loc) · 4.02 KB
/
new.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import random
import pandas as pd
import csv
from tqdm import tqdm
def is_balanced(s):
stack = []
max_depth = 0
count = 0
for bracket in s:
if bracket == ")":
if stack:
stack.pop()
else:
stack.append(bracket)
break
else:
stack.append(bracket)
max_depth = max(max_depth, len(stack))
for bracket in s:
if bracket == "(":
count += 1
else:
count -= 1
if stack:
return -1 * max_depth, count
return max_depth, count
def generate_bracket_sequence(n):
brackets = ["(", ")"]
sequence = ""
for i in range(n):
sequence += random.choice(brackets)
if is_balanced(sequence)[0] < 0:
return sequence
else:
return generate_bracket_sequence(n)
def generate_unbalanced_brackets(n):
seq = generate_bracket_sequence(n)
bal = is_balanced(seq)
seq2 = seq
while bal[0] > 0:
print('yes')
seq2 = seq
index = random.randint(0, n-1)
if seq[index] == '(':
seq2 = seq[:index] + ')' + seq[index+1:]
else:
seq2 = seq[:index] + '(' + seq[index+1:]
bal = is_balanced(seq2)
return seq2
def generate_balanced_brackets(n):
sequence = ""
stack = []
for i in range(n):
open_or_close = random.choice([0, 1]) and stack
left = n - i
if left < len(stack) or (not open_or_close and len(stack) == left):
open_or_close = 1
if open_or_close:
sequence += ")"
stack.pop()
else:
sequence += "("
stack.append("(")
return sequence
if __name__=="__main__":
# balanced_brackets =[]
# unbalanced_brackets =[]
# balanced_seqs = set()
# unbalanced_seqs = set()
brackets = []
seqs = set()
count = 0
while count < 200:
# select a random length for the bracket sequence between 2 and 50
n = random.randint(2, 50)
while n % 2 != 0:
n = random.randint(2, 50) # make sure the length is even
# generate a balanced bracket sequence
balanced = generate_balanced_brackets(n)
# generate an unbalanced bracket sequence
unbalanced = generate_unbalanced_brackets(n)
# while balanced in balanced_seqs:
# balanced = generate_balanced_brackets(n)
# while unbalanced in unbalanced_seqs:
# unbalanced = generate_unbalanced_brackets(n)
if balanced in seqs:
continue
if unbalanced in seqs:
continue
seqs.add(balanced)
seqs.add(unbalanced)
# add the balanced and unbalanced sequences to the dictionary
# balanced_brackets.append({
# 'sequence': balanced,
# 'stack_depth': is_balanced(balanced)[0],
# 'count': is_balanced(balanced)[1],
# })
# unbalanced_brackets.append({
# 'sequence': unbalanced,
# 'stack_depth': is_balanced(unbalanced)[0],
# 'count': is_balanced(unbalanced)[1],
# })
brackets.append({
'sequence': balanced,
'stack_depth': is_balanced(balanced)[0],
'count': is_balanced(balanced)[1],
})
brackets.append({
'sequence': unbalanced,
'stack_depth': is_balanced(unbalanced)[0],
'count': is_balanced(unbalanced)[1],
})
count += 1
print(count, end='\r')
# balanced_seqs.add(balanced)
# unbalanced_seqs.add(unbalanced)
# export into csv file
# df = pd.DataFrame(balanced_brackets)
# df.to_csv('Data/new_new_balanced_brackets.csv', index=False)
# df = pd.DataFrame(unbalanced_brackets)
# df.to_csv('Data/new_new_unbalanced_brackets.csv', index=False)
df = pd.DataFrame(brackets)
df.to_csv('Data/new_new_brackets.csv', index=False)