본문

파이썬 독학기 [Letter Queue] - Empire of Code

Letter Queue
In computer science, a queue is a particular kind of data type in which the entities in the collection are kept in order and the principal operations on the collection are the addition of entities to the rear terminal position (enqueue or push), and removal of entities from the front terminal position (dequeue or pop). 

오늘도 열심히 엠파이어 코드를 하면서 파이썬 공부를 하고 있습니다. 

컴퓨터에서 큐가 선입 선출(선입 선출)데이터 구조가 됩니다. 선입 선출 데이터 구조에서 큐에 추가되는 첫번째 요소는 제거되는 첫번째 요소가 됩니다. 이는 새 요소를 추가한 후 새 요소를 제거하기 전에 추가한 모든 요소를 제거해야 하는 요구 사항과 동일합니다.

간단하게 말해서 PUSH 다음에 나오는 알파벳은 적어주고 POP이 있으면 가장 처음 문자열을 없애주면 됩니다. 

from string import ascii_uppercase

alpha_list = list(ascii_uppercase) #알파벳 리스트 치기 귀찮아서 ascii로 불러 왔습니다. 근데 이게 더 귀찮네요.

def letter_queue(commands):

result = ""

for i in range(len(commands)):

    for j in alpha_list:

        if commands[i] == "PUSH " + j:

            result += j

    if commands[i] == "POP":

        result = result[1:]

return result


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"



print("All done? Earn rewards by using the 'Check' button!")




댓글