Standard library · 12 / 17 Exercise Medium

Anagrams

Objective

Create a Python program that shows all the distinct anagrams of a word using itertools.permutations.

Instructions

  • Import permutations from the itertools module.
  • Read a lower-case word of at most 6 letters.
  • Generate all the permutations of its letters, join them back into words ("".join(p)) and store them in a set to remove the repeated ones.
  • On the first line show Anagrams: K, with the number of distinct anagrams.
  • Then show the anagrams sorted alphabetically, one per line.
Input
abc
Output
Anagrams: 6
abc
acb
bac
bca
cab
cba

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

Start exercise