diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-02-05 11:07:19 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-05 11:07:19 +0000 |
| commit | e3e9505e844e9eed70c16e89b0e0e75168fd5017 (patch) | |
| tree | 78f2cad1f088ea19366bb22b20698989d37f3637 | |
| parent | 68bcd28a8e3d50b8573be094aec8dc55a3b1483e (diff) | |
| parent | 735978e34a40c558e7e3996d7ac575eed6ea4738 (diff) | |
| download | perlweeklychallenge-club-e3e9505e844e9eed70c16e89b0e0e75168fd5017.tar.gz perlweeklychallenge-club-e3e9505e844e9eed70c16e89b0e0e75168fd5017.tar.bz2 perlweeklychallenge-club-e3e9505e844e9eed70c16e89b0e0e75168fd5017.zip | |
Merge pull request #11534 from oWnOIzRi/week307
add solution week 307 task 2 in python
| -rw-r--r-- | challenge-307/steven-wilson/python/ch-02.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/challenge-307/steven-wilson/python/ch-02.py b/challenge-307/steven-wilson/python/ch-02.py new file mode 100644 index 0000000000..f910e3bf29 --- /dev/null +++ b/challenge-307/steven-wilson/python/ch-02.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) |
