From a3131963dafd86a62a1910561cbda9151af6bd13 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Sun, 25 Dec 2022 10:19:59 -0500 Subject: initial commit --- challenge-196/adam-russell/perl/ch-1.pl | 26 ++++++++++++++++++++++++++ challenge-196/adam-russell/perl/ch-2.pl | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 challenge-196/adam-russell/perl/ch-1.pl create mode 100644 challenge-196/adam-russell/perl/ch-2.pl diff --git a/challenge-196/adam-russell/perl/ch-1.pl b/challenge-196/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..06da89b2e0 --- /dev/null +++ b/challenge-196/adam-russell/perl/ch-1.pl @@ -0,0 +1,26 @@ +use v5.36; +## +# You are given a list of integers, @list. +# Write a script to find the subsequence that respects Pattern 132. +# Return an empty array if none found. +## +use Data::Dump q/pp/; +sub pattern_132{ + # i < j < k and a[i] < a[k] < a[j] + my @list= @_; + my @subsequences; + for my $i (0 .. @list - 1){ + for my $j (0 .. @list - 1){ + for my $k (0 .. @list - 1){ + push @subsequences, [$list[$i], $list[$j], $list[$k]] if $i < $j && $j < $k && $list[$i] < $list[$k] && $list[$k] < $list[$j]; + } + } + } + return $subsequences[0]; +} + +MAIN:{ + say pp pattern_132(3, 1, 4, 2); + say pp pattern_132(1, 3, 2, 4, 6, 5); + say pp pattern_132(1, 3, 4, 2); +} diff --git a/challenge-196/adam-russell/perl/ch-2.pl b/challenge-196/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..a931ac65f9 --- /dev/null +++ b/challenge-196/adam-russell/perl/ch-2.pl @@ -0,0 +1,33 @@ +use v5.36; +## +# 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). +## +use Data::Dump q/pp/; +sub range_list{ + my @numbers = @_; + my %ranges; + my @ranges; + for my $i (0 .. @numbers - 2){ + if($numbers[$i] == $numbers[$i + 1] - 1){ + $ranges{$numbers[$i]} = undef; + $ranges{$numbers[$i + 1]} = undef; + } + if((keys %ranges) > 0 && $numbers[$i] != $numbers[$i + 1] - 1){ + push @ranges, [sort {$a <=> $b} keys %ranges]; + %ranges = (); + } + } + if((keys %ranges) > 0){ + push @ranges, [sort {$a <=> $b} keys %ranges]; + } + @ranges = map { [$_->[0], $_->[@{$_} - 1]] } @ranges; + return @ranges; +} + +MAIN:{ + say pp range_list(1, 3, 4, 5, 7); + say pp range_list(1, 2, 3, 6, 7, 9); + say pp range_list(0, 1, 2, 4, 5, 6, 8, 9); +} -- cgit