diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-12-27 12:56:47 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-27 12:56:47 +0000 |
| commit | 99757e07229af6c9e7e59a1f9571d00801ac415c (patch) | |
| tree | 5f934a478a2182900921a0942f09c0bf9c35a063 | |
| parent | 2a51e0eca645b88b6a14705a3f721c7512a9491e (diff) | |
| parent | 58d2e54e0381157072a8b30d60a6ea06e4f900ae (diff) | |
| download | perlweeklychallenge-club-99757e07229af6c9e7e59a1f9571d00801ac415c.tar.gz perlweeklychallenge-club-99757e07229af6c9e7e59a1f9571d00801ac415c.tar.bz2 perlweeklychallenge-club-99757e07229af6c9e7e59a1f9571d00801ac415c.zip | |
Merge pull request #7313 from zapwai/branch-for-challenge-197
Week 197
| -rwxr-xr-x | challenge-197/zapwai/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-197/zapwai/perl/ch-2.pl | 25 |
2 files changed, 53 insertions, 0 deletions
diff --git a/challenge-197/zapwai/perl/ch-1.pl b/challenge-197/zapwai/perl/ch-1.pl new file mode 100755 index 0000000000..fae666d7d0 --- /dev/null +++ b/challenge-197/zapwai/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +use v5.30.0; +my @list = (1, 0, 3, 0, 0, 5); +# my @list = (0, 1, 0, 2, 0); + +say "Input: (".join(",", @list).")"; +my $numzeros = 0; +foreach (@list) { + $numzeros++ if $_ == 0; +} +my $nonzero = @list - $numzeros - 1; # index of last nonzero entry +sub swap { + for (0 .. $#list - 1) { + if ($list[$_] == 0) { + $list[$_] = $list[$_ + 1]; + $list[$_ + 1] = 0; + } + } +} +my $flag; +do { + $flag=0; + swap(); + foreach (0 .. $#list) { + $flag++ if (($_ <= $nonzero) && ($list[$_] == 0)); + } +} while ($flag); +say "Output: (".join(",", @list).")"; diff --git a/challenge-197/zapwai/perl/ch-2.pl b/challenge-197/zapwai/perl/ch-2.pl new file mode 100755 index 0000000000..0b1dfb4313 --- /dev/null +++ b/challenge-197/zapwai/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl +use v5.30.0; +my $list1 = [1,5,1,1,6,4]; +my $list2 = [1,3,2,2,3,1]; +for my $ref ($list1, $list2) { + my @list = @$ref; + say "Input: \@list: (".join(",", @list).")"; + my $cnt; + do { + $cnt=0; + for (0 .. $#list) { + if ($_ % 2 == 0) { + wiggle(\$cnt, \@list) if ($list[$_ + 1] < $list[$_]); + } else { + wiggle(\$cnt, \@list) if ($list[$_ + 1] > $list[$_]); + } + } + } while ($cnt > 0); + say "Output: (".join(",", @list).")"; +} +sub wiggle { + my ($cnt, $list) = @_; + $$cnt++; + ($$list[$_],$$list[$_ + 1]) = ($$list[$_+1], $$list[$_]); +} |
