Problem 268753 · medium · Level 02 Linear Data Structures

Turn Over Every Bundle of k

arrays · slicing · reversal

A clerk lifts the cards nums off a pile in bundles of k (a positive integer), turns each bundle over so its cards come out in reverse order, and lays the bundles down one after another in the order they were lifted. The last bundle may hold fewer than k cards; it is turned over as well. Return the new sequence of cards as a new list.

Examples

Input:  nums = [5, 8, 1, 9, 4, 6, 2], k = 3
Output: [1, 8, 5, 6, 4, 9, 2]
Explanation: bundles [5,8,1], [9,4,6] and [2] are each turned over.

Input:  nums = [7, 3, 9], k = 5
Output: [9, 3, 7]

Constraints

  • 0 <= len(nums) <= 10**5
  • 1 <= k <= 10**5
  • Return a new list; do not modify nums. One test calls nums_after(nums, k), which runs your reverse_groups, appends an item to the list you returned and then returns nums itself, so it must come back unchanged.
  • Target: O(n) time.

Goals

  • Process a list block by block
  • Reverse a shorter trailing block the same way
  • Keep the blocks in their original order
Starting Python…