diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-12-26 01:42:39 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-12-26 01:42:39 +0000 |
| commit | 0ea16ba5c3a0e2a2576fd486ee06cb4eaf14324c (patch) | |
| tree | efc16ed13a6245e90245b114e169485d3609b9e9 /challenge-196 | |
| parent | 60466a9b1fd344af505fe3cc5b6cdaf1842457e3 (diff) | |
| download | perlweeklychallenge-club-0ea16ba5c3a0e2a2576fd486ee06cb4eaf14324c.tar.gz perlweeklychallenge-club-0ea16ba5c3a0e2a2576fd486ee06cb4eaf14324c.tar.bz2 perlweeklychallenge-club-0ea16ba5c3a0e2a2576fd486ee06cb4eaf14324c.zip | |
- Added solutions by Jaldhar H. Vyas.
- Added blog post by Roger Bell_West.
- Added solutions by Adam Russell.
- Added solutions by Cheok-Yin Fung.
- Added solutions by Bruce Gray.
- Added solutions by Arne Sommer.
Diffstat (limited to 'challenge-196')
| -rw-r--r-- | challenge-196/colin-crain/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-196/colin-crain/perl/ch-1.pl | 79 | ||||
| -rwxr-xr-x | challenge-196/colin-crain/perl/ch-2.pl | 108 |
3 files changed, 188 insertions, 0 deletions
diff --git a/challenge-196/colin-crain/blog.txt b/challenge-196/colin-crain/blog.txt new file mode 100644 index 0000000000..f6b74c6c5f --- /dev/null +++ b/challenge-196/colin-crain/blog.txt @@ -0,0 +1 @@ +https://colincrain.com/2022/12/25/range-rover-over-clover diff --git a/challenge-196/colin-crain/perl/ch-1.pl b/challenge-196/colin-crain/perl/ch-1.pl new file mode 100755 index 0000000000..80d36a56dd --- /dev/null +++ b/challenge-196/colin-crain/perl/ch-1.pl @@ -0,0 +1,79 @@ +#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# room-132.pl
+#
+# 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].
+#
+# Example 1
+# Input: @list = (3, 1, 4, 2)
+# Output: (1, 4, 2) respect the Pattern 132.
+#
+# Example 2
+# Input: @list = (1, 2, 3, 4)
+# Output: () since no susbsequence can be found.
+#
+# Example 3
+# Input: @list = (1, 3, 2, 4, 6, 5)
+# Output: (1, 3, 2) if more than one subsequence found then return the first.
+#
+# Example 4
+# Input: @list = (1, 3, 4, 2)
+# Output: (1, 3, 2)
+#
+# method
+#
+# rearranging the pattern, I think it will be easier to search
+# backwards from the third element instead of looking ahead from
+# the first. Which means we can iterate across the input array
+# starting at the third elemnet, then, backtracking to the first,
+# find the first element less than the value at the start, and then
+# continue closing from that point forward intil we are back to the
+# start position, looking for a value greater than the start. If we
+# find both other elements we will have found the leftmost match of
+# the pattern.
+
+
+#
+#
+# © 2022 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+
+my @array = (1, 3, 4, 2);
+my @match;
+my ($first, $second, $third);
+
+OUTER: for my $start (2..$#array) {
+ for my $idx (0..$start-1) {
+ $first = $array[$idx] if $array[$idx] < $array[$start];
+ if (defined($first) && ($array[$idx] > $array[$start])) {
+ @match = ($first, $array[$idx], $array[$start]);
+ last OUTER;
+ }
+ }
+ undef $first;
+}
+
+print '(', (join ', ', @match), ')';
+
+
+
+
+
diff --git a/challenge-196/colin-crain/perl/ch-2.pl b/challenge-196/colin-crain/perl/ch-2.pl new file mode 100755 index 0000000000..aac3c0b371 --- /dev/null +++ b/challenge-196/colin-crain/perl/ch-2.pl @@ -0,0 +1,108 @@ +#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# down-range.pl
+#
+# 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
+#
+# Example 1
+# Input: @array = (1,3,4,5,7)
+# Output: [3,5]
+#
+# Example 2
+# Input: @array = (1,2,3,6,7,9)
+# Output: [1,3], [6,7]
+#
+# Example 3
+# Input: @array = (0,1,2,4,5,6,8,9)
+# Output: [0,2], [4,6], [8,9]
+
+# breakdown
+#
+# I had to read this and study the examples several times before I
+# got the jist of what was being requested here. Rephrased, we are
+# given a list of ascending integers, and are asked to isolate out
+# contiguous runs of sequental values that can be represented with
+# ranges, and return the ranges.
+#
+# Ranges fro the purposes of discussion are assumed to have the
+# most restrained and basic definition: sequential ascending values
+# differing by 1. No funny stuff.
+#
+# An immediastely useful observation that will make our search
+# simpler is that the ranges cannot overlap or even abut, and this
+# will immediately stimulate reverse mitosis and the two ranges
+# will spontaniously merge into one single larger element. So as we
+# look for ranges, we know they will have distinct boundaries.
+#
+# method
+#
+# The essental pattern to recognize here is two seqential elements
+# in the input array that increase in value by 1. If we walk the
+# list from left to right, then, if the next element differs from
+# the previous by 1, it is in a range with it, and if the
+# difference is any other number it is not.
+#
+# What we can do, then, is iterate over the indices of the target
+# array from 0 to the second-to-last. Looking at each index value,
+# if the next index value is one more we have a range, with a low
+# of the current index and a high of the next. Moving to the next,
+# if the difference is 1 again we extend the working range making
+# that value the new high. If it is not in the range, however, the
+# current range is closed as-is and stored away or even printed
+# immediately, and then the working range is wiped clean.
+#
+# After evaluating the second-to-last index, if we still have a
+# working range it is added to the output, but if that range is nil
+# it is left off.
+#
+# In this way we can process the whole array in one pass.
+#
+#
+# I seem to recall another challenge that was solvable using a very
+# similar strategy, perhaps with intervals.
+
+
+#
+# © 2022 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+
+
+my @array = (0,1,2,4,5,6,8,9);
+my @working;
+my @ranges;
+
+for (0..$#array-1) {
+ if ($array[$_+1] - $array[$_] == 1) {
+ $working[0] //= $array[$_ ];
+ $working[1] = $array[$_+1];
+ next;
+ }
+ push @ranges, [ @working ] if @working;
+ @working = ();
+}
+
+push @ranges, [ @working ] if @working;
+
+
+say q([) . (join ',', $_->@*) . q(]) for @ranges;
+
+
+
+
|
