Skip to content

Instantly share code, notes, and snippets.

@oerpli
Created October 7, 2018 11:24
Show Gist options
  • Save oerpli/07db127146d859033d8b87e3601311df to your computer and use it in GitHub Desktop.
Save oerpli/07db127146d859033d8b87e3601311df to your computer and use it in GitHub Desktop.
# 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