Collection.deque() | HackerRank Solution

admin
By -
0

This similar like our other problems like those previous newspaper subscription based stuff here we just need to use  deque, and perform various task on deque of collections according to it deque intilai y is empty and we perform task on it i perfear match case inated if else here this time and

use another for  loop  to print into single line



Problem:https://www.hackerrank.com/challenges/py-collections-deque/problem?isFullScreen=true

Code:

# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import deque
n = int(input())
d = deque()
for _ in range(n):
    user_input= input().split()
    x = user_input[0]
    y =  int(user_input[1]) if len(user_input) > 1 else None
 
    match x, y:
        case ("append", x):
            d.append(y)
        case ("appendleft", x):
            d.appendleft(y)
        case ("pop" ,x):
            d.pop()
        case ("popleft", x):
            d.popleft()
           
for item in d:
    print(item, end=" ")

Time Complexity : Cause it works on n times input and iteration also constant size
so Big(O)n Overall see

We can do this on less line possible if you know then comment the your solution as well.

   
   

Post a Comment

0Comments

Please Select Embedded Mode To show the Comment System.*