Python challenge 2 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 14

Python challenge 2

The next Python challenge I am making. What to do: given input list n, write a function that shows all the possible permutations in the form of a 2D-list. The challenge: Don't use powerful modules! Example: https://code.sololearn.com/cVnmWL3uOE5x/?ref=app 2D lists: https://code.sololearn.com/c1xs8n03Th96/?ref=app

27th Jul 2017, 2:16 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
10 Answers
+ 9
Nice can make it for lists?
27th Jul 2017, 2:24 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 8
Maybw for that use list(set()) but use iteration.
28th Jul 2017, 11:28 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 7
So given to list [1,1,1,1,1], there are still 120 permutations that should be returned?
27th Jul 2017, 3:11 AM
J.G.
J.G. - avatar
+ 6
@$Vengat It returns 1 or 1 list of 1s, the one passed to it? And why would it not return all possibilities/permutations? Like in your not to do code, it does.
27th Jul 2017, 2:09 PM
J.G.
J.G. - avatar
+ 6
@Baptiste No, permutations are all possibilities regardless of repeats. Combinations aren't, so if that is supposed to be the output, then it is combinations (how many distinctly different ways can something happen) and not permutations (all the possible ways something can happen). Just saying. ; )
27th Jul 2017, 9:12 PM
J.G.
J.G. - avatar
+ 5
No. It returns 1. And yeah try recursion
27th Jul 2017, 3:44 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 5
I wrote this some time ago. It is almost same to what u ask You Enter a string and all possible permutations are returned And Yes I did not use any powerful module https://code.sololearn.com/cm9kq0LPTei7/?ref=app
27th Jul 2017, 9:49 AM
Harsh Prakash Agarwal
Harsh Prakash Agarwal - avatar
+ 3
It has not a good complexity though ^^' https://code.sololearn.com/cRZupotoCP0k/?ref=app
27th Jul 2017, 12:11 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
@JG, returning [[1,1,1,1,1]] in your example is all the permutations you can do, all the 119 others are just copies of this one
27th Jul 2017, 2:31 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
Here you go. I used Heap's Algorithm recursively to do this, and added a check to ignore all duplicates if they come up based on a randomly loaded array based on user input. https://code.sololearn.com/cp3X62Tv6X11/?ref=app
30th Jul 2017, 10:05 PM
Vari93
Vari93 - avatar