aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Köhler <jean-luc@picard.franken.de>2022-12-26 13:54:50 +0100
committerThomas Köhler <jean-luc@picard.franken.de>2022-12-26 13:54:50 +0100
commit0a8e87c380f1dddca4a8105abdacd7d339b39718 (patch)
treeabe37ed35adfa6d757a46d2208a3daa12f1da758
parent63fb76188e132564e50feefd2d9d5b8491568948 (diff)
downloadperlweeklychallenge-club-0a8e87c380f1dddca4a8105abdacd7d339b39718.tar.gz
perlweeklychallenge-club-0a8e87c380f1dddca4a8105abdacd7d339b39718.tar.bz2
perlweeklychallenge-club-0a8e87c380f1dddca4a8105abdacd7d339b39718.zip
Add solution 197 by Thomas Köhler
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
-rw-r--r--challenge-197/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-197/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-197/jeanluc2020/perl/ch-1.pl39
-rwxr-xr-xchallenge-197/jeanluc2020/perl/ch-2.pl44
4 files changed, 85 insertions, 0 deletions
diff --git a/challenge-197/jeanluc2020/blog-1.txt b/challenge-197/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..07829acb5a
--- /dev/null
+++ b/challenge-197/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-197-1.html
diff --git a/challenge-197/jeanluc2020/blog-2.txt b/challenge-197/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..0f3cc2503b
--- /dev/null
+++ b/challenge-197/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-197-2.html
diff --git a/challenge-197/jeanluc2020/perl/ch-1.pl b/challenge-197/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..8197b5dd64
--- /dev/null
+++ b/challenge-197/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-197/#TASK1
+#
+# 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.
+
+use strict;
+use warnings;
+
+# sample input values
+my $inputs = [
+ [1, 0, 3, 0, 0, 5],
+ [1, 6, 4],
+ [0, 1, 0, 2, 0]
+];
+
+# handle all input arrays from sample list above
+foreach my $input (@$inputs) {
+ print "(" . join(", ", @$input) . ") returns (" . join(", ", move_zero(@$input)) . ")\n";
+}
+
+# given a list of integers, return the same list with all zeros moved to the end
+sub move_zero {
+ my @values = @_;
+ my @return = ();
+ my @tmp = ();
+ # collect all non-zero values into @return, all zero values into @tmp
+ foreach my $elem (@values) {
+ if($elem == 0) {
+ push @tmp, $elem;
+ } else {
+ push @return, $elem;
+ }
+ }
+ # add all zero values to the end of @return before returning
+ push @return, @tmp;
+ return @return;
+}
diff --git a/challenge-197/jeanluc2020/perl/ch-2.pl b/challenge-197/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..b5e0634d5f
--- /dev/null
+++ b/challenge-197/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-197/#TASK2
+#
+# 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]...
+
+use strict;
+use warnings;
+
+# sample input values
+my $inputs = [
+ [1,5,1,1,6,4],
+ [1,3,2,2,3,1],
+ [1,2,3,4,5],
+ [1,1,1,2,2,2,3,3,4,4],
+];
+
+# handle all input arrays from sample list above
+foreach my $input (@$inputs) {
+ print "(" . join(", ", @$input) . ") returns (" . join(", ", wiggle_sort(@$input)) . ")\n";
+}
+
+# wiggle sort has to jump up and down every time
+sub wiggle_sort {
+ # let's start by sorting all values
+ my @values = sort {$a <=> $b} @_;
+ my @result = ();
+ # by starting in the middle of the sorted array and walking down while at
+ # the same time starting at the top and wakling down as well, we can always
+ # make sure we have a bigger and a smaller value next to each other
+ my $start = int($#values/2);
+ my ($i, $j) = ($start, $#values);
+ while($i >= 0 && $j > $start) {
+ push @result, $values[$i--], $values[$j--];
+ }
+ # if we have an odd number of values in the original array, we now have
+ # one more value to take care of; in this case $i just reached 0 by means of
+ # the last "$i--" in the loop
+ push @result, $values[$i] if $i == 0;
+ return @result;
+}