Sum Fifteen

Asked at MetaOnsite

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

  1. Example 1

    Input: deck = [5,5,5,1,9,5,4,6], table_size = 4

    Output: 2

    Explanation: 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 cards 9, 5, 4 are added to give [1, 9, 5, 4]. Slots 0, 1, 2 sum to 15 again, so 1, 9, 5 go and [4] stays. Only the card 6 is left to draw, giving [4, 6], which has no triple. That is 2 triples.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.