-
Notifications
You must be signed in to change notification settings - Fork 0
/
poker_map.py
71 lines (55 loc) · 1.79 KB
/
poker_map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import string
def tuple_sort(max_hash):
max_tuple = max_hash[0]
for i in range(1, len(max_hash)):
for j in range(26):
value = max_hash[i][j]
if max_tuple[j] > value:
break
elif value > max_tuple[j]:
max_tuple = max_hash[i]
return max_tuple
def most_likely_hand(D, k):
alpha_string = string.ascii_lowercase
alpha_dict = {char:index for index, char in enumerate(alpha_string)}
freq = 26 * [0]
hand_freq = {}
hand = []
max_freq = 0
max_hash = []
hash_vals = {}
for i in range(len(D)):
if i == 0:
for j in range(k):
freq[alpha_dict[D[j]]] += 1
else:
freq[alpha_dict[D[i-1]]] += -1
freq[alpha_dict[D[(i+k-1) % len(D)]]] += 1
freq_tuple = ()
for i in freq:
freq_tuple += (i,)
hash_value = hash(freq_tuple)
hand.append(hash_value)
if hash_value in hand_freq:
hand_freq[hash_value] += 1
if hand_freq[hash_value] > max_freq:
max_freq = hand_freq[hash_value]
max_hash = [freq_tuple]
elif hand_freq[hash_value] == max_freq:
max_hash.append(freq_tuple)
else:
hand_freq[hash_value] = 1
if hand_freq[hash_value] > max_freq:
max_freq = hand_freq[hash_value]
max_hash = [freq_tuple]
elif hand_freq[hash_value] == max_freq:
max_hash.append(freq_tuple)
hash_vals[hash_value] = freq_tuple
max_tuple = tuple_sort(max_hash)
hand = ""
for i in range(len(max_tuple)):
value = max_tuple[i]
while value > 0:
hand += alpha_string[i]
value += -1
return hand