I saw this Puzzling problem and thought I would try to write a Python program to solve it. The task is to transform "four" to "five", forming a new four-letter word at each step, replacing one letter at each step, in as few steps as possible.

But turns out I don't know how to optimize recursion, so I'm posting here for help. I'm mostly just confused on why the code to change the past needs to be at the top of the function, but I would also like advice on how to speed this up in general. Right now it takes about 10x as long for each step up max_depth gets on my computer.

There won't be any matches until you change max_depth - I didn't want anyone copy-pasting and lagging out. There should be a solution at depth 5, according to Puzzling. However, my words file doesn't have the Foud or the word Fous, which that answer uses. Bumping up to max_depth six will take my computer ~10 minutes, which I don't want to try yet.

def hamming(string1, string2):
    assert len(string1) == len(string2)
    return sum(char1 != char2 for char1, char2 in zip(string1, string2))

max_depth = 3
start_word = "five"
end_word = "four"
all_words = open("/usr/share/dict/words", "r").read().lower().splitlines()
all_words = list(filter(lambda word: word.isalpha(), all_words))
all_words = list(filter(lambda word: len(word) == len(start_word), all_words))

sequences = []

def search(current_word, past = []):
    # Needs to be first to be fast for some reason
    past = past[:]

    if len(past) > max_depth:

    for word in all_words:
        if hamming(word, current_word) == 1 and word not in past:
            search(word, past)


sequences = [sequence[:sequence.index(end_word) + 1] for sequence in sequences if end_word in sequence]

if len(sequences) == 0:
    print("No matches")
    print(min(sequences, key=len))


1 Answer 1


By performing recursion, you are performing a depth-first search of four-letter words. However, this task involves finding a shortest path, and shortest-path problems are generally better done using breadth-first search. With BFS, the first solution you encounter will be an optimal solution — which is not the case with DFS.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.