Deal Into Piles
Deal the items of cards one at a time into k piles, going round and round: the first item goes to pile 0, the second to pile 1, and so on up to pile k - 1, after which the next item goes to pile 0 again.
Return the piles as a list of k lists in pile order. Each pile keeps its items in the order they were dealt, and a pile that received nothing is an empty list.
Example 1
cards [1,2,3,4,5,6,7]
k 3
returns [[1,4,7],[2,5],[3,6]]
Items 1, 4 and 7 land on pile 0, items 2 and 5 on pile 1, items 3 and 6 on pile 2.
Example 2
cards [10,20,30]
k 1
returns [[10,20,30]]
With one pile everything stays together in order.
Constraints
0 <= len(cards) <= 100-1000 <= cards[i] <= 10001 <= k <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Arrays problems
- Decompress a Pair ListBeginner
- Find the Highest AltitudeBeginner
- Groups of KBeginner
- Items Within BudgetBeginner
- Kids With the Most CandiesBeginner
- Largest At Least Twice OthersBeginner
- Longest Increasing RunBeginner
- Max Consecutive OnesBeginner