は、私はPythonで、次の「ゲーム」を実装しようとしています:最初にpythonでの幅 - 計算上の最大?
開始ワードを考えると、との目標単語を見つける修正 ステップごとの変更許可:削除したり、任意の文字を追加し、
タスクを並べ替えます最短のステップでスタートからゴールまでの道を見つけることです。
私のアプローチは、文字を追加/削除し、結果の文字を置換し、辞書内の各置換文字を検索することでした。
これはすぐに実行時間が長くなります(9文字の単語と最初のステップでは約60秒です)。
ここに私のコードです。
import time
from itertools import permutations
startword = 'croissant'
nodes = list()
nodes.append(startword)
dicts = set([line.rstrip('\n') for line in open('wordList.txt')])
alpha = set([chr(i) for i in range(ord('a'),ord('z')+1)])
def step(nodes):
nnodes = list()
for word in nodes:
for s in word:
new_word = word.replace(s, '', 1)
perms = [''.join(p) for p in permutations(new_word)]
for per in perms:
if per in dicts:
nnodes.append(per)
for s in alpha:
new_word = word + s
perms = [''.join(p) for p in permutations(new_word)]
for per in perms:
if per in dicts:
nnodes.append(per)
return set(nnodes)
btime = time.time()
step(nodes)
print time.time() - btime
どのようにパフォーマンス/ロジックを改善できますか?具体的には、幅広い最初の検索を使用するように求められます。
このコードは機能しますか?はいの場合、どのように_improve_できるかを尋ねたいだけですが、[codereview.se]に関する質問を歓迎します。 – ForceBru
はい、そうです。私の質問は、私が使用しているコンセプトについてのものでした.... – Leon