aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-12-20 09:31:07 +0000
committerGitHub <noreply@github.com>2022-12-20 09:31:07 +0000
commitfc3de4489ee3ea953756cf5730c8754ed5a26b5b (patch)
treea1f89e487df89dd98b785257f60e72c0fe83038b
parent7c61e50191c9ea07177c660ff3214d358c759411 (diff)
parent59e68fd44ac07d019225b01a7af4db0fa9970385 (diff)
downloadperlweeklychallenge-club-fc3de4489ee3ea953756cf5730c8754ed5a26b5b.tar.gz
perlweeklychallenge-club-fc3de4489ee3ea953756cf5730c8754ed5a26b5b.tar.bz2
perlweeklychallenge-club-fc3de4489ee3ea953756cf5730c8754ed5a26b5b.zip
Merge pull request #7284 from PerlBoy1967/branch-for-challenge-196
w196 - Task 1 & 2
-rwxr-xr-xchallenge-196/perlboy1967/perl/ch-1.pl55
-rwxr-xr-xchallenge-196/perlboy1967/perl/ch-2.pl53
2 files changed, 108 insertions, 0 deletions
diff --git a/challenge-196/perlboy1967/perl/ch-1.pl b/challenge-196/perlboy1967/perl/ch-1.pl
new file mode 100755
index 0000000000..8587eee3ab
--- /dev/null
+++ b/challenge-196/perlboy1967/perl/ch-1.pl
@@ -0,0 +1,55 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 196
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-195/#TASK1
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Pattern 132
+Submitted by: Mohammad S Anwar
+
+You are given a list of integers, @list.
+
+Write a script to find out subsequence that respect Pattern 132. Return empty array if none found.
+
+|| Pattern 132 in a sequence (a[i], a[j], a[k]) such that i < j < k and a[i] < a[k] < a[j].
+
+=cut
+
+use v5.16;
+use common::sense;
+
+use Test::More;
+use Test::Deep qw(cmp_deeply);
+
+sub pattern132 {
+ my @a = @{$_[0]};
+
+ while (@a >= 3) {
+ my $a = shift @a; my @b = @a;
+ while (@b >= 2) {
+ my $b = shift @b; my @c = @b;
+ while (@c >= 1) {
+ my $c = shift @c;
+ return [$a,$b,$c] if ($a < $c and $c < $b);
+ }
+ }
+ }
+
+ return [];
+}
+
+for (
+ [[3, 1, 4, 2], [1, 4, 2]],
+ [[1, 2, 3, 4], []],
+ [[1, 3, 2, 4, 6, 5], [1, 3, 2]],
+ [[1, 3, 4, 2], [1, 3, 2]],
+) {
+ cmp_deeply(pattern132($_->[0]),
+ $_->[1],
+ sprintf('test [%s]',join(',',@{$_->[0]})));
+}
+
+done_testing;
diff --git a/challenge-196/perlboy1967/perl/ch-2.pl b/challenge-196/perlboy1967/perl/ch-2.pl
new file mode 100755
index 0000000000..1152b277db
--- /dev/null
+++ b/challenge-196/perlboy1967/perl/ch-2.pl
@@ -0,0 +1,53 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 196
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-195/#TASK2
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Range List
+Submitted by: Mohammad S Anwar
+
+You are given a sorted unique integer array, @array.
+
+Write a script to find all possible Number Range i.e [x, y] represent range all integers from x and y (both inclusive).
+
+|| Each subsequence of two or more contiguous integers
+
+=cut
+
+use v5.16;
+use common::sense;
+
+use List::MoreUtils qw(slide);
+
+use Test::More;
+use Test::Deep qw(cmp_deeply);
+
+sub rangeList (@) {
+ my @r = ([$_[0]]);
+
+ slide {
+ if ($a == $b - 1) {
+ push(@{$r[-1]},$b);
+ } else {
+ push(@r,[$b]);
+ }
+ } @_;
+
+ [map { [$$_[0],$$_[-1]] } grep { scalar @$_ > 1 } @r];
+}
+
+for (
+ [[1,3,4,5,7], [[3,5]]],
+ [[1,2,3,6,7,9], [[1,3],[6,7]]],
+ [[0,1,2,4,5,6,8,9], [[0,2],[4,6],[8,9]]],
+) {
+ cmp_deeply(rangeList(@{$_->[0]}),
+ [@{$_->[1]}],
+ sprintf('test [%s]',join(',',@{$_->[0]})));
+}
+
+done_testing;