-
Notifications
You must be signed in to change notification settings - Fork 13
/
Swap Alternate.py
43 lines (31 loc) · 932 Bytes
/
Swap Alternate.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
# You have been given an array/list(ARR) of size N. You need to swap every pair of alternate elements in the array/list.
# You don't need to print or return anything, just change in the input array itself.
from sys import stdin
def swapAlternate(arr, n):
#Your code goes here
length = len(arr)
if length == 1:
print(arr)
else:
for i in range(0, length-1, 2):
arr[i], arr[i+1] = arr[i+1], arr[i]
#Taking Input Using Fast 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
#Printing 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()
if n != 0:
swapAlternate(arr, n)
printList(arr, n)
t -= 1