Can someone help me understand this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

31st May 2021, 3:03 PM
Feelc
12 Answers
+ 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?
31st May 2021, 3:52 PM
Igor Makarsky
Igor Makarsky - avatar
+ 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 ;)
1st Jun 2021, 2:29 AM
visph
visph - avatar
+ 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 ^^
31st May 2021, 7:40 PM
visph
visph - avatar
+ 1
no, it can handle more: change the string passed as argument in the function call... you will see by yourself ^^
1st Jun 2021, 12:38 AM
visph
visph - avatar
+ 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)
1st Jun 2021, 12:58 AM
visph
visph - avatar
1st Jun 2021, 2:16 AM
visph
visph - avatar
+ 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
1st Jun 2021, 2:24 AM
Feelc
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"?
31st May 2021, 4:59 PM
Feelc
0
visph so basically this code can't handle more than 3 characters in a string?
1st Jun 2021, 12:34 AM
Feelc
0
visph ahh I see but it can't eliminate duplicate arrangements if I try change the argument into a string like "ccaa"
1st Jun 2021, 12:53 AM
Feelc
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
1st Jun 2021, 2:04 AM
Feelc
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.😊
1st Jun 2021, 2:47 AM
Feelc