Created
October 7, 2018 11:24
-
-
Save oerpli/07db127146d859033d8b87e3601311df to your computer and use it in GitHub Desktop.
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
# https://techdevguide.withgoogle.com/resources/compress-decompression/#! | |
def decomp(s,i = None): | |
i = [0] if i is None else i | |
number = 0 | |
out = [] | |
while i[0] < len(s): | |
c = s[i[0]] | |
i[0] += 1 | |
if c == ']': | |
return out | |
elif c == '[': | |
out += decomp(s,i) * number | |
number = 0 | |
elif c.isdigit(): | |
number *= 10 | |
number += int(c) | |
else: | |
out.append(c) | |
return ''.join(out) | |
decomp("3[abc]4[ab]c") | |
decomp('2[3[a]b]x') | |
decomp('2[0[abcd]]') # inner bracket 0 times, thus '', outer bracket empty |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment