Skip to content

Commit 37bf248

Browse files
Merge pull request #127 from HimadriPathak/master
Rotating Array
2 parents 699810a + f9ad7bb commit 37bf248

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

Array/Rotate_Left.py

+11-28
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,20 @@
44
# In[ ]:
55

66

7-
8-
97
arr = list();
108
n=input("Enter the number of elements you want in the array: ")
119

12-
print("Enter the array elements one by one:")
10+
#print("Enter the array elements one by one:")
1311
for i in range(int(n)):
14-
ele=input()
15-
arr.append(int(ele))
16-
12+
print("Enter the",i+1,"element of the array: ",end='')
13+
arr.append(int(input()))
14+
1715
#Display the original array
18-
print("Array before rotation is: ")
19-
for i in range(0, len(arr)):
20-
print(arr[i])
21-
22-
#Rotate the given array by 3 times towards left
23-
for i in range(0, 3):
24-
#Stores the first element of the array
25-
first = arr[0]
26-
27-
for j in range(0, len(arr)-1):
28-
#Shift element of array by one
29-
arr[j] = arr[j+1]
30-
31-
#First element of array will be added to the end
32-
arr[len(arr)-1] = first
33-
34-
print()
35-
36-
#Display the array after 3 left rotations
37-
print("Array after 3 left rotations is: ");
38-
for i in range(0, len(arr)):
39-
print(arr[i])
16+
print("Array before rotation is:",arr)
17+
18+
#rotation of the array by 3 elements to left by slicing
19+
l=arr[0:3]
20+
del arr[0:3]
21+
arr=arr+l
22+
print('Array after rotation is:',arr)
4023

Array/Rotate_Right.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
arr = list();
4+
n=int(input("Enter the number of elements you want in the array: "))
5+
6+
#print("Enter the array elements one by one:")
7+
for i in range(int(n)):
8+
print("Enter the",i+1,"element of the array: ",end='')
9+
arr.append(int(input()))
10+
11+
#Display the original array
12+
print("Array before rotation is:",arr)
13+
14+
#rotation of the array by 3 elements to right by slicing
15+
l=arr[n-3:n]
16+
del arr[n-3:n]
17+
l=l+arr
18+
print(l)

0 commit comments

Comments
 (0)