blob: 131d7f53330a0594405599ff84f1438a6d8e4948 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This program prints all anagrams of a given word.
Input:
word - a string representing the word to find anagrams of
Output:
A list of all anagrams of the given word
"""
import itertools
def get_anagrams(word: str) -> set:
"""
This function takes a word and finds all anagrams of it
Input:
word - a string representing the word to find anagrams of
Output:
A list of all anagrams of the given word
"""
# generate all permutations of the given word
permutations = itertools.permutations(word)
# convert the permutations to strings
anagrams = ["".join(p) for p in permutations]
# remove duplicates
anagrams = list(set(anagrams))
return set(anagrams)
# tests
assert get_anagrams("cat") == {"tca", "tac", "act", "atc", "cta", "cat"}
|