-
Write a simple parser that will parse and run Deadfish.
-
Deadfish has 4 commands, each 1 character long:
i
increments the value (initially 0)d
decrements the values
squares the valueo
outputs the value into the return array
-
Invalid characters should be ignored.
parse("iiisdoso") ==> [8, 64]
Solution:
def parse(data):
res = []
current = 0
for i in range(len(data)):
if data[i] == 'i':
current += 1
elif data[i] == 'd':
current -= 1
elif data[i] == 's':
current *= current
elif data[i] == 'o':
res.append(current)
else:
continue
return res
print(parse("ooo")); # [0, 0, 0]
print(parse("iiisdoso")); # [ 8, 64 ]