Skip to content

Instantly share code, notes, and snippets.

@officialcjunior
Created November 2, 2019 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save officialcjunior/213d9a2a2b9fbe8b4dedc725460f6bf0 to your computer and use it in GitHub Desktop.
Save officialcjunior/213d9a2a2b9fbe8b4dedc725460f6bf0 to your computer and use it in GitHub Desktop.
This is a python program which right-rotates and left-rotates a list n times.
def leftrotate(a,n):
for i in range (n):
first=a[0]
for i in range (0,len(a)-1):
a[i]=a[i+1]
a.pop()
a.append(first)
print(a)
def rightrotate(a,n):
for i in range (n):
last=a[-1]
for i in range (len(a)-1,0,-1):
a[i]=a[i-1]
a[0]=last
print(a)
a=[int(i) for i in input("Enter the list elements (Space seperated) \n").split()]
n=int(input("Enter the number of times you want to rotate the list \n"))
rightrotate(a,n)
leftrotate(a,n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment