递归回溯无返回值python



完全问题出现https://www.hackerrank.com/challenges/password-cracker/我想知道我的递归回溯实现出了什么问题

问题:给定一组密码,返回";密码错误"如果单词不是这些密码的组合

我想问一下如何从中返回值;我可以打印解决方案,但不能以字符串形式返回。我不知道在这里我能做什么;当单词==''时,我尝试返回一个值,但不起作用

def crackhelper(passwords,word,sol):
#Check if theres some password that currently works
print(sol)
for password in passwords:
if word[:len(password)]==password:
sol+=password
crackhelper(passwords,word[len(password):],sol)
sol=sol[:-len(password)]
return ''
def crack():
word="wedowhatwemustbecausewecane"
passwords="because can do must we what cane".split(' ')
j=crackhelper(passwords,word,'')    
print(j)
#print(passwords)
def crack_helper(passwords, word, sol):
# Check if there is some password that currently works.
if word ==  "":
return sol
for password in passwords:
if word[:len(password)] == password:
sol += password
s = crack_helper(passwords, word[len(password):], sol)
if s != "No Result":
sol = s
return sol
sol = sol[:-len(password)]
return "No Result"

这应该做那项工作:(

最新更新