diff options
| author | pme <hauptadler@gmail.com> | 2024-09-22 10:46:33 +0200 |
|---|---|---|
| committer | pme <hauptadler@gmail.com> | 2024-09-22 10:46:33 +0200 |
| commit | b0e0699dc83b905a9c11ff2103c16cd57c284646 (patch) | |
| tree | 4e71354dc381cd921b65f3f135e003a45be83524 /challenge-197 | |
| parent | 9691c964bfe50886d505401ee3db7ae6f3ef96a3 (diff) | |
| download | perlweeklychallenge-club-b0e0699dc83b905a9c11ff2103c16cd57c284646.tar.gz perlweeklychallenge-club-b0e0699dc83b905a9c11ff2103c16cd57c284646.tar.bz2 perlweeklychallenge-club-b0e0699dc83b905a9c11ff2103c16cd57c284646.zip | |
challenge-197
Diffstat (limited to 'challenge-197')
| -rwxr-xr-x | challenge-197/peter-meszaros/perl/ch-1.pl | 55 | ||||
| -rwxr-xr-x | challenge-197/peter-meszaros/perl/ch-2.pl | 57 |
2 files changed, 112 insertions, 0 deletions
diff --git a/challenge-197/peter-meszaros/perl/ch-1.pl b/challenge-197/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..4f10b1e158 --- /dev/null +++ b/challenge-197/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,55 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Move Zero + +Submitted by: Mohammad S Anwar + +You are given a list of integers, @list. + +Write a script to move all zero, if exists, to the end while maintaining the +relative order of non-zero elements. + +=head2 Example 1 + + Input: @list = (1, 0, 3, 0, 0, 5) + Output: (1, 3, 5, 0, 0, 0) + +=head2 Example 2 + + Input: @list = (1, 6, 4) + Output: (1, 6, 4) + +=head2 Example 3 + + Input: @list = (0, 1, 0, 2, 0) + Output: (1, 2, 0, 0, 0) + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[1, 0, 3, 0, 0, 5], [1, 3, 5, 0, 0, 0], 'Example 1'], + [[1, 6, 4], [1, 6, 4], 'Example 2'], + [[0, 1, 0, 2, 0], [1, 2, 0, 0, 0], 'Example 3'], +]; + +sub move_zero +{ + my $l = shift; + + my @res = grep {$_} @$l; + push @res, (0) x (@$l - @res); + + return \@res; +} + +for (@$cases) { + is(move_zero($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-197/peter-meszaros/perl/ch-2.pl b/challenge-197/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..4d1d66368b --- /dev/null +++ b/challenge-197/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Wiggle Sort + +Submitted by: Mohammad S Anwar + +You are given a list of integers, @list. + +Write a script to perform Wiggle Sort on the given list. + + Wiggle sort would be such as list[0] < list[1] > list[2] < list[3]... + +=head2 Example 1 + + Input: @list = (1,5,1,1,6,4) + Output: (1,6,1,5,1,4) + +=head2 Example 2 + + Input: @list = (1,3,2,2,3,1) + Output: (2,3,1,3,1,2) + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + #[[1, 5, 1, 1, 6, 4], [1, 6, 1, 5, 1, 4], 'Example 1'], + #[[1, 3, 2, 2, 3, 1], [2, 3, 1, 3, 1, 2], 'Example 2'], + [[1, 5, 1, 1, 6, 4], [ 1, 5, 1, 6, 1, 4 ], 'Example 1'], + [[1, 3, 2, 2, 3, 1], [ 1, 3, 2, 3, 1, 2 ], 'Example 2'], +]; + +sub wiggle_sort +{ + my $l = shift; + + for my $i (1 .. $#$l) { + my $odd_index = $i % 2; + if ( $odd_index && $l->[$i] < $l->[$i-1] or + !$odd_index && $l->[$i] > $l->[$i-1]) { + ($l->[$i], $l->[$i-1]) = ($l->[$i-1], $l->[$i]); + } + } + + return $l; +} + +for (@$cases) { + is(wiggle_sort($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; |
