I have a deck of 52 poker cards represented as array of int – [0, 1, ... 51]
.
There are 2.598.960 possible combinations of 5 cards. I can generate all combinations like this:
#id #combination
0 - 0, 1, 2, 3, 4
1 - 0, 1, 2, 3, 5
2 - 0, 1, 2, 3, 6
...omitted data..
2598957 - 46, 47, 49, 50, 51
2598958 - 46, 48, 49, 50, 51
2598959 - 47, 48, 49, 50, 51
How I can effectively find #id
for given #combination
and #combination
for given #id
?
3
For the first card you have 52 options for the second you have 51 and so on. You need to encode a sequence c1
, c2
, c3
, c4
and c5
where there are 52, 51, 50 ,49, 48 options resp.
You can adjust the numbers by counting how many of the previous numbers are smaller: a5 = c5 - sum(x < c5 for x in [c1, c2, c3, c4])
you can simply encode it as a single integer output =(((a1*51 + a2)*50 + a3)*49 + a4)*48 + a5
.
you can extract it by using the modulo operation and integer divide.
int input = //...
a5 = input%48;
input/=48;
a4 = input%49;
input/=49;
a3 = input%50;
input/=50;
a2 = input%51;
input/=51;
a1 = input;
And then the reverse operation is needed to adjust them all back into the range [0..52)
.
6