From ea528b3a4f9f371f274a0c9c3012f49cb5e2bed0 Mon Sep 17 00:00:00 2001 From: Mohammad Sajid Anwar Date: Wed, 5 Feb 2025 11:08:16 +0000 Subject: - Added solutions by Steven Wilson. --- challenge-307/steven-wilson/python/ch-02.py | 39 ----------------------------- challenge-307/steven-wilson/python/ch-2.py | 39 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 39 deletions(-) delete mode 100644 challenge-307/steven-wilson/python/ch-02.py create mode 100644 challenge-307/steven-wilson/python/ch-2.py diff --git a/challenge-307/steven-wilson/python/ch-02.py b/challenge-307/steven-wilson/python/ch-02.py deleted file mode 100644 index f910e3bf29..0000000000 --- a/challenge-307/steven-wilson/python/ch-02.py +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env python3 - - -def find_anagrams(*words): - """ Given a list of words, find any two consecutive words and if they are - anagrams, drop the first word and keep the second. You continue this until - there is no more anagrams in the given list and return the count of final - list. - >>> find_anagrams("acca", "dog", "god", "perl", "repl") - 3 - >>> find_anagrams("abba", "baba", "aabb", "ab", "ab") - 2 - """ - length_words = len(words) - drop = 0 - - if length_words < 2: - raise ValueError("At least 2 arguments are required") - - for n, _ in enumerate(words): - if n < length_words - 1 and are_anagrams(words[n], words[n+1]): - drop += 1 - return length_words - drop - - -def are_anagrams(first, second): - """ Are two words an anagram of each other - >>> are_anagrams("dog", "god") - True - >>> are_anagrams("aabb", "ab") - False - """ - return sorted(first) == sorted(second) - - -if __name__ == "__main__": - import doctest - - doctest.testmod(verbose=True) diff --git a/challenge-307/steven-wilson/python/ch-2.py b/challenge-307/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..f910e3bf29 --- /dev/null +++ b/challenge-307/steven-wilson/python/ch-2.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + + +def find_anagrams(*words): + """ Given a list of words, find any two consecutive words and if they are + anagrams, drop the first word and keep the second. You continue this until + there is no more anagrams in the given list and return the count of final + list. + >>> find_anagrams("acca", "dog", "god", "perl", "repl") + 3 + >>> find_anagrams("abba", "baba", "aabb", "ab", "ab") + 2 + """ + length_words = len(words) + drop = 0 + + if length_words < 2: + raise ValueError("At least 2 arguments are required") + + for n, _ in enumerate(words): + if n < length_words - 1 and are_anagrams(words[n], words[n+1]): + drop += 1 + return length_words - drop + + +def are_anagrams(first, second): + """ Are two words an anagram of each other + >>> are_anagrams("dog", "god") + True + >>> are_anagrams("aabb", "ab") + False + """ + return sorted(first) == sorted(second) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) -- cgit