One way to shuffle playing cards is to use a perfect shuffle. First, you divide a deck of 52 cards into two halves of 26 cards each. Next, you merge the halves by interleaving the cards as follows....


One way to shuffle playing cards is to use a perfect shuffle. First, you divide a deck of 52 cards into two halves of 26 cards each. Next, you merge the halves by interleaving the cards as follows. Beginning with the top half and alternating halves, you take the bottom card from a half and place it on top of a new deck. For example, if our deck contains the six cards 1 2 3 4 5 6, the top half is 1 2 3, and the bottom half is 4 5 6. The 3 at the bottom of the top half becomes the bottom card in the shuffled deck. We then place the 6, which is at the bottom of the bottom half, on top of the shuffled deck. Next, we place 2 on top, then 5, 1, and finally 4. The shuffled deck is then 4 1 5 2 6 3. Notice that the card that was on top of the original deck is now second in the shuffled result, and the bottom card in the original deck is now second from the bottom in the shuffled deck. This shuffle is called an in-shuffle and is achieved by beginning with the top half when you move cards into the shuffled result. If you begin with the bottom half, you get an out-shuffle, whereby the original top card and bottom card remain in their positions in the shuffled deck. Define a class of playing-card decks by using a deque to contain the cards. Your class should define methods to perform perfect in-shuffles and perfect out-shuffles. Using your class,


a. How many perfect out-shuffles are needed to return a deck of n cards to its original order?


 b. How many perfect in-shuffles are needed to return a deck of n cards to its original order?


c. You can move a deck’s top card, which is at position 0, to any desired position m by performing a sequence of in-shuffles and out-shuffles, as follows. You write m in binary. Beginning with the leftmost 1 and proceeding to the right, you perform an in-shuffle for each 1 encountered and an out-shuffle for each 0. For example, if m is 8, we have 1000 for its binary equivalent. We would perform one in-shuffle followed by three out-shuffles to move the original top card to position 8, that is, so it is the ninth card from the top of the deck. Define a method to perform this card trick.

Nov 13, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here