Reverse blocks of k
Your task
Given an integer array nums and a positive integer k, reverse every consecutive block of k elements in place: the first k elements get reversed, then the next k, and so on. If fewer than k elements remain at the end, reverse that shorter block too. Return the array. Example: nums = [1, 2, 3, 4, 5], k = 2 gives [2, 1, 4, 3, 5], and with k = 3 it gives [3, 2, 1, 5, 4].
Examples
reverse_blocks(nums: list[int], k: int) -> list[int]Example 1
Input
reverse_blocks([1, 2, 3, 4, 5], 2)Output
[2, 1, 4, 3, 5]