Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Python by (45.3k points)

For example, if N=2, given the input array [1, 2, 3, 4, 5, 6]

the function should return [5, 6, 1, 2, 3, 4] 

if N=3 then [4, 5, 6, 1, 2, 3]

1 Answer

0 votes
by (16.8k points)

Check this code:

def rotate(array, n):

    length = len(array)

    if n > 0:

        tail = array[length - n:]

        head = array[0:length - n]

        new_array = tail + head

    else:

        tail = array[- n:]

        head = array[0:- n]

        new_array = tail + head

    print(new_array)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...