0

I am studying rotating a list, and made a function to rotate the list left and right, but how can I write a code for how many times to rotate? if that makes a sense. I want to pass it as an argument to the functions.

table = [1, 10 ,20, 0, 59, 86, 32, 11, 9, 40]


def rotate_left():
    (table.append(table.pop(0)))
    return table

print(rotate_left())

def rotate_right():
    (table.insert(0,table.pop()))
    return table
    
print(rotate_right())
3
  • Can you elaborate with example about your issue? Commented Jun 19, 2022 at 23:17
  • It doesn't really make sense no, but if you provide step by step examples of what you want, before/after, it would make a lot more sense. Commented Jun 19, 2022 at 23:19
  • I want to run 2nd line of the function codes multiple time depending on an argument I pass. for an example, if I call a function with print(rotate_left(2)), it will rotate to left twice. Commented Jun 19, 2022 at 23:22

3 Answers 3

1

You can use for loop inside your functions and pass how many times you want to rotate as a argument.

table = [1, 10 ,20, 0, 59, 86, 32, 11, 9, 40]

def rotate_left(rotate_times):
    for _ in range(rotate_times):
        table.append(table.pop(0))
    return table

>>> print(rotate_left(2))
>>> [20, 0, 59, 86, 32, 11, 9, 40, 1, 10]

def rotate_right(rotate_times):
    for _ in range(rotate_times):
        table.insert(0,table.pop())
    return table
    
>>> print(rotate_right(2))
>>> [1, 10, 20, 0, 59, 86, 32, 11, 9, 40]

NOTE

In above scenario, be aware of the fact that, when you pass a list to a method and modify it inside that method, the changes are made in original list unless you make a deep copy, because list is a mutable type.

So, when you call rotate_left(2), it rotates the original list twice towards left. Then when you call rotate_right(2), it rotates the original list, which is already rotated by rotate_left(2), so we got the list as in initial order.

As, the functions are already modifying the original list, you can remove return table from the function (unless you want a new deep copy of list). And simply print the list after that like:

def rotate_left(rotate_times):
    for _ in range(rotate_times):
        table.append(table.pop(0))
 
>>> rotate_left(2)
>>> print(table)
>>> [20, 0, 59, 86, 32, 11, 9, 40, 1, 10]
Sign up to request clarification or add additional context in comments.

Comments

0

You can write a 'for loop' and use 'range' to decide how many times you want to rotate. In this example 'rotate_left()' is called three times:

table = [1, 10 ,20, 0, 59, 86, 32, 11, 9, 40]

def rotate_left():
    (table.append(table.pop(0)))
    return table


def rotate_right():
    (table.insert(0,table.pop()))
    return table
    
for i in range(3):
    print(rotate_left())

Comments

0

You can write a 'loop' and use 'range' to decide how many times you want to loop. In this example, there is a program that asks the user in which direction and how many times to turn and calculates.

def rotate_left(count,table):
    
    for i in range (count):
        (table.append(table.pop(0)))
    return table

def rotate_right(count,table):
   
    for i in range (count):
        (table.insert(0,table.pop()))
    return table

def main():

    table = [1, 10 ,20, 0, 59, 86, 32, 11, 9, 40]
    isSelect = True

    while(isSelect):
        rotate = int(input("1- Rotate Left\n2- Rotate Right\n: "))
        count = int(input("\nHow many times to rotate ?\n: "))

        if((rotate == 1 or rotate == 2) and count > 0):
            isSelect = False
            if (rotate == 1):
                print(rotate_left(count,table))
                
            elif (rotate == 2):
                print(rotate_right(count,table))
        else:
            print("\nInvalid Parameter. Please choose again.\n")
    
main()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.