My favorites | Sign in
Project Logo
                
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/env python

# Copyright (c) 2008 Gregory Andrew Robinson
# gregarobi@dev.java.net
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

from string import maketrans

class ComboItem:
def __init__(self, combo, words):
self.combo = combo
self.words = words

class RankItem:
def __init__(self, rank, comboitems):
assert isinstance(comboitems[0], ComboItem)
self.rank = rank
self.combos = comboitems

def chardelta(word1, word2):
delta = 0
for i in xrange(len(word1)):
if word1[i] == word2[i]:
delta += 1
return delta

class Challenge:

def __init__(self):
self.words = {}
self.numcombos = {}
self.makecombolist = lambda : [ ComboItem(combo, words) for (combo, words) in self.numcombos.iteritems()]
self.is_not_unique = lambda comboitem : len(comboitem.words) > 1
letters = 'abcdefghijklmnopqrstuvwxyz'
numbers = '22233344455566677778889999'
transtable = maketrans(letters, numbers)
self.getnumbers = lambda word : word.translate(transtable)

def diff2chars(self, comboitem):
words = comboitem.words[:]
goodwords = []
while len(words) > 0:
word1 = words.pop()
simmatch = False
for j, word2 in enumerate(words):
if chardelta(word1, word2) > 1:
words.pop(j)
simmatch = True
if not simmatch:
goodwords.append(word1)
return ComboItem(comboitem.combo, goodwords)


def greatest(self, a, b):
if a.rank > b.rank:
return a
elif a.rank < b.rank:
return b
else:
# Combine combos of similar rank
return RankItem( a.rank, a.combos + b.combos )

def addwords(self, wordlist):
for line in wordlist:
line = line.strip()
if line.isalpha():
word = line.lower()
numcombo = self.getnumbers(word)

# Add the word to the words dictionary
## self.words.update( {word : getnumbers(word)} )

# Create a list to add to the combos dictionary
entry = [word]
# Add any existing words to the entry
if numcombo in self.numcombos:
entry += self.numcombos[numcombo]
# Replace or add the combo's dictionary entry
self.numcombos.update( {numcombo : entry} )
self.combolist = self.makecombolist()

def getlongest(self):
# Map to word lengths
mapwordlen = lambda comboitem : RankItem( len(comboitem.combo), [comboitem])
return reduce(self.greatest,
map(mapwordlen,
filter(self.is_not_unique,
map(self.diff2chars,
filter(self.is_not_unique, self.combolist[:])))))

# A programmer's most-needed function
# But really it returns the combos with the most
def getpopular(self):
# Map to get lengths
mappopular = lambda comboitem : RankItem( len(comboitem.words), [comboitem])
return reduce(self.greatest,
map(mappopular,
filter(self.is_not_unique, self.combolist[:])))

def main():
t = Challenge()
wordlist = open('words')

# Debugging line
# wordlist = ['aa', 'bb', 'cc', 'd', 'e', 'qqqqq', 'rrrrr', 'qqqqqq', 'qqqqqr']

t.addwords(wordlist)

longest = t.getlongest()
print 'longest:'
for combo in longest.combos:
print ' ', combo.combo
for word in combo.words:
print ' ', word
popular = t.getpopular()
print 'popular'
for combo in popular.combos:
print ' ', combo.combo
for word in combo.words:
print ' ', word

if __name__ == '__main__':
main()
Show details Hide details

Change log

r4 by softwarecommie on May 19, 2008   Diff
Small performance optimization.
Go to: 
Project members, sign in to write a code review

Older revisions

r3 by softwarecommie on May 18, 2008   Diff
Made everthing far more object-based
and Python-ey
r2 by softwarecommie on May 18, 2008   Diff
Adding initial script and word list
All revisions of this file

File info

Size: 4484 bytes, 143 lines
Hosted by Google Code