aboutsummaryrefslogtreecommitdiff
path: root/challenge-270
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-20 12:52:37 +0100
committerGitHub <noreply@github.com>2024-05-20 12:52:37 +0100
commit5b28a9d608d2a419bf320f8528d56c136a309ebc (patch)
tree7fd58d14e7a5cd556723a04f44de90852315ab14 /challenge-270
parentb5efef7c70c0a49e70d8e45849ceaa63c0992b8d (diff)
parent749da7ab7b6c68de590cec57f44243333f2895d9 (diff)
downloadperlweeklychallenge-club-5b28a9d608d2a419bf320f8528d56c136a309ebc.tar.gz
perlweeklychallenge-club-5b28a9d608d2a419bf320f8528d56c136a309ebc.tar.bz2
perlweeklychallenge-club-5b28a9d608d2a419bf320f8528d56c136a309ebc.zip
Merge pull request #10119 from pokgopun/pwc270
pwc270 solution
Diffstat (limited to 'challenge-270')
-rw-r--r--challenge-270/pokgopun/python/ch-1.py79
-rw-r--r--challenge-270/pokgopun/python/ch-2.py105
2 files changed, 184 insertions, 0 deletions
diff --git a/challenge-270/pokgopun/python/ch-1.py b/challenge-270/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..dff5428e14
--- /dev/null
+++ b/challenge-270/pokgopun/python/ch-1.py
@@ -0,0 +1,79 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-270/
+"""
+
+Task 1: Special Positions
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a m x n binary matrix.
+
+ Write a script to return the number of special positions in the given
+ binary matrix.
+
+ A position (i, j) is called special if $matrix[i][j] == 1 and all
+ other elements in the row i and column j are 0.
+
+Example 1
+
+Input: $matrix = [ [1, 0, 0],
+ [0, 0, 1],
+ [1, 0, 0],
+ ]
+Output: 1
+
+There is only special position (1, 2) as $matrix[1][2] == 1
+and all other elements in row 1 and column 2 are 0.
+
+Example 2
+
+Input: $matrix = [ [1, 0, 0],
+ [0, 1, 0],
+ [0, 0, 1],
+ ]
+Output: 3
+
+Special positions are (0,0), (1, 1) and (2,2).
+
+Task 2: Distribute Elements
+"""
+### solution by pokgopun@gmail.com
+
+def isAllZero(lst):
+ for i in lst:
+ if i!=0:
+ return False
+ return True
+
+def cntSP(mn: list):
+ m = len(mn)
+ n = len(mn[0])
+ c = 0
+ for i in range(m):
+ for j in range(n):
+ if mn[i][j] != 1:
+ continue
+ if isAllZero((mn[x][j] for x in range(m) if x!=i)) == False:
+ continue
+ if isAllZero((mn[i][x] for x in range(n) if x!=j)) == False:
+ continue
+ c += 1
+ return c
+
+import unittest
+
+class TestCntSP(unittest.TestCase):
+ def test(self):
+ for otpt, inpt in {
+ 1: [ [1, 0, 0],
+ [0, 0, 1],
+ [1, 0, 0],
+ ],
+ 3: [ [1, 0, 0],
+ [0, 1, 0],
+ [0, 0, 1],
+ ],
+ }.items():
+ self.assertEqual(cntSP(inpt),otpt)
+
+unittest.main()
diff --git a/challenge-270/pokgopun/python/ch-2.py b/challenge-270/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..ac6e3bbcb0
--- /dev/null
+++ b/challenge-270/pokgopun/python/ch-2.py
@@ -0,0 +1,105 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-270/
+"""
+
+Task 2: Distribute Elements
+
+Submitted by: [44]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are give an array of integers, @ints and two integers, $x and $y.
+
+ Write a script to execute one of the two options:
+Level 1:
+Pick an index i of the given array and do $ints[i] += 1
+
+Level 2:
+Pick two different indices i,j and do $ints[i] +=1 and $ints[j] += 1.
+
+ You are allowed to perform as many levels as you want to make every
+ elements in the given array equal. There is cost attach for each level,
+ for Level 1, the cost is $x and $y for Level 2.
+
+ In the end return the minimum cost to get the work done.
+
+Example 1
+
+Input: @ints = (4, 1), $x = 3 and $y = 2
+Output: 9
+
+Level 1: i=1, so $ints[1] += 1.
+@ints = (4, 2)
+
+Level 1: i=1, so $ints[1] += 1.
+@ints = (4, 3)
+
+Level 1: i=1, so $ints[1] += 1.
+@ints = (4, 4)
+
+We perforned operation Level 1, 3 times.
+So the total cost would be 3 x $x => 3 x 3 => 9
+
+Example 2
+
+Input: @ints = (2, 3, 3, 3, 5), $x = 2 and $y = 1
+Output: 6
+
+Level 2: i=0, j=1, so $ints[0] += 1 and $ints[1] += 1
+@ints = (3, 4, 3, 3, 5)
+
+Level 2: i=0, j=2, so $ints[0] += 1 and $ints[2] += 1
+@ints = (4, 4, 4, 3, 5)
+
+Level 2: i=0, j=3, so $ints[0] += 1 and $ints[3] += 1
+@ints = (5, 4, 4, 4, 5)
+
+Level 2: i=1, j=2, so $ints[1] += 1 and $ints[2] += 1
+@ints = (5, 5, 5, 4, 5)
+
+Level 1: i=3, so $ints[3] += 1
+@ints = (5, 5, 5, 5, 5)
+
+We perforned operation Level 1, 1 time and Level 2, 4 times.
+So the total cost would be (1 x $x) + (3 x $y) => (1 x 2) + (4 x 1) => 6
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 26th May 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def distElem(ints: list, x: int, y: int):
+ if len(set(ints)) < 2:
+ return 0
+ ints.sort()
+ mx = ints[-1]
+ c = 0
+ while True:
+ ints = ints[:ints.index(mx)]
+ l = len(ints)
+ if l == 0:
+ break
+ if l == 1 or 2*x < y:
+ while ints[-1] < mx:
+ for i in range(l):
+ ints[i] += 1
+ c += x
+ else:
+ while ints[-1] < mx:
+ for i in range(-2,0):
+ ints[i] += 1
+ c += y
+ return c
+
+import unittest
+
+class TestDistElem(unittest.TestCase):
+ def test(self):
+ for cost, (ints, x, y) in {
+ 9: ([4, 1], 3, 2),
+ 6: ([2, 3, 3, 3, 5], 2, 1),
+ }.items():
+ self.assertEqual(distElem(ints,x,y),cost)
+
+unittest.main()