How can I make rotate que by deque?

Ironman

My Code (by. List)

n, m = map(int, input().split())
x = list(map(int,input().split()))

a = [z for z in range(1, n+1)]
print(a)
# x = [d, e, f]

f_idx = 0
b_idx = 0

for i in range(len(x)-1):
    if a.index(x[i]) == 0 :
            a.pop(0)
            print(a)
            
    
    elif a.index(x[i]) <= a.index(x[i+1]) :
            while True:
                a.append(a.pop(0))
                f_idx += 1
                if a.index(x[i]) == 0 :
                    a.pop(0)
                    print(a)
                    break    
                
    elif a.index(x[i]) > a.index(x[i+1]) :
           while True:
               a.insert(0,a.pop(-1))
               b_idx +=1
               if a.index(x[i]) == 0 :
                   a.pop(0)
                   print(a)
                   break
                   print(a)
                   break
    
    if i == len(x)-2 :
        if a.index(x[i+1]) == 0 :
            a.pop(0)

        elif a.index(x[i+1]) < len(a)/2:
            while True:
                a.append(a.pop(0))
                f_idx += 1
                if a.index(x[i+1]) == 0 :
                    a.pop(0)
                    print(a)
                    break    

        else:
            while True:
               a.insert(0,a.pop(-1))
               b_idx +=1
               if a.index(x[i+1]) == 0 :
                   a.pop(0)
                   print(a)
                   break

print("f_idx :", f_idx)
print("b_idx :", b_idx)
print(a)

Practice(To make rotate Que)

  • input1 :

10, 3

1, 2, 3

  • output1 : (f.idx + b.idx)

0

  • input2 :

10, 3

2, 9, 5

  • output2 : (f.idx + b.idx)

8

I already made rotate que by list.

But I want to make rotate que by deque.

So my question is 2.

  1. What is the time complexity of my code?

  2. How can I make rotate que by deque?

Ironman
# deque

from collections import deque

n, m = map(int, input().split())
x = list(map(int,input().split()))

a = deque(z for z in range(1, n+1))
print(a)
# x = [d, e, f]

f_idx = 0
b_idx = 0


for i in range(len(x)-1):
    if a.index(x[i]) == 0 :
            a.popleft()
            print(a)
            
    elif a.index(x[i]) <= a.index(x[i+1]) :
            while True:
                a.append(a.popleft())
                f_idx += 1
                if a.index(x[i]) == 0 :
                    a.popleft()
                    print(a)
                    break    
                
    elif a.index(x[i]) > a.index(x[i+1]) :
           while True:
               a.appendleft(a.pop())
               b_idx +=1
               if a.index(x[i]) == 0 :
                   a.popleft()
                   print(a)
                   break
    
    if i == len(x)-2 :
        if a.index(x[i+1]) == 0 :
            a.popleft()

        elif a.index(x[i+1]) < len(a)/2:
            while True:
                a.append(a.popleft())
                f_idx += 1
                if a.index(x[i+1]) == 0 :
                    a.popleft()
                    print(a)
                    break    

        else:
            while True:
               a.appendleft(a.pop())
               b_idx +=1
               if a.index(x[i+1]) == 0 :
                   a.popleft()
                   print(a)
                   break

print("f_idx :", f_idx)
print("b_idx :", b_idx)
print(a)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I make a trim rotate in SWIFTUI?

How can i rotate?

How can I make one method to rotate multiple number of characters?

How can I make Floating Action Button rotate on every click

How can I make the Earth rotate around it's axis in matlab?

How can I convert a string into a Deque?

How can I make turret rotate facing target if the target is close enough and make the target rotate around turret with random height and speed?

How can I rotate an element?

How can I rotate a Rectangle?

How can I rotate a video?

How can I rotate this array?

How to make an arrow rotate when I click on it?

How do I make the ball rotate?

How to i make an sprite rotate smoothly

How can I make the npc character to rotate slowly facing the player while changing animator transitions?

How can I make the object to rotate back when releasing a key press?

How can I rotate and make the image semitransparent at the same time by using ATL::CImage?

How can I make my player move whilst still being able to rotate?

How can I make my FBX model rotate using animate function?

How can I make the a RotateTransition rotate my widget half way (180 degrees)

How can I make a Player object rotate using the A and D keys in Unity 3D?

How Can I Make This Car Rotate Properly With or Without Merging Meshes(Three.js)?

How can I deque UICollectionViewCells before needed, as first time deque is laggy

In Java, how can I implement new functionality in a subclass of a Deque class without access to the private fields in the Deque class?

How can i auto rotate this carousel

How can I rotate a linear gradient?

How can I rotate a matplotlib map?

How can I rotate the screen in Swift 3?

How can I rotate Rectangles in Libgdx?