Cutting and Riffling a Deck
Implement a class CardDeck for a face-down stack of numbered cards. Write these methods:
CardDeck(n)makes a deck of the cards1ton, with card1on top.draw()removes the top card and returns it, or returns-1if the deck is empty.cut(k)moves the topkcards to the bottom, keeping their order. Returns the new top card, or-1if the deck is empty.kis never more than the number of cards.riffle()splits the deck into a top half and a bottom half, where the top half gets the extra card when the count is odd, and interleaves them: the first card of the top half, then the first card of the bottom half, then the second of each, and so on. Returns nothing.put_bottom(card)putscardunder the deck and returns how many cards the deck has now.list_cards()returns the cards from top to bottom.
The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
CardDeck(5)
riffle()
list_cards() returns [1,4,2,5,3]
cut(2) returns 2
draw() returns 2
put_bottom(9) returns 5
list_cards() returns [5,3,1,4,9]
With five cards the top half is 1 2 3 and the bottom half is 4 5, so the riffle gives 1 4 2 5 3. Cutting two cards moves 1 4 under the rest, and the drawn 2 is replaced at the bottom by 9.
Constraints
0 <= n <= 200 <= k <=the number of cards in the deck1 <= card <= 99- Each test makes
1to30method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.