Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
split + for solution in Clear category for Letter Queue by naf-naf
def letter_queue(commands):
# split a list of strings into a list of lists
# [['PUSH', 'A'], ['POP']...]
stack = [i.split() for i in commands]
res = ''
for item in stack:
if 'PUSH' in item:
res += item[1]
elif 'POP' in item and res:
res = res[1:]
return res
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"
July 22, 2020