我目前正试图深入挖掘python,并且我发现了一个对(hackthissite.org)的挑战,我试图破解它。我必须解读在提供的单词表中找到的10个单词。解密Python字解密器
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()
功能排列看起来像是实际解密的代码块。你能帮我理解它在编程上如何解决这个问题吗?
您的缩进被打破。 – SilentGhost 2009-10-28 15:23:12
是的,要正确地粘贴python是很困难的。 – adam 2009-10-28 15:25:28
如果您将'permutation'函数识别为正在执行实际工作的函数,您不能单独运行它并查看它返回的结果吗? – SilentGhost 2009-10-28 15:28:34