aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-05-06 16:45:40 +1000
committerMichael Manring <michael@manring>2024-05-06 16:45:40 +1000
commitc1f23ff188765024d1b7f122ca1f5b2db251bd35 (patch)
tree158b62d0c359602cf39ce56cf7719dcdc1de43a0
parenta25f22449decd32a0999053f193c19f183c6a33e (diff)
downloadperlweeklychallenge-club-c1f23ff188765024d1b7f122ca1f5b2db251bd35.tar.gz
perlweeklychallenge-club-c1f23ff188765024d1b7f122ca1f5b2db251bd35.tar.bz2
perlweeklychallenge-club-c1f23ff188765024d1b7f122ca1f5b2db251bd35.zip
pwc268 solution in python
-rw-r--r--challenge-268/pokgopun/python/ch-1.py69
-rw-r--r--challenge-268/pokgopun/python/ch-2.py65
2 files changed, 134 insertions, 0 deletions
diff --git a/challenge-268/pokgopun/python/ch-1.py b/challenge-268/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..93a566716a
--- /dev/null
+++ b/challenge-268/pokgopun/python/ch-1.py
@@ -0,0 +1,69 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-268/
+"""
+
+Task 1: Magic Number
+
+Submitted by: [47]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given two arrays of integers of same size, @x and @y.
+
+ Write a script to find the magic number that when added to each
+ elements of one of the array gives the second array. Elements order is
+ not important.
+
+Example 1
+
+Input: @x = (3, 7, 5)
+ @y = (9, 5, 7)
+Output: 2
+
+The magic number is 2.
+@x = (3, 7, 5)
+ + 2 2 2
+@y = (5, 9, 7)
+
+Example 2
+
+Input: @x = (1, 2, 1)
+ @y = (5, 4, 4)
+Output: 3
+
+The magic number is 3.
+@x = (1, 2, 1)
+ + 3 3 3
+@y = (5, 4, 4)
+
+Example 3
+
+Input: @x = (2)
+ @y = (5)
+Output: 3
+
+Task 2: Number Game
+"""
+### solution by pokgopun@gmail.com
+
+def magicNumber(x: tuple, y: tuple):
+ l = len(x)
+ if l == 0 or l != len(y):
+ return None
+ x, y = sorted(x), sorted(y)
+ d = y[0]-x[0]
+ for i in range(1,l):
+ if d != y[i]-x[i]:
+ return None
+ return d
+
+import unittest
+
+class TestMagicNumber(unittest.TestCase):
+ def test(self):
+ for (x,y), ans in {
+ ((3, 7, 5),(9, 5, 7)): 2,
+ ((1, 2, 1),(5, 4, 4)): 3,
+ ((2,),(5,)): 3,
+ }.items():
+ self.assertEqual(magicNumber(x,y),ans)
+
+unittest.main()
diff --git a/challenge-268/pokgopun/python/ch-2.py b/challenge-268/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..ce4e10e2b4
--- /dev/null
+++ b/challenge-268/pokgopun/python/ch-2.py
@@ -0,0 +1,65 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-268/
+"""
+
+Task 2: Number Game
+
+Submitted by: [48]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of integers, @ints, with even number of
+ elements.
+
+ Write a script to create a new array made up of elements of the given
+ array. Pick the two smallest integers and add it to new array in
+ decreasing order i.e. high to low. Keep doing until the given array is
+ empty.
+
+Example 1
+
+Input: @ints = (2, 5, 3, 4)
+Output: (3, 2, 5, 4)
+
+Round 1: we picked (2, 3) and push it to the new array (3, 2)
+Round 2: we picked the remaining (4, 5) and push it to the new array (5, 4)
+
+Example 2
+
+Input: @ints = (9, 4, 1, 3, 6, 4, 6, 1)
+Output: (1, 1, 4, 3, 6, 4, 9, 6)
+
+Example 3
+
+Input: @ints = (1, 2, 2, 3)
+Output: (2, 1, 3, 2)
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 12th May 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def numberGame(ints: tuple):
+ l = len(ints)
+ if l==0 or l % 2 != 0:
+ return None
+ ints = tuple(sorted(ints,reverse=True))
+ ans = ()
+ while l > 0:
+ ans += ints[l-2:l]
+ l -= 2
+ return ans
+
+import unittest
+
+class TestNumberGame(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ (2, 5, 3, 4): (3, 2, 5, 4),
+ (9, 4, 1, 3, 6, 4, 6, 1): (1, 1, 4, 3, 6, 4, 9, 6),
+ (1, 2, 2, 3): (2, 1, 3, 2),
+ }.items():
+ self.assertEqual(numberGame(inpt),otpt)
+
+unittest.main()