0
Can someone help me understand this code?
How this permutation method inside the for loop statement works? https://code.sololearn.com/wej6dN8oTgkC/?ref=app
12 Antworten
+ 11
The permutation method uses recursion. It calls itself if a condition is false. If the condition is true, the recursion is broken and the output is shown.
What exactly do you not understand?
+ 2
Feelc yes: you're right... I just assumed that you know how to use array_unique function, so only move it outside of the function...
but in fact, it does not modify the array in place, and rather return a copy of the array with unique values ^^
check my code: I've slightly modified it ;)
+ 1
at line 30 the recursion occur: same function is called with two arguments... initial call only require one argument (second default to empty string)
at line 24 code check if first argument is an empty string, if so, then the result is complete and will be outputed and function return (exit).without executing rest of code ^^
+ 1
no, it can handle more: change the string passed as argument in the function call... you will see by yourself ^^
+ 1
you may handle (removing) the duplicates with another function... if instead of printing you store the string in an array (you may even test if string to be added is already inside the result array and add it only if not)
+ 1
this should work:
https://code.sololearn.com/wEGnX8gd7MNw/?ref=app
+ 1
visph I tried putting "aabb" as an argument in your variant but it still returns duplicate arrangements:
aabb aabb abab abba abab abba aabb aabb abab abba abab abba baab baba baab baba bbaa bbaa baab baba baab baba bbaa bbaa
0
Igor Makarsky Hi thanks for the response. I'm still a beginner and trying to figure out how this line of code in line 30 and line 24 exactly works?
Edit: And also what is the use of the second parameter in the function declaration if the function call only passes one argument which is the "cat"?
0
visph so basically this code can't handle more than 3 characters in a string?
0
visph ahh I see but it can't eliminate duplicate arrangements if I try change the argument into a string like "ccaa"
0
visph i tried using array_unique() method instead but it doesn't seem to work, please look at my variant.
https://code.sololearn.com/wxfZ4JEd6EJ7/?ref=app
0
Yes yes that's what im trying to achieve only shows the unique values inside the array. I misused the array_unique function. Thanks you for the clarification.😊