rg3 / letras

Program to solve the language problems from the Spanish TV show "Cifras y Letras"

rg3 (author)
Sat Apr 11 06:30:25 -0700 2009
letras / letras.py.in
100755 66 lines (55 sloc) 1.721 kb
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
#!@PYTHON_EXECUTABLE@
import anydbm
import sys
import shelve
 
terminal_charset = 'utf-8'
dbname = '@CMAKE_INSTALL_PREFIX@/@PROJECT_DATA_DIR@/palabras.db'
 
# Print matching words in database
def print_words(db, unicode_key):
key = unicode_key.encode('utf-8')
try:
values = db[key]
for word in values:
uword = word.decode('utf-8')
print '%s (%s)' % (uword, len(uword))
except KeyError:
pass
 
# Builds combinations of a minimum length and run action on each of them
def build_combinations(elements, min_len, action):
if len(elements) < min_len:
return
 
all = [elements] # Stores all combinations
latest = set([elements]) # Stores combinations from last step
 
# Iterate obtaining new combinations derived from the previous ones
for x in xrange(len(elements) - min_len):
tmp = set()
for comb in latest:
tmp.update(derived_combs(comb))
latest = tmp
all.extend(latest)
 
# Run action on each combination
for comb in all:
action(comb)
 
# Remove each element from the combination to get smaller combinations
def derived_combs(comb):
result = set()
for x in xrange(len(comb)):
result.add(comb[:x] + comb[(x+1):])
return result
 
# Strings are inmutable, so we need a function to sort their characters.
def sort_string(chars):
seq = list(chars)
seq.sort()
return u''.join(seq)
 
# Main program
try:
if len(sys.argv) > 3:
raise IndexError
letters = sort_string(sys.argv[1].decode(terminal_charset))
min_length = int(sys.argv[2])
db = shelve.open(dbname, flag='r')
except (IndexError, ValueError):
sys.exit('Usage: %s letters min_length' % sys.argv[0])
except anydbm.error:
sys.exit('Error opening %s' % dbname)
 
build_combinations(letters, min_length, lambda x: print_words(db, x))