0

Q12. Reverse an array in groups of given size Input: arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] k = 3

4th Jan 2017, 3:36 PM
manish
3 Réponses
0
the output be like [321654987]
4th Jan 2017, 3:36 PM
manish
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
4th Jan 2017, 3:55 PM
Maxim Kuzmin
Maxim Kuzmin - avatar
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
5th Jan 2017, 6:06 PM
Amaras A
Amaras A - avatar