Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Letter Queue by ChubinOleg
def letter_queue(commands):
from collections import deque
queue = deque()
mapping = {
"PUSH": lambda x: queue.append(x),
"POP": lambda: queue and queue.popleft() or None
}
for command in commands:
cmds = command.split()
args = cmds[1:]
mapping[cmds[0]](*args)
return "".join(queue)
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert letter_queue(["PUSH A", "POP", "POP", "PUSH Z", "PUSH D", "PUSH O", "POP", "PUSH T"]) == "DOT", "dot example"
assert letter_queue(["POP", "POP"]) == "", "Pop, Pop, empty"
assert letter_queue(["PUSH H", "PUSH I"]) == "HI", "Hi!"
assert letter_queue([]) == "", "Nothing"
Oct. 2, 2014