• We just launched and are currently in beta. Join us as we build and grow the community.

How to Rotate Array Position in Console Terminal using Python

Number23

SMM Automation Expert
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
131
Likes
69
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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. no_of_reverse =

    end-start+1


  3. count =

    0
  4. while

    (

    (

    no_of_reverse)

    //2

    !=

    count)

    :
  5. arr[

    start+count]

    ,

    arr[

    end-count]

    =

    arr[

    end-count]

    ,

    arr[

    start+count]
  6. count +=

    1
  7. return

    arr


  8. def

    left_rotate_array(

    arr,

    size,

    d)

    :


  9. start =

    0
  10. end =

    size-1
  11. arr =

    reverse(

    start,

    end,

    arr)

  12. start =

    0
  13. end =

    size-d-1
  14. arr =

    reverse(

    start,

    end,

    arr)

  15. start =

    size-d
  16. end =

    size-1
  17. arr =

    reverse(

    start,

    end,

    arr)
  18. return

    arr

  19. ret =

    False


  20. while

    True

    :
  21. print

    (

    "\n

    ================= Rotate Array Position =================\n

    \n

    "

    )



  22. arr =

    [

    1

    ,

    2

    ,

    3

    ,

    4

    ,

    5

    ,

    6

    ,

    7

    ,

    8

    ,

    9

    ]
  23. size =

    9
  24. n =

    int

    (

    input

    (

    "Enter a number 1-9: "

    )

    )


  25. print

    (

    'Original array:'

    ,

    arr)


  26. if

    (

    n <=

    size)

    :
  27. print

    (

    'Rotated array: '

    ,

    left_rotate_array(

    arr,

    size,

    n)

    )
  28. else

    :
  29. n =

    n % size
  30. print

    (

    'Rotated array: '

    ,

    left_rotate_array(

    arr,

    size,

    n)

    )


  31. opt =

    input

    (

    "\n

    Do you want to try again?(yes/no): "

    )

  32. if

    opt.lower

    (

    )

    ==

    'yes'

    :
  33. ret=

    False
  34. elif

    opt.lower

    (

    )

    ==

    'no'

    :
  35. ret=

    True
  36. print

    (

    "Exiting program...."

    )

  37. else

    :
  38. print

    (

    "Please enter yes/no:"

    )
  39. break

  40. if

    ret ==

    False

    :
  41. 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:

how-to-rotate-array-position-in-python-1.jpg

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


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

329,392

329,400

Top