Reputation: 11
I have the follwoing python code that I would like to convert to Mathematica. But I totally get stuck on how to deifne the functions I def shouldSwap and def findPermutations in Mathematica and then call on findPermutations for index + 1. How do I define such functions in Mathematica? Thank you very much for your help!
My python code:
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)
My hopelessly failed attempt to convert it to 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]]
]
]
]
]
Upvotes: 1
Views: 174
Reputation: 679
There are several syntax errors in the Python code. Is there a reason for wanting to transliterate it? The WL way to do it
"ABCA" // Characters // Permutations // Map[StringJoin]
{"ABCA", "ABAC", "ACBA", "ACAB", "AABC", "AACB", "BACA", "BAAC", "BCAA", "CABA", "CAAB", "CBAA"}
Upvotes: 0