-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRotatearray.py
50 lines (37 loc) · 1.33 KB
/
Rotatearray.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# You have been given a random integer array/list(ARR) of size N. Write a function that rotates the given array/list by D elements(towards the left).
# Input format :
# The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
# First line of each test case or query contains an integer 'N' representing the size of the array/list.
# Second line contains 'N' single space separated integers representing the elements in the array/list.
# Third line contains the value of 'D' by which the array/list needs to be rotated.
from sys import stdin
def rotate(arr, n, d):
#Your code goes here
temp = []
for i in range(d):
temp.append(arr[i])
for i in range(n-d):
arr[i] = arr[i+d]
for i in range(d):
arr[n-d+i] = temp[i]
return arr
#Taking Input Using Fats I/O
def takeInput() :
n = int(stdin.readline().rstrip())
if n == 0:
return list(), 0
arr = list(map(int, stdin.readline().rstrip().split(" ")))
return arr, n
#to print the array/list
def printList(arr, n) :
for i in range(n) :
print(arr[i], end = " ")
print()
#main
t = int(stdin.readline().rstrip())
while t > 0 :
arr, n = takeInput()
d = int(stdin.readline().rstrip())
rotate(arr, n, d)
printList(arr, n)
t -= 1