Created
August 7, 2014 04:42
-
-
Save luizirber/e393ebc1cfb3d0e329ee to your computer and use it in GitHub Desktop.
Basic bash brace expansion exercise
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
def brace_expand(expr): | |
expansions = [] | |
for block in expr.split('{'): | |
if '}' in block: | |
expansion, remainder = block.split('}') | |
expanded = [] | |
for piece in expansions: | |
for component in expansion.split(','): | |
expanded.append("".join((piece, component, remainder))) | |
expansions = expanded | |
else: | |
expansions.append(block) | |
return " ".join(expansions) |
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
from brace_expand import brace_expand | |
def test_simple(): | |
assert brace_expand("{ab,c}a") == "aba ca" | |
def test_multiple(): | |
assert brace_expand("{ab,c,d,ef}a") == "aba ca da efa" | |
def test_with_prefix_and_suffix(): | |
assert brace_expand("a{ab,c}a") == "aaba aca" | |
def test_double(): | |
assert brace_expand("{ab,c}{d,e}") == "abd abe cd ce" | |
def test_double_with_middle(): | |
assert brace_expand("{ab,c}ff{d,e}") == "abffd abffe cffd cffe" | |
def test_double_with_prefix_and_suffix(): | |
assert brace_expand("f{ab,c}{d,e}f") == "fabdf fabef fcdf fcef" | |
def test_no_expansion(): | |
assert brace_expand("d,e") == "d,e" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment