コード例 #1
0
ファイル: super-scramble.py プロジェクト: bashwork/common
def get_clean_word_list():
    ''' Retrieve a clean word list that only has words matching
    the supplied letter set.

    :returns: The cleaned word list
    '''
    words = Words.get_word_list_lazy()
    return Words.generate_anagram_lookup(words)
コード例 #2
0
ファイル: edit_distance.py プロジェクト: bashwork/common
import sys
from bashwork.utility.lookup import Words

def edit_distance(this, that):
    n, m = len(this) - 1, len(that) - 1
    edits = {}
    for i in range(n + 1): edits[i,0] = i
    for j in range(m + 1): edits[0,j] = j
    for i in range(1, n + 1):
        for j in range(1, m + 1):
            d = 0 if this[i] == that[j] else 1
            edits[i,j] = min(1 + edits[i - 1, j], 1 + edits[i, j - 1], d + edits[i - 1, j - 1])
    return edits[n,m]

if __name__ == "__main__":
    dictionary = Words.get_word_list()
    this = 'evantualie'
    words, value = [], sys.maxint
    for w in dictionary:
        v = edit_distance(this, w)
        if v < value: value, words = v, [w]
        elif  v == value: words.append(w)
    print words
コード例 #3
0
ファイル: word-transform.py プロジェクト: bashwork/common
        if current[-1] == end:
            yield current

        for word in lookup[current[-1]]:
            if word not in current:
                queue.append(current + [word])

def print_solution(solution):
    ''' Given the original problem and a solution, print
    them out.

    :param solution: The final solution to the problem
    '''
    print " → ".join(solution)

# ------------------------------------------------------------
# constants
# ------------------------------------------------------------
WORDS   = set(Words.get_word_list())
#LOOKUP  = Words.generate_single_letter_lookup(WORDS)
import cPickle as pickle
LOOKUP  = pickle.load(open('/tmp/lookup.pickle'))
PROBLEM = ("lose", "gain") # lost last lait gait gain
PROBLEM = ("cold", "warm") # cold → wold → word → ward → warm

if __name__ == "__main__":
    start, end = PROBLEM
    for solution in get_solutions(start, end, LOOKUP):
        print_solution(solution)
コード例 #4
0
ファイル: drink-encoding.py プロジェクト: bashwork/common
    while queue:
        word = queue.pop()
        if word in drinks:
            yield word
        else:
            for letter in letters[drink[len(word)]]:
                path = word + letter
                if drinks.has_path(path):
                    queue.insert(0, path)

# ------------------------------------------------------------
# constants
# ------------------------------------------------------------

WORDS      = set(Words.get_word_list('drinks'))
DICTIONARY = Trie.create(WORDS)
LETTERS    = {
    1: 'bdfhklt',
    2: 'cmnrsvwxz',
    3: 'aeiou',
    4: 'gjpq',
    5: 'y',
}
DRINKS     = [
    [(1,3,4,3,3,1,3), (2,3,2,2,3,2,3)],
    [(1,1,3,3,1,5), (2,3,2,5)],
    [(4,3,2), (3,2,1), (1,3,2,3,2)],
    [(4,3,2,3), (2,3,1,3,1,3)],
    [(2,3,2,1), (4,3,1,3,4)],
]