aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-12-27 14:07:50 +0000
committerGitHub <noreply@github.com>2022-12-27 14:07:50 +0000
commit4fb56957ab474ce299e123a605af0acf4365cc30 (patch)
tree1f5543048782f1e6ffb63f604a32cb7ec2ed81d8
parent99783e7acaa3e8d3ac1a8564c69fac08ba45b080 (diff)
parentafc8e32c815e5bbfdfb207392a8cc143eb7bb1ff (diff)
downloadperlweeklychallenge-club-4fb56957ab474ce299e123a605af0acf4365cc30.tar.gz
perlweeklychallenge-club-4fb56957ab474ce299e123a605af0acf4365cc30.tar.bz2
perlweeklychallenge-club-4fb56957ab474ce299e123a605af0acf4365cc30.zip
Merge pull request #7319 from boblied/master
Week 197 bob-lied
-rw-r--r--challenge-197/bob-lied/README4
-rw-r--r--challenge-197/bob-lied/perl/ch-1.pl48
-rw-r--r--challenge-197/bob-lied/perl/ch-2.pl59
3 files changed, 109 insertions, 2 deletions
diff --git a/challenge-197/bob-lied/README b/challenge-197/bob-lied/README
index 1eebe9fbd4..270b68a64d 100644
--- a/challenge-197/bob-lied/README
+++ b/challenge-197/bob-lied/README
@@ -1,3 +1,3 @@
-Solutions to weekly challenge 196 by Bob Lied
+Solutions to weekly challenge 197 by Bob Lied
-https://perlweeklychallenge.org/blog/perl-weekly-challenge-196/
+https://perlweeklychallenge.org/blog/perl-weekly-challenge-197/
diff --git a/challenge-197/bob-lied/perl/ch-1.pl b/challenge-197/bob-lied/perl/ch-1.pl
new file mode 100644
index 0000000000..18153180a6
--- /dev/null
+++ b/challenge-197/bob-lied/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# ch-1.pl
+#=============================================================================
+# Copyright (c) 2022, Bob Lied
+#=============================================================================
+# Perl Weekly Challenge Week 197, Task 1 Move Zero
+#
+# 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 v5.36;
+
+use Getopt::Long;
+my $Verbose = 0;
+my $DoTest = 0;
+
+GetOptions("test" => \$DoTest, "verbose" => \$Verbose);
+exit(!runTest()) if $DoTest;
+
+sub moveZero(@list)
+{
+ my @noZero = grep { $_ != 0 } @list;
+ return [ @noZero, (0) x ( scalar(@list)-scalar(@noZero) ) ];
+}
+
+sub runTest
+{
+ use Test::More;
+
+ is_deeply( moveZero(1,0,3,0,0,5), [1,3,5,0,0,0], "Example 1");
+ is_deeply( moveZero(1,6,4 ), [1,6,4 ], "Example 2");
+ is_deeply( moveZero(0,1,0,2,0 ), [1,2,0,0,0 ], "Example 3");
+ is_deeply( moveZero(0 ), [0 ], "Example 3");
+
+ done_testing;
+}
+
diff --git a/challenge-197/bob-lied/perl/ch-2.pl b/challenge-197/bob-lied/perl/ch-2.pl
new file mode 100644
index 0000000000..9660429516
--- /dev/null
+++ b/challenge-197/bob-lied/perl/ch-2.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# ch-2.pl Perl Weekly Challenge Week 197 Task 2 Wiggle Sort
+#=============================================================================
+# Copyright (c) 2022, Bob Lied
+#=============================================================================
+# 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 v5.36;
+
+use Getopt::Long;
+my $Verbose = 0;
+my $DoTest = 0;
+
+GetOptions("test" => \$DoTest, "verbose" => \$Verbose);
+exit(!runTest()) if $DoTest;
+
+sub wiggleSort(@list)
+{
+ my @sl = sort { $a <=> $b } @list;
+
+ # Subtract one for 0-based arrays
+ my $midpoint = int( scalar(@sl) / 2 ) - 1;
+ my @bottom = reverse @sl[0..$midpoint];
+ my @top = reverse splice(@sl, $midpoint+1);
+
+ my @result;
+ while ( @bottom && @top )
+ {
+ push @result, (shift @bottom), (shift @top);
+ }
+ push @result, @top if @top; # Odd-sized lists
+ return \@result;
+}
+
+sub runTest
+{
+ use Test::More;
+
+ is_deeply( wiggleSort(1,5,1,1,6,4), [1,6,1,5,1,4], "Example 1");
+ is_deeply( wiggleSort(1,3,2,2,3,1), [2,3,1,3,1,2], "Example 2");
+ is_deeply( wiggleSort(1,3,2,2,3,1,4), [2,4,1,3,1,3,2], "Odd");
+
+ done_testing;
+}
+