Skip to content

Commit a765ecc

Browse files
authored
Add files via upload
1 parent a2ac607 commit a765ecc

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

array3.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#function to rotate array by d element
2+
3+
def rotateArray(arr,n,d):
4+
temp = []
5+
i =0
6+
while (i<d):
7+
temp.append(arr[i])
8+
i = i+1
9+
i=0
10+
while(d<n):
11+
arr[i]=arr[d]
12+
i=i+1
13+
d=d+1
14+
arr[:] = arr[:i]+temp
15+
return arr
16+
17+
#Driver function to test above function
18+
arr = [1,2,3,4,5,6,7]
19+
print("Array after left rotation is:",end='')
20+
print(rotateArray(arr,len(arr),2))
21+
22+
23+
#Function to left rotate arr[] of size n by d*/
24+
25+
def leftRotate(arr,d,n):
26+
for i in range(d):
27+
leftRotatebyOne(arr,n)
28+
29+
#Function to left Rotate arr[] of size n 1*/
30+
def leftRotatebyOne(arr,n):
31+
temp = arr[0]
32+
for i in range(n-1):
33+
arr[i] = arr[i+1]
34+
arr[n-1] = temp
35+
#Utility function to print an array
36+
37+
def printArray(arr,size):
38+
for i in range(size):
39+
print("%d"% arr[i],end=" ")
40+
41+
#Driver program to test above function
42+
43+
arr = [1,2,3,4,5,6,7]
44+
leftRotate(arr,2,7)
45+
printArray(arr,7)
46+
47+
48+
#Function to left rotate arr[] of size n by d
49+
def leftRotate(arr,d,n):
50+
for i in range(gcd(d,n)):
51+
temp = arr[i]
52+
j = i
53+
while 1:
54+
k = j+d
55+
if k>=n:
56+
k = k-n
57+
if k==i:
58+
break
59+
arr[j] = arr[k]
60+
j = k
61+
arr[j] = temp
62+
63+
def printArray(arr,size):
64+
for i in range(size):
65+
print("%d" % arr[i],end='')
66+
def gcd(a,b):
67+
if b==0:
68+
return a;
69+
else:
70+
return gcd(b,a%b)
71+
72+
#Driver program to test above funtions
73+
arr = [1,2,3,4,5,6,7]
74+
leftRotate(arr,2,7)
75+
printArray(arr,7)
76+
77+
78+
#Python program using the List
79+
#slicing approch to rotate the array
80+
81+
def rotateList(arr,d,n):
82+
arr[:] = arr[d:n]+arr[0:d]
83+
return arr
84+
#Driver function to test above function
85+
arr = [1,2,3,4,5,6,7]
86+
print(arr)
87+
print("Rotate list is")
88+
print(rotateList(arr,2,len(arr)))
89+

0 commit comments

Comments
 (0)