Python program to right rotate n-numbers by 1

Given a number n. The task is to print n-integers n-times (starting from 1) and right rotate the integers by after each iteration.
Examples: 
 

Input : 6
Output :
1 2 3 4 5 6
2 3 4 5 6 1
3 4 5 6 1 2
4 5 6 1 2 3
5 6 1 2 3 4
6 1 2 3 4 5

Input : 3
Output :
1 2 3 
2 3 1 
3 1 2


Below is the implementation.
 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

def print_pattern(n):
    for i in range(1, n+1, 1):
        for j in range(1, n+1, 1):
            # check that if index i is
            # equal to j
            if i == j:
 
                print(j, end=" ")
                # if index i is less than j
                if i <= j:
 
                    for k in range(j+1, n+1, 1):
                        print(k, end=" ")
 
                for p in range(1, j, 1):
                    print(p, end=" ")
 
        # print new line
        print()
 
 
# Driver's code
print_pattern(3)

chevron_right


Output:
 

1 2 3 
2 3 1 
3 1 2 

 

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up

I am a Btech IIIrd Year CSE Student I have a deep Interest in Data Structure and Algorithms I loved to solve the Real World Coding Problems

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : krishna_6431