aboutsummaryrefslogtreecommitdiff
path: root/challenge-308/pokgopun/python
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-02-11 11:03:47 +0000
committerGitHub <noreply@github.com>2025-02-11 11:03:47 +0000
commita4cc4b5322dda475bf4c0d18e87ee972cce7fe6f (patch)
tree8216ec3f9ffde635b1301aae5744b57e291be349 /challenge-308/pokgopun/python
parent71218d14d0f8df1d2933ac1944c62dbc21fd84ec (diff)
parentc1c1323849a9659113f16d26f56d5548722de1e8 (diff)
downloadperlweeklychallenge-club-a4cc4b5322dda475bf4c0d18e87ee972cce7fe6f.tar.gz
perlweeklychallenge-club-a4cc4b5322dda475bf4c0d18e87ee972cce7fe6f.tar.bz2
perlweeklychallenge-club-a4cc4b5322dda475bf4c0d18e87ee972cce7fe6f.zip
Merge pull request #11563 from pokgopun/pwc308
Pwc308
Diffstat (limited to 'challenge-308/pokgopun/python')
-rw-r--r--challenge-308/pokgopun/python/ch-1.py49
-rw-r--r--challenge-308/pokgopun/python/ch-2.py73
2 files changed, 122 insertions, 0 deletions
diff --git a/challenge-308/pokgopun/python/ch-1.py b/challenge-308/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..b68d5e5966
--- /dev/null
+++ b/challenge-308/pokgopun/python/ch-1.py
@@ -0,0 +1,49 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-308/
+"""
+
+Task 1: Count Common
+
+Submitted by: [46]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given two array of strings, @str1 and @str2.
+
+ Write a script to return the count of common strings in both arrays.
+
+Example 1
+
+Input: @str1 = ("perl", "weekly", "challenge")
+ @str2 = ("raku", "weekly", "challenge")
+Output: 2
+
+Example 2
+
+Input: @str1 = ("perl", "raku", "python")
+ @str2 = ("python", "java")
+Output: 1
+
+Example 3
+
+Input: @str1 = ("guest", "contribution")
+ @str2 = ("fun", "weekly", "challenge")
+Output: 0
+
+Task 2: Decode XOR
+"""
+### solution by pokgopun@gmail.com
+
+def countCommon(strs1: tuple[str], strs2: tuple[str]) -> int:
+ return len( set(strs1) & set(strs2) )
+
+import unittest
+
+class TestCountCommon(unittest.TestCase):
+ def test(self):
+ for (strs1, strs2), otpt in {
+ (("perl", "weekly", "challenge"),("raku", "weekly", "challenge")): 2,
+ (("perl", "raku", "python"),("python", "java")): 1,
+ (("guest", "contribution"),("fun", "weekly", "challenge")): 0,
+ }.items():
+ self.assertEqual(countCommon(strs1,strs2), otpt)
+
+unittest.main()
diff --git a/challenge-308/pokgopun/python/ch-2.py b/challenge-308/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..25d2716b60
--- /dev/null
+++ b/challenge-308/pokgopun/python/ch-2.py
@@ -0,0 +1,73 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-308/
+"""
+
+Task 2: Decode XOR
+
+Submitted by: [47]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an encoded array and an initial integer.
+
+ Write a script to find the original array that produced the given
+ encoded array. It was encoded such that encoded[i] = orig[i] XOR orig[i
+ + 1].
+
+Example 1
+
+Input: @encoded = (1, 2, 3), $initial = 1
+Output: (1, 0, 2, 1)
+
+Encoded array created like below, if the original array was (1, 0, 2, 1)
+$encoded[0] = (1 xor 0) = 1
+$encoded[1] = (0 xor 2) = 2
+$encoded[2] = (2 xor 1) = 3
+
+Example 2
+
+Input: @encoded = (6, 2, 7, 3), $initial = 4
+Output: (4, 2, 0, 7, 4)
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 16^th February
+ 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def decodeXOR(enc: tuple[int], init: int) -> tuple[int]:
+ l = len(enc)
+ org = [0 for i in range(l+1)]
+ org[0] = init
+ for i in range(l):
+ dst, src = enc[i], org[i]
+ if dst == src: ### a ^ o = a --> o = 0
+ continue
+ #print(i)
+ if dst > src:
+ dst, src = src, dst
+ orgn = 0
+ b = 1
+ while dst > 0:
+ orgn += b * abs(dst%2 - src%2)
+ b *= 2
+ dst //= 2
+ src //= 2
+ if src > 0:
+ orgn += b * src
+ org[i+1] = orgn
+ return tuple(org)
+
+import unittest
+
+class TestDecodeXOR(unittest.TestCase):
+ def test(self):
+ for (enc, init), otpt in {
+ ((1, 2, 3), 1): (1, 0, 2, 1),
+ ((6, 2, 7, 3), 4): (4, 2, 0, 7, 4),
+ }.items():
+ #print(f'{enc}\n{otpt}')
+ self.assertEqual(decodeXOR(enc, init), otpt)
+
+unittest.main()