J'essaye actuellement de creuser profondément dans python et j'ai trouvé un défi sur (hackthissite.org) que j'essaye de casser. Je dois déchiffrer 10 mots qui se trouvent dans la liste de mots fournie.Déchiffrer le mot Python désembrouilleur
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()
La permutation de fonction semble être le bloc de code qui effectue le déchiffrement. Pouvez-vous m'aider à comprendre comment cela résout ce problème par programme?
vos indentations sont brisées. – SilentGhost
Ouais, c'était un peu difficile d'obtenir le python à coller correctement. – adam
Si vous avez identifié la fonction «permutation» comme faisant le travail actuel, ne pouvez-vous pas l'exécuter seule et voir ce qu'elle renvoie? – SilentGhost