Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First (='ω'=) solution in Clear category for Letter Queue by Magu
from typing import List
from collections import deque
def letter_queue(commands: List[str]) -> str:
d = deque() # d.append(), d.popleft() <-- FIFO
for c in commands:
if c == 'POP' and d: d.popleft()
elif c != 'POP': d.append(c[-1])
return ''.join(d)
if __name__ == '__main__':
print("Example:")
print(letter_queue(['PUSH A',
'POP',
'POP',
'PUSH Z',
'PUSH D',
'PUSH O',
'POP',
'PUSH T']))
# These "asserts" are used for self-checking and not for an auto-testing
assert letter_queue(['PUSH A',
'POP',
'POP',
'PUSH Z',
'PUSH D',
'PUSH O',
'POP',
'PUSH T']) == 'DOT'
assert letter_queue(['POP', 'POP']) == ''
assert letter_queue(['PUSH H', 'PUSH I']) == 'HI'
assert letter_queue([]) == ''
print("Coding complete? Click 'Check' to earn cool rewards!")
Oct. 13, 2021