simplemente podría utilizar recursiva posterior de seguimiento, pero esto está lejos de ser la solución más óptima.
# Given two words of equal length that are in a dictionary, write a method to transform one word into another word by changing only
# one letter at a time. The new word you get in each step must be in the
# dictionary.
# def transform(english_words, start, end):
# transform(english_words, 'damp', 'like')
# ['damp', 'lamp', 'limp', 'lime', 'like']
# ['damp', 'camp', 'came', 'lame', 'lime', 'like']
def is_diff_one(str1, str2):
if len(str1) != len(str2):
return False
count = 0
for i in range(0, len(str1)):
if str1[i] != str2[i]:
count = count + 1
if count == 1:
return True
return False
potential_ans = []
def transform(english_words, start, end, count):
global potential_ans
if count == 0:
count = count + 1
potential_ans = [start]
if start == end:
print potential_ans
return potential_ans
for w in english_words:
if is_diff_one(w, start) and w not in potential_ans:
potential_ans.append(w)
transform(english_words, w, end, count)
potential_ans[:-1]
return None
english_words = set(['damp', 'camp', 'came', 'lame', 'lime', 'like'])
transform(english_words, 'damp', 'lame', 0)
Tales gráficos se utilizan realmente más en el Wikcionario ruso, ver http://ru.wiktionary.org/w/ index.php? title =% D0% A1% D0% BB% D1% 83% D0% B6% D0% B5% D0% B1% D0% BD% D0% B0% D1% 8F% 3ESearch & ns6 = 1 & search =% D0% BC% D0% B5% D1% 82% D0% B0% D0% B3% D1% 80% D0% B0% D0% BC% D0% BC y texto completo =% D0% A0% D0% B0% D1% 81% D1% 88 % D0% B8% D1% 80% D0% B5% D0% BD% D0% BD% D1% 8B% D0% B9 +% D0% BF% D0% BE% D0% B8% D1% 81% D0% BA o http : //www.aisee.com/graph_of_the_month/words.htm –
@RegDwight: Gracias :) – codaddict
Esto es exactamente lo que tenía en mente. Estaba pensando más en términos de complejidad de espacio y tiempo. – Srikanth