From 61408464279302b702a4b7df1bb8f1106f88454c Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Thu, 27 Nov 2025 02:22:50 +0000 Subject: [PATCH] [Sync Iteration] python/anagram/3 --- solutions/python/anagram/3/anagram.py | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 solutions/python/anagram/3/anagram.py diff --git a/solutions/python/anagram/3/anagram.py b/solutions/python/anagram/3/anagram.py new file mode 100644 index 0000000..ff1ad11 --- /dev/null +++ b/solutions/python/anagram/3/anagram.py @@ -0,0 +1,31 @@ +""" +Anagram. + +Given a target word and one or more candidate words, +your task is to find the candidates that are anagrams +of the target. +""" + + +def find_anagrams(word: str, candidates: list[str]) -> list[str]: + """ + Return the list of candidates that are anagrams of ``word``. + + Two words are considered anagrams if they consist of the same letters + with the same multiplicities when case is ignored. The original casing of + candidates is preserved in the returned list. Exact same word (case-insensitive) + as the target is excluded. + + :param word: Target word to compare against. + :param candidates: Sequence of candidate words to test. + :returns: A list containing each candidate that is an anagram of ``word``. + """ + target_sorted: list[str] = sorted(word.lower()) + target_lower: str = word.lower() + + return [ + candidate + for candidate in candidates + if (candidate_lower := candidate.lower()) != target_lower + and sorted(candidate_lower) == target_sorted + ]