ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 10866번 : 덱 (Python, 파이썬) - 자료구조 덱(Deque)
    카테고리 없음 2019. 1. 14. 23:17

    https://www.acmicpc.net/problem/10866


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    class deque:
        def __init__(self):
            self.items = []
        def push_front(self,x):
            self.items.insert(0,x)
        def pop_front(self):
            if len(self.items) == 0:
                return -1
            else:
                return self.items.pop(0)
        def push_back(self,x):
            self.items.append(x)
        def pop_back(self):
            if len(self.items) == 0:
                return -1
            else:
                return self.items.pop()
        def size(self):
            return len(self.items)
        def empty(self):
            if len(self.items) == 0:
                return 1
            else:
                return 0
        def front(self):
            if len(self.items) == 0:
                return -1
            else:
                return self.items[0]
        def back(self):
            if len(self.items) == 0:
                return -1
            else:
                return self.items[-1]
     
    = deque()
    = int(input())
    for _ in range(N):
        cmd = list(input().split(' '))
        if cmd[0== 'push_back':
            D.push_back(int(cmd[1]))
        elif cmd[0== 'push_front':
            D.push_front(int(cmd[1]))
        elif cmd[0== 'pop_front':
            print(D.pop_front())
        elif cmd[0== 'pop_back':
            print(D.pop_back())
        elif cmd[0== 'size':
            print(D.size())
        elif cmd[0== 'empty':
            print(D.empty())
        elif cmd[0== 'front':
            print(D.front())
        elif cmd[0== 'back':
            print(D.back())
    cs


Designed by Tistory.