0

How can I display all the permutations in a string or any other iterable in the most simplest way for a beginner...

def permutations(iterable):
       #code here

Ex. Permutations("abc")

Output: "abc","acb","bac","bca","cba","cab"

martineau
  • 119,623
  • 25
  • 170
  • 301
DevRanger
  • 9
  • 2
  • 2
    Use *recursion* – Ch3steR May 25 '20 at 19:05
  • Select each of the input's elements as the start, then use recursion to create permutations of the rest. – fafl May 25 '20 at 19:05
  • 1
    There's some "roughly equivalent" code in the [`itertools` documentation](https://docs.python.org/3/library/itertools.html#itertools.permutations) that might help. The Wikipedia article on the subject also describes some [algorithms](https://en.wikipedia.org/wiki/Permutation#Algorithms_to_generate_permutations). – martineau May 25 '20 at 19:18
  • This answers your question. https://stackoverflow.com/a/13109403/7698734 – Hassaan Ali May 25 '20 at 19:27
  • Does this answer your question? [Python recursion permutations](https://stackoverflow.com/questions/13109274/python-recursion-permutations) – Błotosmętek May 25 '20 at 19:44
  • `itertools.permutations` is the way to go here, unless you want to implement it yourself. – Alex May 26 '20 at 04:28

0 Answers0