【发布时间】:2022-01-09 07:37:56
【问题描述】:
我需要将其转换为递归函数。该程序基本上会打印出字符串的所有变体。
def comb(L):
for i in range(3):
for j in range(3):
for k in range(3):
# check if the indexes are not
# same
if (i!=j and j!=k and i!=k):
print(L[i], L[j], L[k])
# Driver Code
comb([1, 2, 3])
输出:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
【问题讨论】:
-
如果代码已经实现了你需要的功能,为什么还要改成递归呢?
-
可能是作业什么的
-
有什么问题?我建议阅读本指南:stackoverflow.com/help/minimal-reproducible-example
-
list(itertools.permutations([1,2,3]))有效。如果您需要递归执行此操作,请参阅How do I ask homework questions? - 尝试和具体问题是此处讨论主题的预期协议。
标签: python python-3.x recursion