diff options
| author | Michael Manring <michael@manring> | 2025-02-07 15:18:14 +1100 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2025-02-07 15:18:14 +1100 |
| commit | 3e614d829c174ffbf305c2bb40b0b7469916e924 (patch) | |
| tree | 0089800edd984cde7c50e10ed40705c4e70d5d39 | |
| parent | ea528b3a4f9f371f274a0c9c3012f49cb5e2bed0 (diff) | |
| download | perlweeklychallenge-club-3e614d829c174ffbf305c2bb40b0b7469916e924.tar.gz perlweeklychallenge-club-3e614d829c174ffbf305c2bb40b0b7469916e924.tar.bz2 perlweeklychallenge-club-3e614d829c174ffbf305c2bb40b0b7469916e924.zip | |
pwc307 solution in python
| -rw-r--r-- | challenge-307/pokgopun/python/ch-1.py | 65 | ||||
| -rw-r--r-- | challenge-307/pokgopun/python/ch-2.py | 75 |
2 files changed, 140 insertions, 0 deletions
diff --git a/challenge-307/pokgopun/python/ch-1.py b/challenge-307/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..be785600a2 --- /dev/null +++ b/challenge-307/pokgopun/python/ch-1.py @@ -0,0 +1,65 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-307/ +""" + +Task 1: Check Order + +Submitted by: [42]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Write a script to re-arrange the given array in an increasing order and + return the indices where it differs from the original array. + +Example 1 + +Input: @ints = (5, 2, 4, 3, 1) +Output: (0, 2, 3, 4) + +Before: (5, 2, 4, 3, 1) +After : (1, 2, 3, 4, 5) + +Difference at indices: (0, 2, 3, 4) + +Example 2 + +Input: @ints = (1, 2, 1, 1, 3) +Output: (1, 3) + +Before: (1, 2, 1, 1, 3) +After : (1, 1, 1, 2, 3) + +Difference at indices: (1, 3) + +Example 3 + +Input: @ints = (3, 1, 3, 2, 3) +Output: (0, 1, 3) + +Before: (3, 1, 3, 2, 3) +After : (1, 2, 3, 3, 3) + +Difference at indices: (0, 1, 3) + +Task 2: Find Anagrams +""" +### solution by pokgopun@gmail.com + +from typing import Tuple + +def checkOrder(ints: Tuple[int]) -> Tuple[int]: + sints = sorted(ints) + return tuple( i for i in range(len(ints)) if ints[i] != sints[i] ) + +import unittest + +class TestCheckOrder(unittest.TestCase): + def test(self): + for inpt, otpt in { + (5, 2, 4, 3, 1): (0, 2, 3, 4), + (1, 2, 1, 1, 3): (1, 3), + (3, 1, 3, 2, 3): (0, 1, 3), + }.items(): + self.assertEqual(checkOrder(inpt), otpt) + +unittest.main() diff --git a/challenge-307/pokgopun/python/ch-2.py b/challenge-307/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..150b9f8210 --- /dev/null +++ b/challenge-307/pokgopun/python/ch-2.py @@ -0,0 +1,75 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-307/ +""" + +Task 2: Find Anagrams + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a list of words, @words. + + Write a script to 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. + +Example 1 + +Input: @words = ("acca", "dog", "god", "perl", "repl") +Output: 3 + +Step 1: "dog" and "god" are anagrams, so dropping "dog" and keeping "god" => ("a +cca", "god", "perl", "repl") +Step 2: "perl" and "repl" are anagrams, so dropping "perl" and keeping "repl" => + ("acca", "god", "repl") + +Example 2 + +Input: @words = ("abba", "baba", "aabb", "ab", "ab") +Output: 2 + +Step 1: "abba" and "baba" are anagrams, so dropping "abba" and keeping "baba" => + ("baba", "aabb", "ab", "ab") +Step 2: "baba" and "aabb" are anagrams, so dropping "baba" and keeping "aabb" => + ("aabb", "ab", "ab") +Step 3: "ab" and "ab" are anagrams, so dropping "ab" and keeping "ab" => ("aabb" +, "ab") + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 9^th February + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +from typing import Tuple + +def findAnagrams(words: Tuple[str]) -> Tuple[str]: + words = list(words) + i = 0 + while True: + if i + 1 >= len(words): + break + cur,nxt = words[i], words[i+1] + if len(cur) == len(nxt) and set(cur) == set(nxt): + words.pop(i) + else: + i += 1 + #print(words) + return len(words) + +import unittest + +class TestFindAnagrams(unittest.TestCase): + def test(self): + for inpt, otpt in { + ("acca", "dog", "god", "perl", "repl"): 3, + ("abba", "baba", "aabb", "ab", "ab"): 2, + }.items(): + self.assertEqual(findAnagrams(inpt), otpt) + +unittest.main() + + |
