Open In App

Python program to right rotate n-numbers by 1

Last Updated : 07 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Method 1:

Below is the implementation.
 

Python3




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)


Output

1 2 3 
2 3 1 
3 1 2 

Method  2: Using pop(),append() and loops

Python3




def print_pattern(n):
    x = []
    for i in range(1, n+1):
        x.append(i)
        print(i, end=" ")
    print()
    j = 0
    while(j < n-1):
        a = x[0]
        x.pop(0)
        x.append(a)
        for k in x:
            print(k, end=" ")
        print()
        j += 1
 
 
print_pattern(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 

Using modulus operator: In this approach, we are using the modulus operator and adding i to the loop variable j to get the current number in each iteration. The time complexity of this approach is O(n^2) as it requires two nested loops. The space complexity is O(1) as we are only using a few variables and not using any extra data structures.
 

Python3




def print_pattern(n):
    for i in range(n):
        for j in range(n):
            print((j+i)%n+1, end=" ")
        print()
 
# Driver's code
print_pattern(6)
#This code is contributed by Edula Vinay Kumar Reddy


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 


Previous Article
Next Article

Similar Reads

Python3 Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N
Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples:  Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6, 3, 8, 5, 2} Explanation: Even element = {2, 4, 6
2 min read
Rotate a picture using ndimage.rotate Scipy
Prerequisites: Mathplotlib, Scipy Some of the most common tasks in image processing are displaying images, Basic manipulations, Image filtering, Image segmentation. In this article, we will use a SciPy module "ndimage.rotate()" to rotate. The SciPy ndimage submodule is dedicated to image processing. Here, ndimage means an n-dimensional image. Appro
2 min read
Python3 Program to Rotate the matrix right by K times
Given a matrix of size N*M, and a number K. We have to rotate the matrix K times to the right side. Examples: Input : N = 3, M = 3, K = 2 12 23 34 45 56 67 78 89 91 Output : 23 34 12 56 67 45 89 91 78 Input : N = 2, M = 2, K = 2 1 2 3 4 Output : 1 2 3 4 A simple yet effective approach is to consider each row of the matrix as an array and perform an
2 min read
Python3 Program to Rotate the sub-list of a linked list from position M to N to the right by K places
Given a linked list and two positions 'm' and 'n'. The task is to rotate the sublist from position m to n, to the right by k places. Examples: Input: list = 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6, m = 2, n = 5, k = 2 Output: 1-&gt;4-&gt;5-&gt;2-&gt;3-&gt;6 Rotate the sublist 2 3 4 5 towards right 2 times then the modified list are: 1 4 5 2 3 6 Input: list
4 min read
Python Program for Program to cyclically rotate an array by one
Write a Python program for a given array, the task is to cyclically rotate the array clockwise by one time. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: arr[] = {5, 1, 2, 3, 4} Input: arr[] = {2, 3, 4, 5, 1}Output: {1, 2, 3, 4, 5} Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.Approach: Assign every element wi
4 min read
Python Program for Rotate a Matrix by 180 degree
Given a square matrix, the task is that turn it by 180 degrees in an anti-clockwise direction without using any extra space. Examples : Input: 1 2 3 4 5 6 7 8 9 Output: 9 8 7 6 5 4 3 2 1Input: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 Output: 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1Python Program for Rotate a Matrix by 180 degreeThe solution of this problem is that t
5 min read
Program to cyclically rotate an array by one in Python | List Slicing
Given an array, cyclically rotate the array clockwise by one. In this article, we will see how to cyclically rotate an array by one in Python. Example Input: arr = [1, 2, 3, 4, 5]Output: arr = [5, 1, 2, 3, 4]Reference: Program to cyclically rotate an array by one Python Program to Cyclically Rotate an Array by One in PythonBelow are the methods or
2 min read
Python Program to Rotate Matrix Elements
Given a matrix, clockwise rotate elements in it. Examples: Input 1 2 3 4 5 6 7 8 9 Output: 4 1 2 7 5 3 8 9 6 For 4*4 matrix Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. The idea is to use loops similar to the program f
3 min read
Python3 Program to Rotate all Matrix elements except the diagonal K times by 90 degrees in clockwise direction
Given a square matrix mat[][] of dimension N and an integer K, the task is to rotate the matrix by 90 degrees K times without changing the position of the diagonal elements. Examples: Input: mat[][] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}, {21, 22, 23, 24, 25}}, K = 1Output: 1 16 11 6 5   22 7 12 9 2 23 18 1
4 min read
Python3 Program to Rotate digits of a given number by K
INTRODUCTION: One important point to consider when working with the algorithm to rotate the digits of a given number by k positions is the time complexity. If we were to implement this algorithm using the approach shown in the previous example, the time complexity would be O(n), where n is the number of digits in the input number. This is because t
4 min read