aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-01-22 20:41:49 +1100
committerMichael Manring <michael@manring>2024-01-22 20:41:49 +1100
commit8b20006ad65464ae2ffdc77b7ebc1f4ba2cfde9b (patch)
tree86ab0350b927e0f439e0e0d99a1aae3b3fe381e7
parent9d7dc816f7775abfee7d90f0a2a969611902be54 (diff)
downloadperlweeklychallenge-club-8b20006ad65464ae2ffdc77b7ebc1f4ba2cfde9b.tar.gz
perlweeklychallenge-club-8b20006ad65464ae2ffdc77b7ebc1f4ba2cfde9b.tar.bz2
perlweeklychallenge-club-8b20006ad65464ae2ffdc77b7ebc1f4ba2cfde9b.zip
pwc253 solution in python
-rw-r--r--challenge-253/pokgopun/python/ch-1.py50
-rw-r--r--challenge-253/pokgopun/python/ch-2.py92
2 files changed, 142 insertions, 0 deletions
diff --git a/challenge-253/pokgopun/python/ch-1.py b/challenge-253/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..820f00efb5
--- /dev/null
+++ b/challenge-253/pokgopun/python/ch-1.py
@@ -0,0 +1,50 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-253/
+"""
+
+Task 1: Split Strings
+
+Submitted by: [43]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of strings and a character separator.
+
+ Write a script to return all words separated by the given character
+ excluding empty string.
+
+Example 1
+
+Input: @words = ("one.two.three","four.five","six")
+ $separator = "."
+Output: "one","two","three","four","five","six"
+
+Example 2
+
+Input: @words = ("$perl$$", "$$raku$")
+ $separator = "$"
+Output: "perl","raku"
+
+Task 2: Weakest Row
+"""
+### solution by pokgopun@gmail.com
+
+from itertools import chain
+
+def splitString(wrd: tuple, sep: str):
+ return tuple(
+ e for e in chain.from_iterable(
+ w.split(sep) for w in wrd
+ ) if e
+ )
+
+import unittest
+
+class TestSplitString(unittest.TestCase):
+ def test(self):
+ for (wrd,sep),otpt in {
+ (("one.two.three","four.five","six"), "."): ("one","two","three","four","five","six"),
+ (("$perl$$", "$$raku$"), "$"): ("perl","raku"),
+ }.items():
+ self.assertEqual(splitString(wrd,sep),otpt)
+
+unittest.main()
+
diff --git a/challenge-253/pokgopun/python/ch-2.py b/challenge-253/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..4f9a88b784
--- /dev/null
+++ b/challenge-253/pokgopun/python/ch-2.py
@@ -0,0 +1,92 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-253/
+"""
+
+Task 2: Weakest Row
+
+Submitted by: [44]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an m x n binary matrix i.e. only 0 and 1 where 1 always
+ appear before 0.
+
+ A row i is weaker than a row j if one of the following is true:
+a) The number of 1s in row i is less than the number of 1s in row j.
+b) Both rows have the same number of 1 and i < j.
+
+ Write a script to return the order of rows from weakest to strongest.
+
+Example 1
+
+Input: $matrix = [
+ [1, 1, 0, 0, 0],
+ [1, 1, 1, 1, 0],
+ [1, 0, 0, 0, 0],
+ [1, 1, 0, 0, 0],
+ [1, 1, 1, 1, 1]
+ ]
+Output: (2, 0, 3, 1, 4)
+
+The number of 1s in each row is:
+- Row 0: 2
+- Row 1: 4
+- Row 2: 1
+- Row 3: 2
+- Row 4: 5
+
+Example 2
+
+Input: $matrix = [
+ [1, 0, 0, 0],
+ [1, 1, 1, 1],
+ [1, 0, 0, 0],
+ [1, 0, 0, 0]
+ ]
+Output: (0, 2, 3, 1)
+
+The number of 1s in each row is:
+- Row 0: 1
+- Row 1: 4
+- Row 2: 1
+- Row 3: 1
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 28th January
+ 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def weakestRow(tup: tuple):
+ r = tuple(e.count(1) for e in tup)
+ return tuple(
+ sorted(
+ range(len(tup)), key=lambda x: r[x]
+ )
+ )
+
+import unittest
+
+class TestWeakestRow(unittest.TestCase):
+ def test(self):
+ for inpt,otpt in {
+ (
+ (1, 1, 0, 0, 0),
+ (1, 1, 1, 1, 0),
+ (1, 0, 0, 0, 0),
+ (1, 1, 0, 0, 0),
+ (1, 1, 1, 1, 1)
+ ): (2, 0, 3, 1, 4),
+ (
+ (1, 0, 0, 0),
+ (1, 1, 1, 1),
+ (1, 0, 0, 0),
+ (1, 0, 0, 0)
+ ): (0, 2, 3, 1),
+ }.items():
+ self.assertEqual(weakestRow(inpt),otpt)
+
+unittest.main()
+
+