【发布时间】:2021-03-06 03:17:03
【问题描述】:
我有以下 python 代码,我想将其转换为 Mathematica。但是我完全不知道如何在 Mathematica 中定义我的函数 def shouldSwap 和 def findPermutations,然后 调用 findPermutations 以获取索引+ 1。如何在 Mathematica 中定义此类函数?非常感谢您的帮助!
我的python代码:
def shouldSwap(string, start, curr):
for j in range(strat, curr):
if string[j] == string[curr]:
return 0
return 1
def findPermutations(string, index, n):
if index >= n:
print(''.join(String))
return
for i in range(index, n):
check = shouldSwap(string index, i)
if check:
string[index], string[i] = string[i], string[index]
findPermutations(string, index + 1, n)
string[index], string[i] = string[i], string[index]
if __name__ == "__main__":
string = list("ABCA")
n = len(string)
findPermutations(string, 0, n)
我将其转换为 Mathematica 的尝试失败了:
string = List[A, B, C, A]
n = Length[string]
index = 0;
If[index>=n, string, Return]
If[index<n,
For[i = index, i<n, i+1,
If[
For[j = index, j<i, j++,
If[string[j] == string[i], Return,
Swap[string[index], string[i]] and
How do I do this??? findPermutations(string, index + 1, n) and
Swap[string[index], string[i]]
]
]
]
]
【问题讨论】:
标签: wolfram-mathematica mathematical-expressions