diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-12-22 11:13:52 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-22 11:13:52 +0000 |
| commit | 736587c37fcdffbb762c23f45269d9e839ae00e6 (patch) | |
| tree | babfa22f1445f4e5e2e21003ed2fcc4afb01af3d | |
| parent | 7de6b12a34afd9d95b624d4db0ca13aeeb8f93a8 (diff) | |
| parent | c9a7c4edb21181d4b1cf70744baa381d8aab2013 (diff) | |
| download | perlweeklychallenge-club-736587c37fcdffbb762c23f45269d9e839ae00e6.tar.gz perlweeklychallenge-club-736587c37fcdffbb762c23f45269d9e839ae00e6.tar.bz2 perlweeklychallenge-club-736587c37fcdffbb762c23f45269d9e839ae00e6.zip | |
Merge pull request #7292 from LoneWolfiNTj/rh196
Robbie Hatley's Perl solutions for challenge 196
| -rwxr-xr-x | challenge-196/LoneWolfiNTj/perl/ch-1.pl | 65 | ||||
| -rwxr-xr-x | challenge-196/LoneWolfiNTj/perl/ch-2.pl | 74 |
2 files changed, 139 insertions, 0 deletions
diff --git a/challenge-196/LoneWolfiNTj/perl/ch-1.pl b/challenge-196/LoneWolfiNTj/perl/ch-1.pl new file mode 100755 index 0000000000..69d6cf3b15 --- /dev/null +++ b/challenge-196/LoneWolfiNTj/perl/ch-1.pl @@ -0,0 +1,65 @@ +#! /usr/bin/perl + +# Robbie Hatley's Perl solution for Challenge 196-1. + +=pod + +Task 1: Pattern 132 +Submitted by: Mohammad S Anwar +Given a list of integers, write a script to find all subsequences which +exhibit Pattern 132 (ie, a sub-sequence, not necessarily contiguous, +a[i], a[j], a[k] such that i < j < k and a[i] < a[k] < a[j]). +If more than one such sub-sequence is found, return the first. +If no such sub-sequence is found, return an empty array. + +Example 1: Input = (3, 1, 4, 2) Output = (1, 4, 2) +Example 2: Input = (1, 2, 3, 4) Output = () +Example 3: Input = (1, 3, 2, 4, 6, 5) Output = (1, 3, 2) +Example 4: Input = (1, 3, 4, 2) Output = (1, 3, 2) + +=cut + +# NOTE: Input is via built-in array of arrays, or command-line args. +# If using args, they should be a space -separated sequence of +# integers, and will be interpreted as being a single array. + +# NOTE: Output is to stdout and will be each input array followed by +# a list of all sub-sequences exhibiting pattern 132, if any. + +# PRELIMINARIES: +use v5.36; + +# DEFAULT INPUT: +my @arrays = +( + [3, 1, 4, 2], + [1, 2, 3, 4], + [1, 3, 2, 4, 6, 5], + [1, 3, 4, 2] +); + +# NON-DEFAULT INPUT: +if (@ARGV) {@arrays = ([@ARGV])} + +# SUBROUTINES: +sub p132 (@array) { + my $size = scalar(@array); + my @p132s; + for ( my $i = 0 ; $i < $size - 2 ; ++$i ) { + for ( my $j = $i + 1 ; $j < $size - 1 ; ++$j ) { + for ( my $k = $j + 1 ; $k < $size - 0 ; ++$k ) { + if ( $array[$i] < $array[$k] && $array[$k] < $array[$j] ) { + push @p132s, [$array[$i], $array[$j], $array[$k]]}}}} + return @p132s} + +# MAIN BODY OF SCRIPT: +for (@arrays) +{ + my @array = @{$_}; + my @p132s = p132(@array); + $"=', '; + say ''; + say "Input array: (@{$_})"; + say "${\scalar(@p132s)} subsequences exhibit pattern 132"; + say "(@{$_})" for @p132s; +} diff --git a/challenge-196/LoneWolfiNTj/perl/ch-2.pl b/challenge-196/LoneWolfiNTj/perl/ch-2.pl new file mode 100755 index 0000000000..b12822728c --- /dev/null +++ b/challenge-196/LoneWolfiNTj/perl/ch-2.pl @@ -0,0 +1,74 @@ +#! /usr/bin/perl + +# Robbie Hatley's Perl solution for Challenge 19-2. + +=pod + +Task 2: Range List +Submitted by: Mohammad S Anwar +Given a finite increasing sequence of unique integers, write a script +to find all sub-sequences consisting of consecutive integers +-- eg, (5, 6, 7) -- and represent each such sub-sequence as +a closed interval [a,b]. For example, the sub-sequence (5, 6, 7) +would be represented as the closed interval [5,7]. + +Example 1: Input = (1,3,4,5,7) Output: ([3,5]) +Example 2: Input = (1,2,3,6,7,9) Output: ([1,3], [6,7]) +Example 3: Input = (0,1,2,4,5,6,8,9) Output: ([0,2], [4,6], [8,9]) + +=cut + +# NOTE: Input is via a built-in array of arrays, or command-line args. +# If using args, they should be a space-separated increasing +# sequence of unique integers, and will be iterpretted as being +# a single array. + +# NOTE: Output is to stdout and will be each input array followed by +# a list of all "ranges" found in the input array, expressed as +# closed intervals [a,b]. + +# PRELIMINARIES: +use v5.36; + +# SUBROUTINES: +sub ranges(@array) +{ + my @ranges; + for ( my $idx = 0 ; $idx < $#array ; ++$idx ) + { + my $range = $idx + 1; + for ( ; $range <= $#array ; ++$range ) + { + last if $array[$range] != $array[$range - 1] + 1; + } + if ( $range > $idx + 1 ) + { + push @ranges, [$array[$idx],$array[$range-1]]; + $idx = $range - 1; + } + } + return @ranges; +} + +# DEFAULT INPUT: +my @arrays = +( + [1,3,4,5,7], + [1,2,3,6,7,9], + [0,1,2,4,5,6,8,9] +); + +# NON-DEFAULT INPUT: +if (@ARGV) {@arrays = ([@ARGV])} + +# MAIN BODY OF SCRIPT: +for (@arrays) +{ + my @array = @{$_}; + my @ranges = ranges(@array); + my @range_strings = map {sprintf("[%d,%d]",$_->[0],$_->[1])} @ranges; + $"=', '; + say ''; + say "Input array = (@array)"; + say "Ranges = @range_strings" +} |
