저는 현재 파이썬에 대해 깊이 파고 드는 중이고, 나는 (hackthissite.org)에서 내가 깰려고 노력하고있는 것을 발견했습니다. 제공된 단어 목록에있는 10 개의 단어를 해독해야합니다.파이썬 단어 해석기 암호 해독
def permutation(s):
if s == "":
return [s]
else:
ans = []
for an in permutation(s[1:]):
for pos in range(len(an)+1):
ans.append(an[:pos]+s[0]+an[pos:])
return ans
def dictionary(wordlist):
dict = {}
infile = open(wordlist, "r")
for line in infile:
word = line.split("\n")[0]
# all words in lower case!!!
word = word.lower()
dict[word] = 1
infile.close()
return dict
def main():
diction = dictionary("wordlist.txt")
# enter all the words that fit on a line or limit the number
anagram = raw_input("Please enter space separated words you need to unscramble: ")
wordLst = anagram.split(None)
for word in wordLst:
anaLst = permutation(word)
for ana in anaLst:
if diction.has_key(ana):
diction[ana] = word
#print "The solution to the jumble is" , ana
solutionLst = []
for k, v in diction.iteritems():
if v != 1:
solutionLst.append(k)
print "%s unscrambled = %s" % (v, k)
print solutionLst
main()
함수 순열은 실제로 해독을 수행하는 코드 블록 인 것처럼 보입니다. 이 문제를 프로그래밍 방식으로 해결하는 방법을 이해하도록 도와 줄 수 있습니까?
Load the word list (dictionary)
Input the words to unscramble
For each word:
Find every permutation of letters in that word (permutation)
For each permutation:
Add this permutation to the solution list if it exists in the dictionary
Print the solutions that were found.
사전() 함수는 파일에서 단어 목록을 채우는된다
들여 쓰기가 깨졌습니다. – SilentGhost
그래, 파이썬을 올바르게 붙여 넣기 란 다소 힘든 일이었다. – adam
만약 당신이'permutation' 함수를 실제 작업을하는 것으로 식별했다면, 그것을 혼자서 실행할 수 없으며 리턴하는 것을 볼 수 없습니까? – SilentGhost