Sum Fifteen
Asked at Meta
Reported by the problem's creator, not verified
A card game uses a deck of cards whose values are small integers. Implement play(deck, table_size).
The first table_size cards of deck are dealt face up in a row, in slot order (or the whole deck, if it is shorter). On each turn the player removes three cards from the table whose values sum to exactly 15, and then up to three cards are taken from the front of the unused deck and appended to the end of the table, in deck order; if fewer than three remain, all of them are taken. The game ends when no three cards on the table sum to 15.
play returns the number of triples removed.
Examples
Example 1
Input: deck =
[5,5,5,1,9,5,4,6], table_size =4Output:
2Explanation: The table starts as
[5, 5, 5, 1]. The first triple of slots is the three 5s, so they go,[1]stays, and the next three cards9, 5, 4are added to give[1, 9, 5, 4]. Slots 0, 1, 2 sum to 15 again, so1, 9, 5go and[4]stays. Only the card6is left to draw, giving[4, 6], which has no triple. That is 2 triples.