-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_1.py
24 lines (16 loc) · 837 Bytes
/
task_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
""""
The function input is a list of strings of the form: ['hel', 'low', 'frog', 'hello', 'lohel', 'hlelo', 'leh', ]
The function must process the strings and group them according to the full anagram.
and return a list with lists of anagrams of the original list [['hel', 'leh'], ['low'], ['frog'], ['hello', 'lohel', 'hlelo']]
Further optimize the algorithm to complexity O(n)
"""
from collections import defaultdict
def my_func(data: list) -> list[list, ...]:
result = defaultdict(list)
for item in data:
result[tuple(sorted(item))].append(item)
return list(result.values())
if __name__ == '__main__':
input_data = ['hel', 'low', 'frog', 'hello', 'lohel', 'hlelo', 'leh', ]
output_data = [['hel', 'leh'], ['low'], ['frog'], ['hello', 'lohel', 'hlelo']]
assert my_func(input_data) == output_data