Collections.deque()

Sort by

recency

|

633 Discussions

|

  • + 0 comments

    The shortest code I could come with. I wonder if it could be shorter.

    from collections import deque
    
    d = deque()
    for _ in range(int(input())):
        cmd, *attribute = input().split()
        eval(f'd.{cmd}({(attribute[0:1] or ('',))[0]})')
    print(*d)
    
  • + 0 comments

    from collections import deque n = int(input()) d = deque() for i in range(n): val = input().split() if val[0] == 'append': d.append(int(val[1])) elif val[0] == 'pop': d.pop() elif val[0] == 'popleft': d.popleft() elif val[0] == 'appendleft': d.appendleft(int(val[1])) for i in d: print(i, end=' ')

  • + 0 comments
    from collections import deque
    
    d = deque()
    
    n = int(input())
    
    for i in range(n):
        command, *number = input().split()
        
        if number:
            number = int(number[0])
            getattr(d, command)(number)
        
        else:
            getattr(d, command)()
            
    print(*d)
    
  • + 0 comments

    Here's my code:

    from collections import deque
    res = deque()
    for i in range(int(input())):
        u = input().split()
        if not hasattr(res, u[0]):
            continue
        val = u[1] if len(u) > 1 else None
        if val:
            getattr(res, u[0])(val)
        else:
            getattr(res, u[0])()
    print(*res)
    
  • + 0 comments
    # Enter your code here. Read input from STDIN. Print output to STDOUT
    from collections import deque
    
    n = int(input())
    empty_queue = deque()
    
    for i in range(n):
        m = input().split()
    
        if m[0] == 'append':
            empty_queue.append(m[1])
        if m[0] == 'pop':
            empty_queue.pop()
        if m[0] == 'appendleft':
            empty_queue.appendleft(m[1])
        if m[0] == 'popleft':
            empty_queue.popleft()
    
    print(*empty_queue)