From cec1c29e0cfc6891cd75225c7b45e375d51076b7 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 12 Feb 2024 21:56:06 +1100 Subject: pwc256 solution in python --- challenge-256/pokgopun/python/ch-1.py | 54 ++++++++++++++++++++++++++++++++ challenge-256/pokgopun/python/ch-2.py | 59 +++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 challenge-256/pokgopun/python/ch-1.py create mode 100644 challenge-256/pokgopun/python/ch-2.py diff --git a/challenge-256/pokgopun/python/ch-1.py b/challenge-256/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..09587e16af --- /dev/null +++ b/challenge-256/pokgopun/python/ch-1.py @@ -0,0 +1,54 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-256/ +""" + +Task 1: Maximum Pairs + +Submitted by: [41]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of distinct words, @words. + + Write a script to find the maximum pairs in the given array. The words + $words[i] and $words[j] can be a pair one is reverse of the other. + +Example 1 + +Input: @words = ("ab", "de", "ed", "bc") +Output: 1 + +There is one pair in the given array: "de" and "ed" + +Example 2 + +Input: @words = ("aa", "ba", "cd", "ed") +Output: 0 + +Example 3 + +Input: @words = ("uv", "qp", "st", "vu", "mn", "pq")) +Output: 2 + +Task 2: Merge Strings +""" +### solution by pokgopun@gmail.com + +def maxPair(words: tuple): + c = 0 + for i in range(len(words)-1): + for e in words[i+1:]: + if tuple(words[i])==tuple(reversed(e)): + c += 1 + return c + +import unittest + +class TestMaxPair(unittest.TestCase): + def test(self): + for inpt,otpt in { + ("ab", "de", "ed", "bc"): 1, + ("aa", "ba", "cd", "ed"): 0, + ("uv", "qp", "st", "vu", "mn", "pq"): 2, + }.items(): + self.assertEqual(maxPair(inpt),otpt) + +unittest.main() diff --git a/challenge-256/pokgopun/python/ch-2.py b/challenge-256/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..e32ae2557a --- /dev/null +++ b/challenge-256/pokgopun/python/ch-2.py @@ -0,0 +1,59 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-256/ +""" + +Task 2: Merge Strings + +Submitted by: [42]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two strings, $str1 and $str2. + + Write a script to merge the given strings by adding in alternative + order starting with the first string. If a string is longer than the + other then append the remaining at the end. + +Example 1 + +Input: $str1 = "abcd", $str2 = "1234" +Output: "a1b2c3d4" + +Example 2 + +Input: $str1 = "abc", $str2 = "12345" +Output: "a1b2c345" + +Example 3 + +Input: $str1 = "abcde", $str2 = "123" +Output: "a1b2c3de" + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 18th February + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +from itertools import zip_longest,chain + +def mergeStrings(str1, str2): + return "".join( + chain.from_iterable( + zip_longest(str1,str2,fillvalue="") + ) + ) + +import unittest + +class TestMergeStrings(unittest.TestCase): + def test(self): + for (str1,str2), otpt in { + ("abcd","1234"): "a1b2c3d4", + ("abc", "12345"): "a1b2c345", + ("abcde", "123"): "a1b2c3de", + }.items(): + self.assertEqual(mergeStrings(str1,str2),otpt) + +unittest.main() -- cgit