0
Q12. Reverse an array in groups of given size Input: arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] k = 3
3 odpowiedzi
0
the output be like [321654987]
0
arr=[1,2,3,4,5,6,7,8,9]
k=0
arr1=arr[k:k+3:-1]
k+=3
arr2=arr[k:k+3:-1]
k+=3
arr3=arr[k:k+3:-1]
so, but it doesn't works
0
Ok, I'll try to do a function
def reverse_group(arr, k):
res = []
l = len(arr)
for i in range(0, l, k):
a = arr[i:i+k]
res.append(a[::-1])
res = list(flatten(res))
return res
where flatten is itertools.chain.from_iterable