Playing Card Order and Shuffle
AppleOnsiteEasy
A playing card is a string Suit-Rank, such as Hearts-10 or Spades-A. There are four suits (Clubs, Diamonds, Hearts, Spades) and thirteen ranks (3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A, 2), giving a standard 52-card deck.
Implement a class Deck representing one such deck. Each test gives a sequence of operations and checks what each returns.
Part 1: Ordered deck and repeatable shuffle
Implement two methods.
order() returns the 52 cards sorted by rank first and then by suit. Ranks go 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A, 2 (the 2 is highest) and suits go Clubs, Diamonds, Hearts, Spades. The list starts Clubs-3, Diamonds-3, Hearts-3, Spades-3, Clubs-4, ....
shuffle(swaps) returns a shuffled copy of the ordered deck using the Fisher-Yates procedure, with its random choices passed in so the result is repeatable. If swaps is empty, no swaps are applied and the ordered deck is returned. Otherwise swaps has 52 entries: for i from 51 down to 1, swap the cards at positions i and swaps[i]. swaps[0] is always 0 and unused.
Each call works from the ordered deck and returns a new list; the deck itself does not change.
- Example 1
- init
[]- operations
[["order",[]]]- Output
[["Clubs-3","Diamonds-3","Hearts-3","Spades-3","Clubs-4","Diamonds-4","Hearts-4","Spades-4","Clubs-5","Diamonds-5","Hearts-5","Spades-5","Clubs-6","Diamonds-6","Hearts-6","Spades-6","Clubs-7","Diamonds-7","Hearts-7","Spades-7","Clubs-8","Diamonds-8","Hearts-8","Spades-8","Clubs-9","Diamonds-9","Hearts-9","Spades-9","Clubs-10","Diamonds-10","Hearts-10","Spades-10","Clubs-J","Diamonds-J","Hearts-J","Spades-J","Clubs-Q","Diamonds-Q","Hearts-Q","Spades-Q","Clubs-K","Diamonds-K","Hearts-K","Spades-K","Clubs-A","Diamonds-A","Hearts-A","Spades-A","Clubs-2","Diamonds-2","Hearts-2","Spades-2"]]
Why: Cards are sorted by rank first and suit second. Ranks run 3, 4, ..., 10, J, Q, K, A and then 2, so the deck starts with the four 3s (Clubs, Diamonds, Hearts, Spades) and ends with the four 2s.
Constraints
- Each rank
3,4, ...,10,J,Q,K,A,2appears in each of the 4 suits, for 52 distinct cards swapsis empty, orlen(swaps) == 52withswaps[0] == 0and0 <= swaps[i] <= i