Created
July 26, 2020 06:37
-
-
Save opethe1st/ddb4597e0ebdcca128dad695279efbf6 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
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