微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Mathematica 中重复调用函数

如何解决在 Mathematica 中重复调用函数

我有以下想要转换为 Mathematica 的 Python 代码。但是我完全被困在如何在 Mathematica 中定义我 def shouldSwapdef 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[index]

if __name__ == "__main__":
    string = list("ABCA")
    n = len(string)
    findPermutations(string,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,n) and
                                   Swap[string[index],string[i]]
                   ]
                 ]
          ]
      ]

解决方法

Python 代码中有几个语法错误。有什么理由要音译它吗? WL的方法

"ABCA" // Characters // Permutations // Map[StringJoin]

{"ABCA","ABAC","ACBA","ACAB","AABC","AACB","BACA","BAAC","BCAA","CABA","CAAB","CBAA"}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。