Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions strings/anagrams.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@


def signature(word: str) -> str:
"""Return a word sorted
"""
Return a word's frequency-based signature.

>>> signature("test")
'estt'
'e1s1t2'
>>> signature("this is a test")
' aehiisssttt'
' 3a1e1h1i2s3t3'
>>> signature("finaltest")
'aefilnstt'
'a1e1f1i1l1n1s1t2'
"""
return "".join(sorted(word))
frequencies = collections.Counter(word)
return "".join(
f"{char}{frequency}" for char, frequency in sorted(frequencies.items())
)


def anagram(my_word: str) -> list[str]:
"""Return every anagram of the given word
"""
Return every anagram of the given word from the dictionary.

>>> anagram('test')
['sett', 'stet', 'test']
>>> anagram('this is a test')
Expand All @@ -40,5 +47,5 @@ def anagram(my_word: str) -> list[str]:
all_anagrams = {word: anagram(word) for word in word_list if len(anagram(word)) > 1}

with open("anagrams.txt", "w") as file:
file.write("all_anagrams = \n ")
file.write("all_anagrams = \n")
file.write(pprint.pformat(all_anagrams))