나는 파이썬에서 다음 "게임"을 구현하기 위해 노력하고있어 :우선 파이썬에서의 계산 - 최대?
시작 단어를 감안할 때,에 성공 단어를 찾아 수정 단계에 따라 변경 허용 : 제거하거나 편지를 추가하고
작업을 뒤 바꿔 최소한의 단계로 시작부터 끝까지 길을 찾는 것입니다.
내 접근 방식은 글자를 추가/제거하고, 결과 글자를 바꾸고, 사전에서 각 순열을 찾는다.
이렇게하면 빠르게 실행 시간이 길어집니다 (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