This is a general challenge, opened to all.
Recently, I saw code I wrote when I was a kid that shuffled a regular deck of cards. I made no attempt to randomize the process (give me a break, I was 11), so the shuffle is entirely predictable. My solution, at the time, was the following:
1)Divide the original deck into halves
2)Insert the top half into odd positions of a copy.
3)Insert the bottom half into even positions of a copy.
4)Repeat less than 26 times (otherwise, you reverse the order of the deck)
The challenge: Can you come up with a better/shorter/(more elegant) solution?
Instructions:
Submit code, pseudo-code, or no code. Your choice. What's important is your algorithm. However, if you choose to submit code, just write a function with the following declaration: void shuffle(string* card);
There is no time constraint. Originally, I had 5-10 minutes to write the code. But since it mimicked a real world process I was already familiar with and it was unrandomized, it was relatively easy to code.
Have fun guys!
- Ray
Just for fun,the following is the code I wrote back then:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
void shuffle(string* card, int TRIALS){
for (int run=0; run<TRIALS; run++){
int half = 0;
string copy[52];
for (int i=0; i<52; i++){
if(i%2) { //even slots
copy[i] = card[half];
half++;
}
else{ //odd slots
half = i/2;
copy[i] = card[half+26];
}
}
//Update Card Array
for (int i=0; i<52; i++)
card[i] = copy[i];
}
}
|