diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-12-26 01:30:38 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-26 01:30:38 +0000 |
| commit | 79d42861f1abb04586ba6c358fc3b4b99d6ece86 (patch) | |
| tree | 2115bd54a4529a6bfcab893fde8d6710dc83417d | |
| parent | 91b19b7925bf6cacf17b6c61cd5fbe37f945a8e2 (diff) | |
| parent | 4b1847b66c214d9b19d5020776b60414a8a44b3b (diff) | |
| download | perlweeklychallenge-club-79d42861f1abb04586ba6c358fc3b4b99d6ece86.tar.gz perlweeklychallenge-club-79d42861f1abb04586ba6c358fc3b4b99d6ece86.tar.bz2 perlweeklychallenge-club-79d42861f1abb04586ba6c358fc3b4b99d6ece86.zip | |
Merge pull request #7304 from E7-87-83/newt
Week 196
| -rw-r--r-- | challenge-196/cheok-yin-fung/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-196/cheok-yin-fung/perl/ch-2.pl | 30 |
2 files changed, 57 insertions, 0 deletions
diff --git a/challenge-196/cheok-yin-fung/perl/ch-1.pl b/challenge-196/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..7c65687c88 --- /dev/null +++ b/challenge-196/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,27 @@ +# The Weekly Challenge 196 +# Task 1 Pattern 132 +use v5.30.0; +use warnings; + +sub one32 { + my @a = @_; + for my $i (0..$#a-2) { + for my $j ($i+2..$#a) { + if ($a[$j] > $a[$i]) { + for my $k ($i+1..$j-1) { + return [$a[$i], $a[$k], $a[$j]] if $a[$k] > $a[$j]; + } + } + } + } + return []; +} + + +use Test::More tests=>4; +use Test::Deep; + +cmp_deeply one32(3,1,4,2), [1,4,2]; +cmp_deeply one32(1,2,3,4), []; +cmp_deeply one32(1,3,2,4,6,5), [1,3,2]; +cmp_deeply one32(1,3,4,2), [1,3,2]; diff --git a/challenge-196/cheok-yin-fung/perl/ch-2.pl b/challenge-196/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..e658212abd --- /dev/null +++ b/challenge-196/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,30 @@ +# The Weekly Challenge 196 +# Task 2 Range List +use v5.30.0; +use warnings; + +sub rl { + my @a = @_; + my @rl; + my $range = [$a[0], $a[0]]; + my $i = 1; + while ($i <= $#a) { + if ($a[$i] == $a[$i-1]+1) { + $range->[1]++; + } else { + push @rl, $range if $range->[0] < $range->[1]; + $range = [$a[$i], $a[$i]]; + } + $i++; + } + push @rl, $range if $range->[0] < $range->[1]; + return [@rl]; +} + + +use Test::More tests=>3; +use Test::Deep; + +cmp_deeply rl(1,3,4,5,7), [[3,5]]; +cmp_deeply rl(1,2,3,6,7,9), [[1,3], [6,7]]; +cmp_deeply rl(0,1,2,4,5,6,8,9), [[0,2], [4,6], [8,9]]; |
