aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-24 10:13:41 +0100
committerGitHub <noreply@github.com>2024-09-24 10:13:41 +0100
commit68a82c0ebb581e710d1bca4615a935fa1c0949fc (patch)
treee4204b787e4a7f8de97c3cb86c4a97a4fa976f1c
parent9ced81d32e594001ffe943db9363bb48a4583d67 (diff)
parent915160b5e8bee9e5a712a9fcc6b047b788c78828 (diff)
downloadperlweeklychallenge-club-68a82c0ebb581e710d1bca4615a935fa1c0949fc.tar.gz
perlweeklychallenge-club-68a82c0ebb581e710d1bca4615a935fa1c0949fc.tar.bz2
perlweeklychallenge-club-68a82c0ebb581e710d1bca4615a935fa1c0949fc.zip
Merge pull request #10902 from pauloscustodio/master
Add solutions to challenge 288
-rw-r--r--challenge-067/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-072/paulo-custodio/python/ch-2.py2
-rw-r--r--challenge-073/paulo-custodio/perl/ch-1.pl2
-rw-r--r--challenge-073/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-073/paulo-custodio/python/ch-1.py36
-rw-r--r--challenge-073/paulo-custodio/python/ch-2.py51
-rw-r--r--challenge-288/paulo-custodio/Makefile2
-rw-r--r--challenge-288/paulo-custodio/perl/ch-1.pl59
-rw-r--r--challenge-288/paulo-custodio/perl/ch-2.pl97
-rw-r--r--challenge-288/paulo-custodio/python/ch-1.py55
-rw-r--r--challenge-288/paulo-custodio/python/ch-2.py86
-rw-r--r--challenge-288/paulo-custodio/t/test-1.yaml20
-rw-r--r--challenge-288/paulo-custodio/t/test-2.yaml15
13 files changed, 425 insertions, 4 deletions
diff --git a/challenge-067/paulo-custodio/perl/ch-2.pl b/challenge-067/paulo-custodio/perl/ch-2.pl
index bd12104b1f..940e427f8e 100644
--- a/challenge-067/paulo-custodio/perl/ch-2.pl
+++ b/challenge-067/paulo-custodio/perl/ch-2.pl
@@ -2,7 +2,7 @@
# Challenge 067
#
-# TASK #2 › Letter Phone
+# TASK #2 > Letter Phone
# Submitted by: Mohammad S Anwar
#
# You are given a digit string $S. Write a script to print all possible letter
diff --git a/challenge-072/paulo-custodio/python/ch-2.py b/challenge-072/paulo-custodio/python/ch-2.py
index 6d897d5b2b..5cace8e5eb 100644
--- a/challenge-072/paulo-custodio/python/ch-2.py
+++ b/challenge-072/paulo-custodio/python/ch-2.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env perl
+#!/usr/bin/env python3
# Challenge 072
#
diff --git a/challenge-073/paulo-custodio/perl/ch-1.pl b/challenge-073/paulo-custodio/perl/ch-1.pl
index 734bef2566..904c257166 100644
--- a/challenge-073/paulo-custodio/perl/ch-1.pl
+++ b/challenge-073/paulo-custodio/perl/ch-1.pl
@@ -2,7 +2,7 @@
# Challenge 073
#
-# TASK #1 › Min Sliding Window
+# TASK #1 > Min Sliding Window
# Submitted by: Mohammad S Anwar
# You are given an array of integers @A and sliding window size $S.
#
diff --git a/challenge-073/paulo-custodio/perl/ch-2.pl b/challenge-073/paulo-custodio/perl/ch-2.pl
index b82a968623..c670224942 100644
--- a/challenge-073/paulo-custodio/perl/ch-2.pl
+++ b/challenge-073/paulo-custodio/perl/ch-2.pl
@@ -2,7 +2,7 @@
# Challenge 073
#
-# TASK #2 › Smallest Neighbour
+# TASK #2 > Smallest Neighbour
# Submitted by: Mohammad S Anwar
# You are given an array of integers @A.
#
diff --git a/challenge-073/paulo-custodio/python/ch-1.py b/challenge-073/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..fb86229a6c
--- /dev/null
+++ b/challenge-073/paulo-custodio/python/ch-1.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python3
+
+# Challenge 073
+#
+# TASK #1 > Min Sliding Window
+# Submitted by: Mohammad S Anwar
+# You are given an array of integers @A and sliding window size $S.
+#
+# Write a script to create an array of min from each sliding window.
+#
+# Example
+# Input: @A = (1, 5, 0, 2, 9, 3, 7, 6, 4, 8) and $S = 3
+# Output: (0, 0, 0, 2, 3, 3, 4, 4)
+#
+# [(1 5 0) 2 9 3 7 6 4 8] = Min (0)
+# [1 (5 0 2) 9 3 7 6 4 8] = Min (0)
+# [1 5 (0 2 9) 3 7 6 4 8] = Min (0)
+# [1 5 0 (2 9 3) 7 6 4 8] = Min (2)
+# [1 5 0 2 (9 3 7) 6 4 8] = Min (3)
+# [1 5 0 2 9 (3 7 6) 4 8] = Min (3)
+# [1 5 0 2 9 3 (7 6 4) 8] = Min (4)
+# [1 5 0 2 9 3 7 (6 4 8)] = Min (4)
+
+import sys
+
+def min_sliding_window(s, a):
+ min_ = []
+ for i in range(0, len(a)-s+1):
+ sub_ = a[i:i+s]
+ min_.append(min(sub_))
+ return min_
+
+S = int(sys.argv[1])
+M = list(map(int, sys.argv[2:]))
+min_ = min_sliding_window(S, M)
+print(", ".join([str(x) for x in min_]))
diff --git a/challenge-073/paulo-custodio/python/ch-2.py b/challenge-073/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..8d28dc627f
--- /dev/null
+++ b/challenge-073/paulo-custodio/python/ch-2.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python3
+
+# Challenge 073
+#
+# TASK #2 > Smallest Neighbour
+# Submitted by: Mohammad S Anwar
+# You are given an array of integers @A.
+#
+# Write a script to create an array that represents the smallest element to
+# the left of each corresponding index. If none found then use 0.
+#
+# Example 1
+# Input: @A = (7, 8, 3, 12, 10)
+# Output: (0, 7, 0, 3, 3)
+#
+# For index 0, the smallest number to the left of $A[0] i.e. 7 is none, so we
+# put 0.
+# For index 1, the smallest number to the left of $A[1] as compare to 8, in
+# (7) is 7 so we put 7.
+# For index 2, the smallest number to the left of $A[2] as compare to 3, in
+# (7, 8) is none, so we put 0.
+# For index 3, the smallest number to the left of $A[3] as compare to 12, in
+# (7, 8, 3) is 3, so we put 3.
+# For index 4, the smallest number to the left of $A[4] as compare to 10, in
+# (7, 8, 3, 12) is 3, so we put 3 again.
+#
+# Example 2
+# Input: @A = (4, 6, 5)
+# Output: (0, 4, 4)
+#
+# For index 0, the smallest number to the left of $A[0] is none, so we put 0.
+# For index 1, the smallest number to the left of $A[1] as compare to 6, in
+# (4) is 4, so we put 4.
+# For index 2, the smallest number to the left of $A[2] as compare to 5, in
+# (4, 6) is 4, so we put 4 again.
+
+import sys
+
+def smallest_left(a):
+ smallest = [0]
+ for i in range(1, len(a)):
+ min_ = min(a[:i])
+ if min_ < a[i]:
+ smallest.append(min_)
+ else:
+ smallest.append(0)
+ return smallest
+
+A = list(map(int, sys.argv[1:]))
+smallest = smallest_left(A)
+print(", ".join([str(x) for x in smallest]))
diff --git a/challenge-288/paulo-custodio/Makefile b/challenge-288/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-288/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-288/paulo-custodio/perl/ch-1.pl b/challenge-288/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..51a042a37f
--- /dev/null
+++ b/challenge-288/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl
+
+# Challenge 288
+#
+# Task 1: Closest Palindrome
+# Submitted by: Mohammad Sajid Anwar
+# You are given a string, $str, which is an integer.
+#
+# Write a script to find out the closest palindrome, not including itself.
+# If there are more than one then return the smallest.
+#
+# The closest is defined as the absolute difference minimized between two
+# integers.
+#
+# Example 1
+# Input: $str = "123"
+# Output: "121"
+# Example 2
+# Input: $str = "2"
+# Output: "1"
+#
+# There are two closest palindrome "1" and "3". Therefore we return the smallest "1".
+# Example 3
+# Input: $str = "1400"
+# Output: "1441"
+# Example 4
+# Input: $str = "1001"
+# Output: "999"
+
+use Modern::Perl;
+
+sub is_palindrome {
+ my($n) = @_;
+ return "$n" eq join("", reverse split //, "$n");
+}
+
+sub next_palindrome {
+ my($n) = @_;
+ my $out;
+ for (my $i = 1; !defined($out) || $i <= $n; $i++) {
+ my $t = $n-$i;
+ if ($t >= 0 && is_palindrome($t)) {
+ if (!defined($out) || abs($out-$n) > abs($t-$n)) {
+ $out = $t;
+ }
+ }
+
+ $t = $n+$i;
+ if (is_palindrome($t)) {
+ if (!defined($out) || abs($out-$n) > abs($t-$n)) {
+ $out = $t;
+ }
+ }
+ }
+ return $out;
+}
+
+my $n = shift // 0;
+say next_palindrome($n);
diff --git a/challenge-288/paulo-custodio/perl/ch-2.pl b/challenge-288/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..fccda9c1cf
--- /dev/null
+++ b/challenge-288/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,97 @@
+#!/usr/bin/env perl
+
+# Challenge 288
+#
+# Task 2: Contiguous Block
+# Submitted by: Peter Campbell Smith
+# You are given a rectangular matrix where all the cells contain either x or o.
+#
+# Write a script to determine the size of the largest contiguous block.
+#
+# A contiguous block consists of elements containing the same symbol which
+# share an edge (not just a corner) with other elements in the block, and where
+# there is a path between any two of these elements that crosses only those
+# shared edges.
+#
+# Example 1
+# Input: $matrix = [
+# ['x', 'x', 'x', 'x', 'o'],
+# ['x', 'o', 'o', 'o', 'o'],
+# ['x', 'o', 'o', 'o', 'o'],
+# ['x', 'x', 'x', 'o', 'o'],
+# ]
+# Ouput: 11
+#
+# There is a block of 9 contiguous cells containing 'x'.
+# There is a block of 11 contiguous cells containing 'o'.
+# Example 2
+# Input: $matrix = [
+# ['x', 'x', 'x', 'x', 'x'],
+# ['x', 'o', 'o', 'o', 'o'],
+# ['x', 'x', 'x', 'x', 'o'],
+# ['x', 'o', 'o', 'o', 'o'],
+# ]
+# Ouput: 11
+#
+# There is a block of 11 contiguous cells containing 'x'.
+# There is a block of 9 contiguous cells containing 'o'.
+# Example 3
+# Input: $matrix = [
+# ['x', 'x', 'x', 'o', 'o'],
+# ['o', 'o', 'o', 'x', 'x'],
+# ['o', 'x', 'x', 'o', 'o'],
+# ['o', 'o', 'o', 'x', 'x'],
+# ]
+# Ouput: 7
+#
+# There is a block of 7 contiguous cells containing 'o'.
+# There are two other 2-cell blocks of 'o'.
+# There are three 2-cell blocks of 'x' and one 3-cell.
+
+use Modern::Perl;
+
+my $SEEN = ' ';
+
+sub parse_matrix {
+ my(@m) = @_;
+ @m = map {[split //, $_]} @m;
+ return @m;
+}
+
+sub size_block {
+ my($m, $ch, $r, $c) = @_;
+ $m->[$r][$c] = $SEEN;
+ my $count = 1;
+ if ($r-1 >= 0 && $m->[$r-1][$c] eq $ch) {
+ $count += size_block($m, $ch, $r-1, $c);
+ }
+ if ($r+1 < @$m && $m->[$r+1][$c] eq $ch) {
+ $count += size_block($m, $ch, $r+1, $c);
+ }
+ if ($c-1 >= 0 && $m->[$r][$c-1] eq $ch) {
+ $count += size_block($m, $ch, $r, $c-1);
+ }
+ if ($c+1 < @{$m->[0]} && $m->[$r][$c+1] eq $ch) {
+ $count += size_block($m, $ch, $r, $c+1);
+ }
+ return $count;
+}
+
+sub max_block {
+ my(@m) = @_;
+ my $max_block = 0;
+ for my $r (0 .. @m-1) {
+ for my $c (0 .. @{$m[0]}-1) {
+ next if $m[$r][$c] eq $SEEN;
+ my $block = size_block(\@m, $m[$r][$c], $r, $c);
+ if ($block > $max_block) {
+ $max_block = $block;
+ }
+ }
+ }
+ return $max_block;
+}
+
+my @m = parse_matrix(@ARGV);
+my $max_block = max_block(@m);
+say $max_block;
diff --git a/challenge-288/paulo-custodio/python/ch-1.py b/challenge-288/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..168d6ce085
--- /dev/null
+++ b/challenge-288/paulo-custodio/python/ch-1.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+
+# Challenge 288
+#
+# Task 1: Closest Palindrome
+# Submitted by: Mohammad Sajid Anwar
+# You are given a string, $str, which is an integer.
+#
+# Write a script to find out the closest palindrome, not including itself.
+# If there are more than one then return the smallest.
+#
+# The closest is defined as the absolute difference minimized between two
+# integers.
+#
+# Example 1
+# Input: $str = "123"
+# Output: "121"
+# Example 2
+# Input: $str = "2"
+# Output: "1"
+#
+# There are two closest palindrome "1" and "3". Therefore we return the smallest "1".
+# Example 3
+# Input: $str = "1400"
+# Output: "1441"
+# Example 4
+# Input: $str = "1001"
+# Output: "999"
+
+import sys
+
+def is_palindrome(n):
+ return str(n) == str(n)[::-1]
+
+def next_palindrome(n):
+ out = None
+ i = 1
+ while out is None or i <= n:
+ t = n-i
+ if t >= 0 and is_palindrome(t):
+ if out is None:
+ out = t
+ elif abs(out-n) > abs(t-n):
+ out = t
+ t = n+i
+ if is_palindrome(t):
+ if out is None:
+ out = t
+ elif abs(out-n) > abs(t-n):
+ out = t
+ i += 1
+ return out
+
+n = int(sys.argv[1])
+print(next_palindrome(n))
diff --git a/challenge-288/paulo-custodio/python/ch-2.py b/challenge-288/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..43b4407ab8
--- /dev/null
+++ b/challenge-288/paulo-custodio/python/ch-2.py
@@ -0,0 +1,86 @@
+#!/usr/bin/env python3
+
+# Challenge 288
+#
+# Task 2: Contiguous Block
+# Submitted by: Peter Campbell Smith
+# You are given a rectangular matrix where all the cells contain either x or o.
+#
+# Write a script to determine the size of the largest contiguous block.
+#
+# A contiguous block consists of elements containing the same symbol which
+# share an edge (not just a corner) with other elements in the block, and where
+# there is a path between any two of these elements that crosses only those
+# shared edges.
+#
+# Example 1
+# Input: $matrix = [
+# ['x', 'x', 'x', 'x', 'o'],
+# ['x', 'o', 'o', 'o', 'o'],
+# ['x', 'o', 'o', 'o', 'o'],
+# ['x', 'x', 'x', 'o', 'o'],
+# ]
+# Ouput: 11
+#
+# There is a block of 9 contiguous cells containing 'x'.
+# There is a block of 11 contiguous cells containing 'o'.
+# Example 2
+# Input: $matrix = [
+# ['x', 'x', 'x', 'x', 'x'],
+# ['x', 'o', 'o', 'o', 'o'],
+# ['x', 'x', 'x', 'x', 'o'],
+# ['x', 'o', 'o', 'o', 'o'],
+# ]
+# Ouput: 11
+#
+# There is a block of 11 contiguous cells containing 'x'.
+# There is a block of 9 contiguous cells containing 'o'.
+# Example 3
+# Input: $matrix = [
+# ['x', 'x', 'x', 'o', 'o'],
+# ['o', 'o', 'o', 'x', 'x'],
+# ['o', 'x', 'x', 'o', 'o'],
+# ['o', 'o', 'o', 'x', 'x'],
+# ]
+# Ouput: 7
+#
+# There is a block of 7 contiguous cells containing 'o'.
+# There are two other 2-cell blocks of 'o'.
+# There are three 2-cell blocks of 'x' and one 3-cell.
+
+import sys
+
+SEEN = ' '
+
+def parse_matrix(lines):
+ m = [[x for x in line] for line in lines]
+ return m
+
+def calc_max_block(m):
+ def size_block(m, ch, r, c):
+ m[r][c] = SEEN
+ count = 1
+ if r-1 >= 0:
+ if m[r-1][c] == ch:
+ count += size_block(m, ch, r-1, c)
+ if r+1 < len(m):
+ if m[r+1][c] == ch:
+ count += size_block(m, ch, r+1, c)
+ if c-1 >= 0:
+ if m[r][c-1] == ch:
+ count += size_block(m, ch, r, c-1)
+ if c+1 < len(m[0]):
+ if m[r][c+1] == ch:
+ count += size_block(m, ch, r, c+1)
+ return count
+
+ max_block = 0
+ for r in range(len(m)):
+ for c in range(len(m[0])):
+ if m[r][c] != SEEN:
+ block = size_block(m, m[r][c], r, c)
+ max_block = max(max_block, block)
+ return max_block
+
+m = parse_matrix(sys.argv[1:])
+print(calc_max_block(m))
diff --git a/challenge-288/paulo-custodio/t/test-1.yaml b/challenge-288/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..b54e9c8e52
--- /dev/null
+++ b/challenge-288/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 123
+ input:
+ output: 121
+- setup:
+ cleanup:
+ args: 2
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 1400
+ input:
+ output: 1441
+- setup:
+ cleanup:
+ args: 1001
+ input:
+ output: 999
diff --git a/challenge-288/paulo-custodio/t/test-2.yaml b/challenge-288/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..1c326b6caa
--- /dev/null
+++ b/challenge-288/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: xxxxo xoooo xoooo xxxoo
+ input:
+ output: 11
+- setup:
+ cleanup:
+ args: xxxxx xoooo xxxxo xoooo
+ input:
+ output: 11
+- setup:
+ cleanup:
+ args: xxxoo oooxx oxxoo oooxx
+ input:
+ output: 7