FunctionRacerProblemsRace

Deal Into Piles

BeginnerArraysPython, JavaScript, TypeScript, Java, C++

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

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Arrays problems