This question has been bugging me for a while now. There are approximately 8e67 unique permutations of a standard, 52 card deck. This theoretically equates to just slightly more than 225 bits of data ($\log_2(52!)$).
So the question is; is it possible to design an algorithm to convert between a sequence of 52 cards and a 226 bit string such that every unique string is a unique number?
Ideally, the algorithm would be reversible. One should be able to go from a deck of cards to a string of data, or from a string of data to a deck of cards.
I wonder, also, if such an algorithm could run in linear time.
Something I thought of, though wasn't able to do anything with, was the notion that all of the cards in order would represent the lowest number, and all of the cards in reverse order would represent the highest number.
1,2,3 -> 0
3,2,1 -> 5
This did help a bit, because it meant I could do things like reverse the 1 and 2 to make the number one. 2,1,3 -> 1 And it wouldn't matter how many numbers come after a reversed sequence. 3,2,1 -> 5 as well as 3,2,1,4,5,6,7,8 -> 5
It gave me some rules as well. Given any two permutations that are nearly identical but differ only by two cards, the set with the higher card first would resolve to a higher number. I.E. 1,2,3,4,5 < 1,2,4,3,5 and 2,1,3 < 3,1,2
For two cards, the table is easy
1,2 -> 0
2,1 -> 1
For three cards there are sequences that obviously fall in line.
1,2,3 -> 0
2,1,3 -> 1
3,2,1 -> 5
After a few days of thinking about this, I figured it out. I was on the right track about the reverse order thing, but I was letting it hold me back, especially when trying to make it fit into linear time. What I should have been thinking about was how the movement of these cards could create a recursive sequence.
I think the easiest way to demonstrate what I've come up with is with some examples.
First, a deck of 1 card. There is only one possibility, so the i/o of the function would look like this:
1 -> 0.This, of course, is still identical to
1,2,3,4,5,6,7... -> 0. As long as the sequence is in order, the output is zero.So now we add a second card. With two cards there are two possible permutations of our deck.
1,2 -> 0and2,1 -> 1What is significant about the second set is not that the sequence is reversed, but that the two moved to the left one space. The way we calculate the output of this iteration is we get the factorial of the number below the highest digit, and multiply that by how many spaces it is to the left. In our case, for
2,1, that's $1! * 1$.Here's all of the possible sequences for a deck of three cards:
Notice that the ordering of cards 1 and 2 repeat in a pattern as the 3 card moves its way to the beginning.
Here's an even more verbose example showing 4 cards:
Decoding
Let's calculate the sequence
3,4,1,2which we know is16.$T$ = accumulating total. $n$ = card number. $d$ = delta, or position from the right.
Find the position of the 4 card from the right. In this case it's moved by 2. So, $d = 2$
Add to $T$ such that $T' = T + d * (n-1)!$
Remove the 4 card from the list
Repeat until there is only one card left
Here's some pseudocode:
Encoding
I have not created an encoder yet, though I imagine it would be similar to the algorithm above, but in reverse. I will update this if or when I create one.