-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex_words.py
32 lines (26 loc) · 842 Bytes
/
hex_words.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
import string
def to_int(hex_str):
return int(hex_str, 16)
HEX_CHARS = set(string.lowercase[0:6])
def hex_word(word):
for c in word.lower():
if c not in HEX_CHARS:
return False
return True
WORD_FILE = '/usr/share/dict/words'
def generate_word_list():
"""
Generate a list of words that can be encoded,
"""
with open(WORD_FILE) as f:
words = f.readlines()
# need to chop off the new lines at the end
hex_words = [word.lower()[:-1] for word in words if hex_word(word[:-1])]
return hex_words
def find_largest(word_list):
word_lengths = [(word, to_int(word)) for word in word_list]
sorted_lengths = sorted(word_lengths, key=lambda x: x[1])
largest = sorted_lengths[-1][0]
print largest
word_list = generate_word_list()
find_largest(word_list)