Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so dont bother with any of their useless mail servers here and just use oauth login instead. Thank the nice Russians for causing that. :)

Paste

Pasted as Python by terror_macbeth_I ( 16 years ago )
#!/usr/bin/python
# vim:fileencoding=utf-8

class Vertex(object):
    def __init__(self, depth, parent, CWR):
        self.depth = depth
        self.parent = parent
        self.CWR = CWR
        self.children = []


def create_vocabulary():
    with open('./words.txt', 'r') as f:
        vocabulary = f.read().split()
    return vocabulary


def solve_CWR(vertex, vocabulary):
    depth = vertex.depth
    if depth == 3:
        return
    elif depth == 0:
        is_valid_extension = lambda cwr, word: cwr[0][2] == word[0]
    elif depth == 1:
        is_valid_extension = lambda cwr, word: cwr[1][2] == word[2]
    elif depth == 2:
        is_valid_extension = (lambda cwr, word: cwr[0][0] == word[0] and
                                                cwr[2][0] == word[2])
    for word in vocabulary:
        if is_valid_extension(vertex.CWR, word):
            CWR = vertex.CWR[:]
            CWR.append(word)
            newchild = Vertex(depth+1, vertex, CWR)
            vertex.children.append(newchild)
            solve_CWR(newchild, vocabulary)


def print_solutions(vertex):
    if vertex.depth == 3:
        print vertex.CWR
        return
    for child in vertex.children:
        print_solutions(child)


if __name__ == '__main__':
    import random

    vocabulary = create_vocabulary()
    CWR = [vocabulary[random.randint(0, len(vocabulary)-1)]]
    root = Vertex(0, None, CWR)
    solve_CWR(root, vocabulary)
    print_solutions(root)

 

Revise this Paste

Parent: 22068
Your Name: Code Language: