diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-21 22:30:27 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-21 22:30:27 +0000 |
| commit | da3731bfb028253180da6118717957b1d1226c44 (patch) | |
| tree | da1412da28b68fe6c180cbbb44948b8abe4b3087 | |
| parent | 24768e7e03a6650db1d81f58ec120b806807fc0f (diff) | |
| download | perlweeklychallenge-club-da3731bfb028253180da6118717957b1d1226c44.tar.gz perlweeklychallenge-club-da3731bfb028253180da6118717957b1d1226c44.tar.bz2 perlweeklychallenge-club-da3731bfb028253180da6118717957b1d1226c44.zip | |
Add Perl solution
| -rw-r--r-- | challenge-197/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-197/paulo-custodio/perl/ch-1.pl | 25 | ||||
| -rw-r--r-- | challenge-197/paulo-custodio/perl/ch-2.pl | 46 | ||||
| -rw-r--r-- | challenge-197/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-197/paulo-custodio/t/test-2.yaml | 20 |
5 files changed, 108 insertions, 0 deletions
diff --git a/challenge-197/paulo-custodio/Makefile b/challenge-197/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-197/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-197/paulo-custodio/perl/ch-1.pl b/challenge-197/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..5c9d599734 --- /dev/null +++ b/challenge-197/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl + +# Challenge 197 +# +# 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. +# +# +# Example 1 +# Input: @list = (1, 0, 3, 0, 0, 5) +# Output: (1, 3, 5, 0, 0, 0) +# Example 2 +# Input: @list = (1, 6, 4) +# Output: (1, 6, 4) +# Example 3 +# Input: @list = (0, 1, 0, 2, 0) +# Output: (1, 2, 0, 0, 0) + +use Modern::Perl; + +say join " ", (grep {$_} @ARGV), (grep {!$_} @ARGV); diff --git a/challenge-197/paulo-custodio/perl/ch-2.pl b/challenge-197/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..85d5da58e0 --- /dev/null +++ b/challenge-197/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/usr/bin/perl + +# Challenge 197 +# +# 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]…. +# +# +# Example 1 +# Input: @list = (1,5,1,1,6,4) +# Output: (1,6,1,5,1,4) +# Example 2 +# Input: @list = (1,3,2,2,3,1) +# Output: (2,3,1,3,1,2) + +use Modern::Perl; + +sub copy_data { + my($to, $to_idx, $from, $from_idx) = @_; + for (; $to_idx >= 0; $to_idx-=2) { + $to->[$to_idx]=$from->[$from_idx++]; + } +} + +sub wiggle_sort { + my(@nums) = @_; + my @copy = sort {$a<=>$b} @nums; + if (scalar(@nums)%2==0) { + copy_data(\@nums, scalar(@nums)-2, \@copy, 0); + copy_data(\@nums, scalar(@nums)-1, \@copy, int(scalar(@nums)/2)); + } + else { + copy_data(\@nums, scalar(@nums)-2, \@copy, 0); + copy_data(\@nums, scalar(@nums)-1, \@copy, int(scalar(@nums)/2)); + } + return @nums; +} + +my @nums = wiggle_sort(@ARGV); +say "@nums"; diff --git a/challenge-197/paulo-custodio/t/test-1.yaml b/challenge-197/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..fd2a3fd918 --- /dev/null +++ b/challenge-197/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 0 3 0 0 5 + input: + output: 1 3 5 0 0 0 +- setup: + cleanup: + args: 1 6 4 + input: + output: 1 6 4 +- setup: + cleanup: + args: 0 1 0 2 0 + input: + output: 1 2 0 0 0 diff --git a/challenge-197/paulo-custodio/t/test-2.yaml b/challenge-197/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..1d7221da1d --- /dev/null +++ b/challenge-197/paulo-custodio/t/test-2.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: 1 5 1 1 6 4 + input: + output: 1 6 1 5 1 4 +- setup: + cleanup: + args: 1 3 2 2 3 1 + input: + output: 2 3 1 3 1 2 +- setup: + cleanup: + args: 1 3 2 2 3 1 + input: + output: 2 3 1 3 1 2 +- setup: + cleanup: + args: 5 1 6 4 1 + input: + output: 6 1 5 1 4 |
