Method: Secretly arrange the cards in the following order..."
Here we stop citing Liberg: Trolleri för alla
(Magic for everyone).
In this lab project, you are required to find
out the secret order!
Therefore you must write a program that does the following:
Order of cards in deck: 3 1 5 2 4
The cards come out in this order:
1 2 3 4 5
isempty(). Your main program is to fulfil its task with these calls, not caring about how the queue has been implemented. Before starting with the card trick, you may try to make the following test program function.
put(1) put(2) x=get() y=get() print x,y # 1 2 should come out
Initially you can write the code for the queue in the same file
as the main program, making it obvious that the queue consists of
nodes, each containg a number and a pointer.
node may come first in the file, then the global
queue pointers, then the definitions of
put(x), get() and
isempty() and finally the main program.
It is a little bit tricky to program
put(x), since there are
two cases depending on whether the queue is empty or not. Picturing these
to cases is a great help, so please draw sketches!
When the test program works, you should modify it to solve the secret of
the card trick. An input tip is to use
Experiment with different input orders and reveal the secret!
Yor program also makes intelligent conversation. As an example, input
JAG GILLAR NÄR DU KRAMAR MEJ. Your Swedish neighbour will be
pleased to translate the sentence and its answer.
last. For example, you may want to move values from one queue to another with
q1=Queue() q2=Queue() - - - x=q1.get() q1.put(x)Let the file
class Queue:and copy all queue code and the node class. Now, every def must have a
self-parameter and instead of two global pointers you use
The main program
lab2.py should start with
from queue import Queue,
from queue import *, for the node class should
firstpoints to the bottom card and
lastto the top card. Finally, to print the deck from top to bottom you have to use the stack again. Sigh!
Unsuccessful shuffle: Card sharks always use riffle shuffle, that is they cut the pack in two and riffle the two halves together such that the top card of the lower half becomes the new top card and the bottom card of the upper half becomes the new bottom card. There is a rumour that this shuffle must not be repeated too many times or the cards will return to the original order. Is there any truth in the rumour?
In order to program this, you need three queues. Your program is to ask for the number of cards (an even number) and the number of shufflings to be performed and then print out the resulting order. Test with 6 cards and 3 shuffles or with 62 cards and 6 shuffles. How many are needed for the usual 52 card deck?