Collections.deque()

Sort by

recency

|

655 Discussions

|

  • + 0 comments
    # Enter your code here. Read input from STDIN. Print output to STDOUT
    from collections import deque
    d = deque()
    for _ in range(int(input())):
        function = input().split()
        match function[0]:
            case "append":
                d.append(int(function[1]))
            case "pop":
                d.pop()
            case "popleft":
                d.popleft()
            case "appendleft":
                d.appendleft(int(function[1]))
    print (" ".join(map(str,d)))
    
    
    
    
    
    
    
  • + 0 comments

    Here is HackerRank Collections.deque() in python solution - https://programmingoneonone.com/hackerrank-collections-deque-solution-in-python.html

  • + 0 comments
    if __name__ == '__main__':
        from collections import deque
        operation_count = int(input())
        que = deque()
        for _ in range(operation_count):
            operation = str(input()).split(" ")
            if operation[0] == 'append':
                que.append(operation[1])
            if operation[0] == 'appendleft':
                que.appendleft(operation[1])
            if operation[0] == 'pop':
                que.pop()
            if operation[0] == 'popleft':
                que.popleft()
                
        for value in que:
            print(value, end=" ")
    
  • + 0 comments

    Here is my attempt:

    from collections import deque
    
    d = deque()
    
    for _ in range(int(input())):
        op, *args = input().rsplit()
        getattr(d, op)(*args)
    
    print(*d)
    
  • + 0 comments
    from collections import deque
    n = int(input())
    
    d = deque()
    for _ in range(n):
        text = input().rstrip().split()
        if len(text) > 1:
            opr, val = text
            getattr(d, opr)(int(val))
        else:
            getattr(d, text[0])()
    print(*d)