Arrange a deck so cards come up in order

Write codeMedium25 min

Your task

A magician deals from a deck with a fixed routine. Show the top card and set it aside, then move the next top card to the bottom of the deck, and repeat. You want the cards to come up from smallest to largest.

Write deck_revealed_increasing(deck) that returns the order to stack the cards in, from top to bottom.

Input and output:

  • deck: a list of 1 to 1,000 distinct integers, each from 1 to 1,000,000, in any order.
  • Returns a list of the same integers, from the top of the deck to the bottom.

Rules:

  • The routine repeats until no cards are left. Reveal the top card and remove it. Then, if any cards remain, move the card now on top to the bottom of the deck.
  • The cards must be revealed in increasing order of value.
  • Exactly one arrangement does this. Return it.

Examples

deck_revealed_increasing(deck: list[int]) -> list[int]
Example 1
Inputdeck_revealed_increasing([17, 13, 11, 2, 3, 5, 7])
Output[2, 13, 3, 11, 5, 17, 7]
Example 2
Inputdeck_revealed_increasing([10, 9, 8])
Output[8, 10, 9]