aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-257/pokgopun/python/ch-1.py60
-rw-r--r--challenge-257/pokgopun/python/ch-2.py173
2 files changed, 233 insertions, 0 deletions
diff --git a/challenge-257/pokgopun/python/ch-1.py b/challenge-257/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..895ac3857b
--- /dev/null
+++ b/challenge-257/pokgopun/python/ch-1.py
@@ -0,0 +1,60 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-257/
+"""
+
+Task 1: Smaller than Current
+
+Submitted by: [44]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a array of integers, @ints.
+
+ Write a script to find out how many integers are smaller than current
+ i.e. foreach ints[i], count ints[j] < ints[i] where i != j.
+
+Example 1
+
+Input: @ints = (5, 2, 1, 6)
+Output: (2, 1, 0, 3)
+
+For $ints[0] = 5, there are two integers (2,1) smaller than 5.
+For $ints[1] = 2, there is one integer (1) smaller than 2.
+For $ints[2] = 1, there is none integer smaller than 1.
+For $ints[3] = 6, there are three integers (5,2,1) smaller than 6.
+
+Example 2
+
+Input: @ints = (1, 2, 0, 3)
+Output: (1, 2, 0, 3)
+
+Example 3
+
+Input: @ints = (0, 1)
+Output: (0, 1)
+
+Example 4
+
+Input: @ints = (9, 4, 9, 2)
+Output: (2, 1, 2, 0)
+
+Task 2: Reduced Row Echelon
+"""
+### solution by pokgopun@gmail.com
+
+def stc(tup: tuple):
+ return tuple(
+ sum(x < y for x in tup) for y in tup
+ )
+
+import unittest
+
+class TestStc(unittest.TestCase):
+ def test(self):
+ for inpt,otpt in {
+ (5, 2, 1, 6): (2, 1, 0, 3),
+ (1, 2, 0, 3): (1, 2, 0, 3),
+ (0, 1): (0, 1),
+ (9, 4, 9, 2): (2, 1, 2, 0),
+ }.items():
+ self.assertEqual(stc(inpt),otpt)
+
+unittest.main()
diff --git a/challenge-257/pokgopun/python/ch-2.py b/challenge-257/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..31f932500e
--- /dev/null
+++ b/challenge-257/pokgopun/python/ch-2.py
@@ -0,0 +1,173 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-257/
+"""
+
+Task 2: Reduced Row Echelon
+
+Submitted by: [45]Ali Moradi
+ __________________________________________________________________
+
+ Given a matrix M, check whether the matrix is in reduced row echelon
+ form.
+
+ A matrix must have the following properties to be in reduced row
+ echelon form:
+1. If a row does not consist entirely of zeros, then the first
+ nonzero number in the row is a 1. We call this the leading 1.
+2. If there are any rows that consist entirely of zeros, then
+ they are grouped together at the bottom of the matrix.
+3. In any two successive rows that do not consist entirely of zeros,
+ the leading 1 in the lower row occurs farther to the right than
+ the leading 1 in the higher row.
+4. Each column that contains a leading 1 has zeros everywhere else
+ in that column.
+
+ For example:
+[
+ [1,0,0,1],
+ [0,1,0,2],
+ [0,0,1,3]
+]
+
+ The above matrix is in reduced row echelon form since the first nonzero
+ number in each row is a 1, leading 1s in each successive row are
+ farther to the right, and above and below each leading 1 there are only
+ zeros.
+
+ For more information check out this wikipedia [46]article.
+
+Example 1
+
+ Input: $M = [
+ [1, 1, 0],
+ [0, 1, 0],
+ [0, 0, 0]
+ ]
+ Output: 0
+
+Example 2
+
+ Input: $M = [
+ [0, 1,-2, 0, 1],
+ [0, 0, 0, 1, 3],
+ [0, 0, 0, 0, 0],
+ [0, 0, 0, 0, 0]
+ ]
+ Output: 1
+
+Example 3
+
+ Input: $M = [
+ [1, 0, 0, 4],
+ [0, 1, 0, 7],
+ [0, 0, 1,-1]
+ ]
+ Output: 1
+
+Example 4
+
+ Input: $M = [
+ [0, 1,-2, 0, 1],
+ [0, 0, 0, 0, 0],
+ [0, 0, 0, 1, 3],
+ [0, 0, 0, 0, 0]
+ ]
+ Output: 0
+
+Example 5
+
+ Input: $M = [
+ [0, 1, 0],
+ [1, 0, 0],
+ [0, 0, 0]
+ ]
+ Output: 0
+
+Example 6
+
+ Input: $M = [
+ [4, 0, 0, 0],
+ [0, 1, 0, 7],
+ [0, 0, 1,-1]
+ ]
+ Output: 0
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 25th February
+ 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def areAllZeroes(tup: tuple): ### check if all members are zeros
+ return sum( x!=0 for x in tup )==0
+
+def genNZ(tot: tuple): ### remove bottom all-zeroes rows, return others rows in reverse
+ c = 0
+ l = len(tot)
+ for i in range(l):
+ if areAllZeroes(tot[l-i-1]):
+ c += 1
+ if c > i:
+ continue
+ yield tot[l-i-1]
+
+def lopos(tup: tuple): ### position of leading 1, return -1 if there is no leadnig 1
+ for i in range(len(tup)):
+ if tup[i]==1:
+ return i
+ return -1
+
+def isRRE(tot: tuple): ### check if matrix is Reduced Row Echelon
+ l = len(tot[0]) ### to store previous leading 1 position, as we will do it in backward, initial value will be the row length which is always greater than any position
+ tot = tuple(genNZ(tot)) ### matrix with bottom all-zeroes rows removed, but the rows will be reversed
+ rc = len(tot) ### row count, this will be used by list comprehension that check if same columns as the leading 1 are all zeros or not
+ for t in tot:
+ lp = lopos(t) ### find out the poistion of leading 1
+ if lp < 0 or lp >= l or sum(tot[r][lp]==0 for r in range(rc))!=rc-1: ### False if leading 1 cannot be found or its position is not less than the previous one or other values in its postions are not all zeros
+ return False
+ l = lp
+ return True
+
+import unittest
+
+class TestIsRRE(unittest.TestCase):
+ def test(self):
+ for inpt,otpt in {
+ (
+ (1, 1, 0),
+ (0, 1, 0),
+ (0, 0, 0)
+ ): 0,
+ (
+ (0, 1,-2, 0, 1),
+ (0, 0, 0, 1, 3),
+ (0, 0, 0, 0, 0),
+ (0, 0, 0, 0, 0)
+ ): 1,
+ (
+ (1, 0, 0, 4),
+ (0, 1, 0, 7),
+ (0, 0, 1,-1)
+ ): 1,
+ (
+ (0, 1,-2, 0, 1),
+ (0, 0, 0, 0, 0),
+ (0, 0, 0, 1, 3),
+ (0, 0, 0, 0, 0)
+ ): 0,
+ (
+ (0, 1, 0),
+ (1, 0, 0),
+ (0, 0, 0)
+ ): 0,
+ (
+ (4, 0, 0, 0),
+ (0, 1, 0, 7),
+ (0, 0, 1,-1)
+ ): 0,
+ }.items():
+ self.assertEqual(isRRE(inpt),otpt)
+
+unittest.main()