Skip to content

Instantly share code, notes, and snippets.

@opethe1st
Created July 26, 2020 06:37
Show Gist options
  • Save opethe1st/ddb4597e0ebdcca128dad695279efbf6 to your computer and use it in GitHub Desktop.
Save opethe1st/ddb4597e0ebdcca128dad695279efbf6 to your computer and use it in GitHub Desktop.
def solution(template, replacements):
ans = ''
i = 0
error = False
while i < len(template):
print(i)
if template[i] == '{':
if (i+1) < len(template) and template[i+1] == '{':
ans += '{'
i += 2
else:
lookahead = i+1
number = ''
while lookahead < len(template) and template[lookahead] != '}':
char = template[lookahead]
error = char not in set('0123456789')
if error:
break
number += template[lookahead]
lookahead += 1
if template[lookahead] == '}':
if int(number) < len(replacements):
ans += replacements[int(number)]
else:
error = True
i = lookahead +1
else:
ans += template[i]
i += 1
if error:
break
return '' if error else ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment