aboutsummaryrefslogtreecommitdiff
path: root/challenge-094/lubos-kolouch/python/ch-1.py
blob: 6324f749343f5b7f88090f44d6750f4d94c23f59 (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
#!env python
"""
# ===============================================================================
#
#         FILE: ch-1.py
#
#        USAGE: ./ch-1.py
#
#  DESCRIPTION: Perl Weekly Challenge 094
#               Task 1
#               Group Anagrams
#
#       AUTHOR: Lubos Kolouch
#      VERSION: 1.0
#      CREATED: 01/06/2021 08:43:01 PM
# ===============================================================================
"""
from collections import defaultdict


def get_anagrams(what):
    """ Sort and group """

    group = defaultdict(list)

    for elem in what:
        joined = ''.join(sorted(elem))
        group[joined].append(elem)

    return list(group.values())


def test_case1():
    """ Test case 1 from the web """
    result = get_anagrams(["opt", "bat", "saw", "tab", "pot", "top", "was"])
    assert result == [['opt', 'pot', 'top'], ['bat', 'tab'], ['saw', 'was']]


def test_case2():
    """ Test case 2 from the web """
    result = get_anagrams(["x"])
    assert result == [['x']]