Created
January 9, 2017 18:36
-
-
Save glader/90f060f54e976dfe5119094ba9be5f43 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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