How to Rotate Array Position in Console Terminal using Python

In this tutorial, we will program 'How to Rotate Array Positions in a Console Terminal using Python.' We will learn how to rotate the array positions by shifting the numbers. The objective is to safely rotate the array positions in an incrementing order. I will provide a sample program to demonstrate the actual coding process in this tutorial.

This topic is very easy to understand. Just follow the instructions I provide, and you will be able to do it yourself with ease. The program I will show you covers the basics of programming for rotating array positions. I will do my best to provide you with a simple method for incrementing the order of array positions. So, let's start coding

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. def reverse(start, end, arr):
  2.  
  3.        
  4.         no_of_reverse = end-start+1
  5.  
  6.  
  7.         count = 0
  8.         while((no_of_reverse)//2 != count):
  9.                 arr[start+count], arr[end-count] = arr[end-count], arr[start+count]
  10.                 count += 1
  11.         return arr
  12.  
  13.  
  14. def left_rotate_array(arr, size, d):
  15.  
  16.        
  17.         start = 0
  18.         end = size-1
  19.         arr = reverse(start, end, arr)
  20.  
  21.         start = 0
  22.         end = size-d-1
  23.         arr = reverse(start, end, arr)
  24.  
  25.         start = size-d
  26.         end = size-1
  27.         arr = reverse(start, end, arr)
  28.         return arr
  29.  
  30. ret = False
  31.  
  32.  
  33. while True:
  34.         print("\n================= Rotate Array Position =================\n\n")
  35.  
  36.  
  37.  
  38.         arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  39.         size = 9
  40.         n = int(input("Enter a number 1-9: "))
  41.  
  42.  
  43.         print('Original array:', arr)
  44.  
  45.  
  46.         if(n <= size):
  47.                 print('Rotated array: ', left_rotate_array(arr, size, n))
  48.         else:
  49.                 n = n % size
  50.                 print('Rotated array: ', left_rotate_array(arr, size, n))
  51.  
  52.  
  53.         opt = input("\nDo you want to try again?(yes/no): ")
  54.  
  55.         if opt.lower() == 'yes':
  56.                 ret=False
  57.         elif opt.lower() == 'no':
  58.                 ret=True
  59.                 print("Exiting program....")
  60.  
  61.         else:
  62.                 print("Please enter yes/no:")
  63.                 break
  64.  
  65.         if ret == False:
  66.                 continue

This Python script rotates an array to the left by a specified number of positions using a reverse function. It repeatedly prompts the user for a number to rotate the array and displays the rotated result. The loop continues until the user decides to exit the program.

Output:

There you have it we successfully created How to Rotate Array Position in Console Terminal using Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment