aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-271/pokgopun/python/ch-1.py69
-rw-r--r--challenge-271/pokgopun/python/ch-2.py72
2 files changed, 141 insertions, 0 deletions
diff --git a/challenge-271/pokgopun/python/ch-1.py b/challenge-271/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..5e2b758e0a
--- /dev/null
+++ b/challenge-271/pokgopun/python/ch-1.py
@@ -0,0 +1,69 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-271/
+"""
+
+Task 1: Maximum Ones
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a m x n binary matrix.
+
+ Write a script to return the row number containing maximum ones, in
+ case of more than one rows then return smallest row number.
+
+Example 1
+
+input: $matrix = [ [0, 1],
+ [1, 0],
+ ]
+output: 1
+
+row 1 and row 2 have the same number of ones, so return row 1.
+
+example 2
+
+input: $matrix = [ [0, 0, 0],
+ [1, 0, 1],
+ ]
+output: 2
+
+row 2 has the maximum ones, so return row 2.
+
+example 3
+
+input: $matrix = [ [0, 0],
+ [1, 1],
+ [0, 0],
+ ]
+Output: 2
+
+Row 2 have the maximum ones, so return row 2.
+
+Task 2: Sort by 1 bits
+"""
+### solution by pokgopun@gmail.com
+
+def maxOne(mtx: list):
+ return 1 - max(
+ (mtx[e].count(1),-e) for e in range(len(mtx))
+ )[1]
+
+import unittest
+
+class TestMaxOne(unittest.TestCase):
+ def test(self):
+ for otpt, inpt in {
+ 1: [ [0, 1],
+ [1, 0],
+ ],
+ 2: [ [0, 0, 0],
+ [1, 0, 1],
+ ],
+ 2: [ [0, 0],
+ [1, 1],
+ [0, 0],
+ ],
+ }.items():
+ self.assertEqual(maxOne(inpt),otpt)
+
+unittest.main()
diff --git a/challenge-271/pokgopun/python/ch-2.py b/challenge-271/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..80ae73080e
--- /dev/null
+++ b/challenge-271/pokgopun/python/ch-2.py
@@ -0,0 +1,72 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-271/
+"""
+
+Task 2: Sort by 1 bits
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are give an array of integers, @ints.
+
+ Write a script to sort the integers in ascending order by the number of
+ 1 bits in their binary representation. In case more than one integers
+ have the same number of 1 bits then sort them in ascending order.
+
+Example 1
+
+Input: @ints = (0, 1, 2, 3, 4, 5, 6, 7, 8)
+Output: (0, 1, 2, 4, 8, 3, 5, 6, 7)
+
+0 = 0 one bits
+1 = 1 one bits
+2 = 1 one bits
+4 = 1 one bits
+8 = 1 one bits
+3 = 2 one bits
+5 = 2 one bits
+6 = 2 one bits
+7 = 3 one bits
+
+Example 2
+
+Input: @ints = (1024, 512, 256, 128, 64)
+Output: (64, 128, 256, 512, 1024)
+
+All integers in the given array have one 1-bits, so just sort them in ascending
+order.
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 2nd June 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def countBitOn(n):
+ c = 0
+ while n > 0:
+ if n % 2 > 0:
+ c += 1
+ n = n // 2
+ return c
+
+def sortBitOn(nums: tuple):
+ return tuple(
+ sorted(
+ nums,
+ key = lambda x: (countBitOn(x), x)
+ )
+ )
+
+import unittest
+
+class TestSortBitOn(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ (0, 1, 2, 3, 4, 5, 6, 7, 8): (0, 1, 2, 4, 8, 3, 5, 6, 7),
+ (1024, 512, 256, 128, 64): (64, 128, 256, 512, 1024),
+ }.items():
+ self.assertEqual(sortBitOn(inpt),otpt)
+
+unittest.main()