aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2023-12-19 03:15:11 +1100
committerMichael Manring <michael@manring>2023-12-19 03:15:11 +1100
commit6d2834f8e078f16934ff9cb34bf8cc5d2d0f6189 (patch)
tree5a8cca5d0b5e24f6d225d7be34157a0d9d8013ff
parenta09beeb342d8937c474e7356b6c6dba565108065 (diff)
downloadperlweeklychallenge-club-6d2834f8e078f16934ff9cb34bf8cc5d2d0f6189.tar.gz
perlweeklychallenge-club-6d2834f8e078f16934ff9cb34bf8cc5d2d0f6189.tar.bz2
perlweeklychallenge-club-6d2834f8e078f16934ff9cb34bf8cc5d2d0f6189.zip
pwc248 solution in python
-rw-r--r--challenge-248/pokgopun/python/':q59
-rw-r--r--challenge-248/pokgopun/python/ch-1.py59
-rw-r--r--challenge-248/pokgopun/python/ch-2.py91
3 files changed, 209 insertions, 0 deletions
diff --git a/challenge-248/pokgopun/python/':q b/challenge-248/pokgopun/python/':q
new file mode 100644
index 0000000000..196199331d
--- /dev/null
+++ b/challenge-248/pokgopun/python/':q
@@ -0,0 +1,59 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-248/
+"""
+
+Task 1: Shortest Distance
+
+Submitted by: [58]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given a string and a character in the given string.
+
+ Write a script to return an array of integers of size same as length of
+ the given string such that:
+distance[i] is the distance from index i to the closest occurence of
+the given character in the given string.
+
+The distance between two indices i and j is abs(i - j).
+
+Example 1
+
+Input: $str = "loveleetcode", $char = "e"
+Output: (3,2,1,0,1,0,0,1,2,2,1,0)
+
+The character 'e' appears at indices 3, 5, 6, and 11 (0-indexed).
+The closest occurrence of 'e' for index 0 is at index 3, so the distance is abs(
+0 - 3) = 3.
+The closest occurrence of 'e' for index 1 is at index 3, so the distance is abs(
+1 - 3) = 2.
+For index 4, there is a tie between the 'e' at index 3 and the 'e' at index 5,
+but the distance is still the same: abs(4 - 3) == abs(4 - 5) = 1.
+The closest occurrence of 'e' for index 8 is at index 6, so the distance is abs(
+8 - 6) = 2.
+
+Example 2
+
+Input: $str = "aaab", $char = "b"
+Output: (3,2,1,0)
+
+Task 2: Submatrix Sum
+"""
+### solution by pokgopun@gmail.com
+
+def shortestDistance(word, letter):
+ l = len(word)
+ letterIndexes = tuple( i for i in range(l) if word[i]==letter )
+ return tuple(
+ min( abs(x-i) for i in letterIndexes ) for x in range(l)
+ )
+
+import unittest
+
+class TestShortestDistance(unittest.TestCase):
+ def test(self):
+ for (word,letter),distance in {
+ ("loveleetcode", "e"): (3,2,1,0,1,0,0,1,2,2,1,1),
+ ("aaab", "b"): (3,2,1,0),
+ }.items():
+ self.assertEqual(shortestDistance(word,letter),distance)
+
+unittest.main()
diff --git a/challenge-248/pokgopun/python/ch-1.py b/challenge-248/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..edb003520b
--- /dev/null
+++ b/challenge-248/pokgopun/python/ch-1.py
@@ -0,0 +1,59 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-248/
+"""
+
+Task 1: Shortest Distance
+
+Submitted by: [58]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given a string and a character in the given string.
+
+ Write a script to return an array of integers of size same as length of
+ the given string such that:
+distance[i] is the distance from index i to the closest occurence of
+the given character in the given string.
+
+The distance between two indices i and j is abs(i - j).
+
+Example 1
+
+Input: $str = "loveleetcode", $char = "e"
+Output: (3,2,1,0,1,0,0,1,2,2,1,0)
+
+The character 'e' appears at indices 3, 5, 6, and 11 (0-indexed).
+The closest occurrence of 'e' for index 0 is at index 3, so the distance is abs(
+0 - 3) = 3.
+The closest occurrence of 'e' for index 1 is at index 3, so the distance is abs(
+1 - 3) = 2.
+For index 4, there is a tie between the 'e' at index 3 and the 'e' at index 5,
+but the distance is still the same: abs(4 - 3) == abs(4 - 5) = 1.
+The closest occurrence of 'e' for index 8 is at index 6, so the distance is abs(
+8 - 6) = 2.
+
+Example 2
+
+Input: $str = "aaab", $char = "b"
+Output: (3,2,1,0)
+
+Task 2: Submatrix Sum
+"""
+### solution by pokgopun@gmail.com
+
+def shortestDistance(word, letter):
+ l = len(word)
+ letterIndexes = tuple( i for i in range(l) if word[i]==letter )
+ return tuple(
+ min( abs(x-i) for i in letterIndexes ) for x in range(l)
+ )
+
+import unittest
+
+class TestShortestDistance(unittest.TestCase):
+ def test(self):
+ for (word,letter),distance in {
+ ("loveleetcode", "e"): (3,2,1,0,1,0,0,1,2,2,1,0),
+ ("aaab", "b"): (3,2,1,0),
+ }.items():
+ self.assertEqual(shortestDistance(word,letter),distance)
+
+unittest.main()
diff --git a/challenge-248/pokgopun/python/ch-2.py b/challenge-248/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..96572b19a3
--- /dev/null
+++ b/challenge-248/pokgopun/python/ch-2.py
@@ -0,0 +1,91 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-248/
+"""
+
+Task 2: Submatrix Sum
+
+Submitted by: [59]Jorg Sommrey
+ __________________________________________________________________
+
+ You are given a NxM matrix A of integers.
+
+ Write a script to construct a (N-1)x(M-1) matrix B having elements that
+ are the sum over the 2x2 submatrices of A,
+b[i,k] = a[i,k] + a[i,k+1] + a[i+1,k] + a[i+1,k+1]
+
+Example 1
+
+Input: $a = [
+ [1, 2, 3, 4],
+ [5, 6, 7, 8],
+ [9, 10, 11, 12]
+ ]
+
+Output: $b = [
+ [14, 18, 22],
+ [30, 34, 38]
+ ]
+
+Example 2
+
+Input: $a = [
+ [1, 0, 0, 0],
+ [0, 1, 0, 0],
+ [0, 0, 1, 0],
+ [0, 0, 0, 1]
+ ]
+
+Output: $b = [
+ [2, 1, 0],
+ [1, 2, 1],
+ [0, 1, 2]
+ ]
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 24th December
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def submatrixSum(tup: tuple):
+ d = len(tup)
+ l = len(tup[0])
+ stup = tuple()
+ for y in range(d-1):
+ t = tuple()
+ for x in range(l-1):
+ t += ( sum( sum(tup[z][x:x+2]) for z in range(y,y+2) ), )
+ stup += (t,)
+ return stup
+
+import unittest
+
+class TestSubmatrixSum(unittest.TestCase):
+ def test(self):
+ for inpt,otpt in {
+ (
+ (1, 2, 3, 4),
+ (5, 6, 7, 8),
+ (9, 10, 11, 12),
+ ): (
+ (14, 18, 22),
+ (30, 34, 38),
+ ),
+ (
+ (1, 0, 0, 0),
+ (0, 1, 0, 0),
+ (0, 0, 1, 0),
+ (0, 0, 0, 1)
+ ): (
+ (2, 1, 0),
+ (1, 2, 1),
+ (0, 1, 2),
+ ),
+ }.items():
+ self.assertEqual(submatrixSum(inpt),otpt)
+
+unittest.main()
+
+