Skip to content

Instantly share code, notes, and snippets.

@glader
Created January 9, 2017 18:36
Show Gist options
  • Save glader/90f060f54e976dfe5119094ba9be5f43 to your computer and use it in GitHub Desktop.
Save glader/90f060f54e976dfe5119094ba9be5f43 to your computer and use it in GitHub Desktop.
import re
import pytest
def cut_by_re(s):
return re.sub(r'\([^)]*$', '', s)
def cut_by_state(s):
final = ''
temp = ''
in_parentheses = False
for c in s:
if c == '(':
in_parentheses = True
temp += c
elif c == ')':
in_parentheses = False
final += temp + c
temp = ''
else:
if in_parentheses:
temp += c
else:
final += c
if not in_parentheses:
final += temp
return final
checks = '''
esdfd((esdf)(esdf esdfd((esdf)
a a
a(b a
a(( a
a(b) a(b)
a(b)c a(b)c
a(b(c) a(b(c)
a(b(c a
a(()()( a(()()
a(b(c()b(c a(b(c()b
a((bb))()(c a((bb))()
'''.strip()
@pytest.mark.parametrize('func_name', ['cut_by_re', 'cut_by_state'])
def test_state(func_name):
func = globals()[func_name]
for check in checks.split('\n'):
start, finish = check.split(' ')
assert func(start) == finish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment