From 59e68fd44ac07d019225b01a7af4db0fa9970385 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 19 Dec 2022 17:24:08 +0000 Subject: w196 - Task 1 & 2 --- challenge-196/perlboy1967/perl/ch-1.pl | 55 ++++++++++++++++++++++++++++++++++ challenge-196/perlboy1967/perl/ch-2.pl | 53 ++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100755 challenge-196/perlboy1967/perl/ch-1.pl create mode 100755 challenge-196/perlboy1967/perl/ch-2.pl 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; -- cgit