aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYsmael Ebreo <Ysmael.Ebreo@latticesemi.com>2020-04-09 21:13:34 +0800
committerYsmael Ebreo <Ysmael.Ebreo@latticesemi.com>2020-04-09 21:13:34 +0800
commitbdf32ca7c1e46ab34f485298a530da59047abe39 (patch)
tree3604e3c2ae3b01d514b1186e40e8b79f9928549d
parent688a4046ca03669f3d8463e987a759ff24674431 (diff)
downloadperlweeklychallenge-club-bdf32ca7c1e46ab34f485298a530da59047abe39.tar.gz
perlweeklychallenge-club-bdf32ca7c1e46ab34f485298a530da59047abe39.tar.bz2
perlweeklychallenge-club-bdf32ca7c1e46ab34f485298a530da59047abe39.zip
Updated ch#55-2 to handle non-unique list
-rw-r--r--challenge-055/yet-ebreo/perl/ch-2.pl35
1 files changed, 16 insertions, 19 deletions
diff --git a/challenge-055/yet-ebreo/perl/ch-2.pl b/challenge-055/yet-ebreo/perl/ch-2.pl
index 37257708ab..a9473ed815 100644
--- a/challenge-055/yet-ebreo/perl/ch-2.pl
+++ b/challenge-055/yet-ebreo/perl/ch-2.pl
@@ -4,26 +4,19 @@ use warnings;
use feature 'say';
-my @n = @ARGV ? @ARGV : (1, 2, 3, 4) ;
-
+my @n = sort {$a - $b} @ARGV ? @ARGV : (1, 2, 3, 4) ;
+my %h;
sub wave {
my ($a,$l,$r) = @_;
if ($l == $r) {
- if ($a->[1]<=$a->[0]) {
- #This filter assumes numbers in array @n are unique.
- #So, combinations like [2, 2, 1, 4, 3] will not be
- #generated/printed when given @n = [1, 2, 2, 3, 4]
- (!grep {
- (
- ($a->[$_] >= $a->[$_-1]) &&
- ($a->[$_-1] >= $a->[$_-2])
- ) or (
- ($a->[$_] <= $a->[$_-1]) &&
- ($a->[$_-1] <= $a->[$_-2])
- )
- } 2..$#{$a}) && say "@{$a}";
+ my $flag = 1;
+ for my $e (1..$#{$a}) {
+ !($flag &= $e % 2? $a->[$e]<=$a->[$e-1] : $a->[$e]>=$a->[$e-1]) && last;
}
+
+ #Only unique permutation will be printed
+ $flag && !$h{"@{$a}"}++ && say "@{$a}"
} else {
for my $i ($l..$r) {
($a->[$l], $a->[$i]) = ($a->[$i],$a->[$l]);
@@ -45,10 +38,14 @@ perl .\ch-2.pl
perl .\ch-2.pl 1 2 2 3 4
2 1 3 2 4
2 1 4 2 3
-2 1 3 2 4
-2 1 4 2 3
+2 2 3 1 4
+2 2 4 1 3
+3 2 2 1 4
3 2 4 1 2
-3 2 4 1 2
-4 2 3 1 2
+3 1 2 2 4
+3 1 4 2 2
+4 2 2 1 3
4 2 3 1 2
+4 1 2 2 3
+4 1 3 2 2
=cut