All questions
MediumSimulation · State Simulation
Design a Deck of Cards
Written by a community member · · 2 attempts · 0% pass
0
Implement a function that models a standard 52-card deck and supports shuffling, dealing, and resetting it.
Write a function deal_cards(actions: list[str]) -> list[str] that simulates a deck of cards and returns the cards dealt in order.
Rules:
- The deck starts as a standard 52-card deck in this order: all Clubs, then Diamonds, then Hearts, then Spades.
- Within each suit, ranks are ordered: A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K.
- Cards are represented as strings like
"A♣","10♦","K♠". - Each action is one of:
"shuffle": randomly shuffle the remaining undealt cards."deal n": deal the nextncards from the top of the deck and append them to the output."reset": restore the deck to its original ordered state.
- Shuffling only affects the remaining undealt portion of the deck.
- Dealing more cards than remain should return all remaining cards.
resetclears any prior shuffle/deal state and restores the full ordered deck.
Return a list of all dealt cards in the order they were dealt.
Examples:
deal_cards(["deal 3"])returns["A♣", "2♣", "3♣"]deal_cards(["deal 2", "reset", "deal 1"])returns["A♣", "2♣", "A♣"]
Constraints:
- Assume all actions are valid.
- Tests that shuffle compare the dealt cards in any order.