Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Collections.deque() Hacker Rank Solution – Queslers

Problem: Collections.deque() Hacker Rank Solution

A deque is a double-ended queue. It can be used to add or remove elements from both ends.
Deques support thread safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.

Example

code :

>>> from collections import deque
>>> d = deque()
>>> d.append(1)
>>> print d
deque([1])
>>> d.appendleft(2)
>>> print d
deque([2, 1])
>>> d.clear()
>>> print d
deque([])
>>> d.extend('1')
>>> print d
deque(['1'])
>>> d.extendleft('234')
>>> print d
deque(['4', '3', '2', '1'])
>>> d.count('1')
1
>>> d.pop()
'1'
>>> print d
deque(['4', '3', '2'])
>>> d.popleft()
'4'
>>> print d
deque(['3', '2'])
>>> d.extend('7896')
>>> print d
deque(['3', '2', '7', '8', '9', '6'])
>>> d.remove('2')
>>> print d
deque(['3', '7', '8', '9', '6'])
>>> d.reverse()
>>> print d
deque(['6', '9', '8', '7', '3'])
>>> d.rotate(3)
>>> print d
deque(['8', '7', '3', '6', '9'])

Task :

Perform appendpoppopleft and appendleft methods on an empty deque d.

Input Format :

The first line contains an integer N, the number of operations.
The next N lines contains the space separated names of methods and their values.

Constraints :

  • 0 < N <= 100

Output Format :

Print the space separated elements of deque d.

Sample Input :

6
append 1
append 2
append 3
appendleft 4
pop
popleft

Sample Output :

1 2

Collections.deque() Hacker Rank Solution in python 2

# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import deque
d=deque()
N=int(raw_input())
for i in range(N):
    A=list(raw_input().split())
    if A[0]=='append':
        d.append(int(A[1]))
    elif A[0]=='appendleft':
        d.appendleft(int(A[1]))
    elif A[0]=='pop':
        d.pop()
    elif A[0]=='popleft':
        d.popleft()
for i in d:
    print i,

Collections.deque() Hacker Rank Solution in python 3

# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import deque
d = deque()
for _ in range(int(input())):
    inp = input().split()
    getattr(d, inp[0])(*[inp[1]] if len(inp) > 1 else [])
print(*[item for item in d])

Collections.deque() Hacker Rank Solution in pypy

# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import deque
d = deque()
for i in range(input()):
    eval('d.{0}({1})'.format(*raw_input().split()+['']))
print ' '.join(map(str,d)) 

Collections.deque() Hacker Rank Solution in pypy 3

# https://www.hackerrank.com/challenges/py-collections-deque?h_r=next-challenge&h_v=zen

from collections import deque

d = deque()
for i in range(int(input())):
    
    operation, *number = input().split()
    if number:
        num1 = int(number[0])
    if operation == 'append':
        d.append(num1)
    elif operation == 'appendleft':
        d.appendleft(num1)
    elif operation == 'pop':
        d.pop()
    elif operation == 'popleft':
        d.popleft()
    

for i in range(len(d)):
    print(d.popleft(), end = ' ')
Collections.deque() Hacker Rank Solution Review:

In our experience, we suggest you solve this Collections.deque() Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

Collections.deque() problem is available on Hacker Rank for Free, if you are stuck anywhere between a compilation, just visit Queslers to get Collections.deque() Hacker Rank Solution

Conclusion:

I hope this Collections.deque() Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning Programming in a business context; there are no prerequisites.

Keep Learning!

More Hacker Rank Problem & Solutions >>

Staircase Hacker Rank Solution

A Very Big Sum Hacker Rank Solution

Diagonal Difference Hacker Rank Solution

Nested Lists Hacker Rank Solution

Lists Hacker Rank Solution

Leave a Reply

Your email address will not be published. Required fields are marked *