물음표 살인마의 개발블로그

알고리즘 문제/CodeWar

Make the Deadfish Swim

BEstyle 2022. 10. 11. 12:52

DESCRIPTION:

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 value
  • s squares the value
  • o outputs the value into the return array

Invalid characters should be ignored.

parse("iiisdoso")  ==>  [8, 64]

def parse(data):
    alist=[]
    ans=0
    for i in data:
        if i=="i":
            ans+=1
        elif i=="d":
            ans-=1
        elif i=="s":
            ans=ans*ans
        elif i=="o":
            alist.append(ans)
    return alist

'알고리즘 문제 > CodeWar' 카테고리의 다른 글

Meeting  (0) 2022.10.13
Sum of Parts  (0) 2022.10.12
IP Validation  (0) 2022.10.11
Give me a Diamond  (0) 2022.10.11
Greed is Good  (0) 2022.10.11