Search and backtracking · 7 / 15 Exercise Medium

Permutations of a word

Objective

Create a Python program that generates all the permutations (different orderings) of the letters of a word, using backtracking: choose a letter, move on and undo the choice.

Instructions

  • Read a word of 1 to 5 letters, all different.
  • Build each permutation letter by letter: at each step go through the positions of the word in order and try the letters that have not been used yet (use a list of booleans used). When you choose a letter you mark it, move on and, when coming back, unmark it.
  • Show one permutation per line, in the order they are generated, and finish with Total: t.
Input
abc
Output
abc
acb
bac
bca
cab
cba
Total: 6

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise