aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-04-24 12:51:28 +1000
committerMichael Manring <michael@manring>2024-04-24 12:51:28 +1000
commita0f54a5b04a049864cc402fc2f85ee62ef040fb4 (patch)
treea46a601de639ec90749dd095661a4407d039e06b
parent25c88efc3e83fc333e98da6a0157e1853be61044 (diff)
downloadperlweeklychallenge-club-a0f54a5b04a049864cc402fc2f85ee62ef040fb4.tar.gz
perlweeklychallenge-club-a0f54a5b04a049864cc402fc2f85ee62ef040fb4.tar.bz2
perlweeklychallenge-club-a0f54a5b04a049864cc402fc2f85ee62ef040fb4.zip
pwc266 solution in python
-rw-r--r--challenge-266/pokgopun/python/ch-1.py57
-rw-r--r--challenge-266/pokgopun/python/ch-2.py103
2 files changed, 160 insertions, 0 deletions
diff --git a/challenge-266/pokgopun/python/ch-1.py b/challenge-266/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..96addf3d91
--- /dev/null
+++ b/challenge-266/pokgopun/python/ch-1.py
@@ -0,0 +1,57 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-266/
+"""
+
+Task 1: Uncommon Words
+
+Submitted by: [41]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given two sentences, $line1 and $line2.
+
+ Write a script to find all uncommmon words in any order in the given
+ two sentences. Return ('') if none found.
+
+ A word is uncommon if it appears exactly once in one of the
+ sentences and doesn’t appear in other sentence.
+
+Example 1
+
+Input: $line1 = 'Mango is sweet'
+ $line2 = 'Mango is sour'
+Output: ('sweet', 'sour')
+
+Example 2
+
+Input: $line1 = 'Mango Mango'
+ $line2 = 'Orange'
+Output: ('Orange')
+
+Example 3
+
+Input: $line1 = 'Mango is Mango'
+ $line2 = 'Orange is Orange'
+Output: ('')
+
+Task 2: X Matrix
+"""
+### solution by pokgopun@gmail.com
+
+def uncommonWords(line1, line2):
+ words = f'{line1} {line2}'.split()
+ return tuple(
+ w for w in words if words.count(w)==1
+ ) or ('',)
+
+
+import unittest
+
+class TestUncommonWords(unittest.TestCase):
+ def test(self):
+ for (line1,line2), ans in {
+ ('Mango is sweet','Mango is sour'): ('sweet', 'sour'),
+ ('Mango Mango','Orange'): ('Orange',),
+ ('Mango is Mango','Orange is Orange'): ('',),
+ }.items():
+ self.assertEqual(uncommonWords(line1,line2),ans)
+
+unittest.main()
diff --git a/challenge-266/pokgopun/python/ch-2.py b/challenge-266/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..f12584ae55
--- /dev/null
+++ b/challenge-266/pokgopun/python/ch-2.py
@@ -0,0 +1,103 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-266/
+"""
+
+Task 2: X Matrix
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a square matrix, $matrix.
+
+ Write a script to find if the given matrix is X Matrix.
+
+ A square matrix is an X Matrix if all the elements on the main
+ diagonal and antidiagonal are non-zero and everything else are zero.
+
+Example 1
+
+Input: $matrix = [ [1, 0, 0, 2],
+ [0, 3, 4, 0],
+ [0, 5, 6, 0],
+ [7, 0, 0, 1],
+ ]
+Output: true
+
+Example 2
+
+Input: $matrix = [ [1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9],
+ ]
+Output: false
+
+Example 3
+
+Input: $matrix = [ [1, 0, 2],
+ [0, 3, 0],
+ [4, 0, 5],
+ ]
+Output: true
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 28th April
+ 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def hasAllZero(arr):
+ return
+
+def mtxIsValid(mtx: list):
+ l = len(mtx)
+ if l < 3:
+ return None
+ h = l // 2
+ col = 0
+ lst = list()
+ for row in range(h):
+ for r in (row,-row-1):
+ lst = [e for e in mtx[r]]
+ if len(lst) != l:
+ return None
+ for c in (col,-col-1):
+ if lst[c] == 0:
+ return False
+ lst[c] = 0
+ for e in lst:
+ if e != 0:
+ return False
+ col += 1
+ if h % 2:
+ lst = [e for e in mtx[h]]
+ if len(lst) != l:
+ return None
+ if mtx[h][h] == 0:
+ return False
+ lst[h] = 0
+ for e in lst:
+ if e != 0:
+ return False
+ return True
+
+import unittest
+
+class TestMtxIsValid(unittest.TestCase):
+ def test(self):
+ for ans, mtx in {
+ True: [ [1, 0, 0, 2],
+ [0, 3, 4, 0],
+ [0, 5, 6, 0],
+ [7, 0, 0, 1],],
+ False: [ [1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9],],
+ True: [ [1, 0, 2],
+ [0, 3, 0],
+ [4, 0, 5],],
+ }.items():
+ self.assertEqual(ans,mtxIsValid(mtx))
+
+unittest.main()