【发布时间】:2021-04-06 10:33:22
【问题描述】:
我正在尝试编写一些递归 python 代码来打印每个长度为 n 的二进制数。函数的输入只能是 n。到目前为止,这是我的代码:
def printAll(n):
result=[]
stringSoFar=''
def printAllrec(stringSoFar,n,result):
if n ==0:
result.append(stringSoFar)
if len(result) == (2):
return result
else:
printAllrec((stringSoFar+"0"),n-1,result)
printAllrec((stringSoFar+"1"),n-1,result)
return printAllrec(stringSoFar,n,result)
print(printAll(2))
但是,此代码一直返回“无”。我不明白为什么它不起作用。任何提示将不胜感激。
【问题讨论】:
标签: python python-3.x list recursion permutation