Last active
November 14, 2018 16:19
-
-
Save pepasflo/802b7ae7c0d2b3aa9fa02b86e572264a to your computer and use it in GitHub Desktop.
large test file generator for the "balanced parens" problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ for i in {1..10}; do ./gen-test-file.py 20 ; echo ; done | |
[{{}{[[(){}[[][[([[][()]])(())]][{}]]][{}]]}}] | |
{[{{}([{{({[[(({}{{}}()))]]})[{}]}}])}()]} | |
{[{[[{{[]}([{({(({([])}))}{[]})}]())()}]]}[]()]} | |
()[({({[]({[{()}{[()]}]}[])}{}()())})[]] | |
[[([]{}{([([[{(([{()[([])]}])[])[]}](){}(){[]}])])}[])]] | |
[({{[{[][({[]({[(){()[]}][]})}{}())]}]}{}})[]] | |
({[]{()}(((({({[]([[[]]])()})})){()}){}[])}()[]) | |
[(){{{[((((([[{{}}({})]{}]([{}]())[])){})))]}}({}()[])}] | |
[{{{}}([[]([]()({}[]()))])}] | |
(())()({(([(){}{}[][([])]]))}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import random | |
def opposite(ch): | |
if ch == '(': return ')' | |
if ch == '[': return ']' | |
if ch == '{': return '}' | |
openers = ['(', '[', '{'] | |
closers = [')', ']', '}'] | |
output = [] | |
stack = [] | |
# in first half, 75% chance of push, 25% chance pop | |
# in second half, reverse, until pop to zero. | |
early = [True, True, True, False] | |
late = [True, False, False, False] | |
n = int(sys.argv[1]) | |
for _ in xrange(n): | |
if random.choice(early): | |
ch = random.choice(openers) | |
output.append(ch) | |
stack.append(ch) | |
else: | |
if len(stack) > 0: | |
ch = stack.pop() | |
output.append(opposite(ch)) | |
while True: | |
if random.choice(late): | |
ch = random.choice(openers) | |
output.append(ch) | |
stack.append(ch) | |
else: | |
ch = stack.pop() | |
output.append(opposite(ch)) | |
if len(stack) == 0: | |
break | |
for ch in output: | |
sys.stdout.write(ch) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment