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
?
-
If you generate the list of all combinations, you now have a hash table. Given a set of cards, generate the same hash and do a lookup. The trick is a repeatable way of generating the hash number from a set of cards.gbjbaanb– gbjbaanb2015年04月28日 09:02:29 +00:00Commented Apr 28, 2015 at 9:02
-
@gbjbaanb list or dictionary with indices already in place. I am looking for an algorithm.Worker– Worker2015年04月28日 09:04:12 +00:00Commented Apr 28, 2015 at 9:04
-
If you have no way of calculating a particular row in the table from your cards then you'll just have to loop through them all.gbjbaanb– gbjbaanb2015年04月28日 09:12:57 +00:00Commented Apr 28, 2015 at 9:12
1 Answer 1
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)
.
-
Thank you for great tip. I kind of stock with indexing 0,1,2.. Now I just realised that I don't need index. ints are just fine!Worker– Worker2015年04月28日 09:23:24 +00:00Commented Apr 28, 2015 at 9:23
-
Or you could multiply by 100 and you'd have an id that you can read as well.Neil– Neil2015年04月28日 09:55:21 +00:00Commented Apr 28, 2015 at 9:55
-
@Neil and then you don't have to adjust the individual cards either.ratchet freak– ratchet freak2015年04月28日 09:58:15 +00:00Commented Apr 28, 2015 at 9:58
-
The only problem that I see in that is that it leaves spaces in your "id" range.Neil– Neil2015年04月28日 10:08:40 +00:00Commented Apr 28, 2015 at 10:08
-
Technically there still is in my answer as it assumes that the order of the cards matter, if you remove that then you have a lot less options for the subsequent cards but the algorithm is more complicated.ratchet freak– ratchet freak2015年04月28日 10:22:41 +00:00Commented Apr 28, 2015 at 10:22