diff options
| author | Adam Russell <ac.russell@live.com> | 2022-12-25 10:19:59 -0500 |
|---|---|---|
| committer | Adam Russell <ac.russell@live.com> | 2022-12-25 10:19:59 -0500 |
| commit | a3131963dafd86a62a1910561cbda9151af6bd13 (patch) | |
| tree | c2236934a2f9e5eb8a82c4fb6eeafaef7f91af33 | |
| parent | 80b02f0b8318ed6894e5f80f8fce6df973288d44 (diff) | |
| download | perlweeklychallenge-club-a3131963dafd86a62a1910561cbda9151af6bd13.tar.gz perlweeklychallenge-club-a3131963dafd86a62a1910561cbda9151af6bd13.tar.bz2 perlweeklychallenge-club-a3131963dafd86a62a1910561cbda9151af6bd13.zip | |
initial commit
| -rw-r--r-- | challenge-196/adam-russell/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-196/adam-russell/perl/ch-2.pl | 33 |
2 files changed, 59 insertions, 0 deletions
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); +} |
