aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-12-27 13:02:54 +0000
committerGitHub <noreply@github.com>2022-12-27 13:02:54 +0000
commit92ecd89aa4e88ab95c7b38c8d09b6f6a29856d7e (patch)
treea875a6c34a233027c54efa1d84f27f11188e7d4e
parent574136ce7a06629883d75b8fd43460041835abf7 (diff)
parent31d74d24779de6f2b89ebddd78507887943cccdc (diff)
downloadperlweeklychallenge-club-92ecd89aa4e88ab95c7b38c8d09b6f6a29856d7e.tar.gz
perlweeklychallenge-club-92ecd89aa4e88ab95c7b38c8d09b6f6a29856d7e.tar.bz2
perlweeklychallenge-club-92ecd89aa4e88ab95c7b38c8d09b6f6a29856d7e.zip
Merge pull request #7318 from steve-g-lynn/branch-for-challenge-197
pwc 197
-rw-r--r--challenge-197/steve-g-lynn/blog.txt1
-rwxr-xr-xchallenge-197/steve-g-lynn/perl/ch-1.sh3
-rwxr-xr-xchallenge-197/steve-g-lynn/perl/ch-2.pl44
-rwxr-xr-xchallenge-197/steve-g-lynn/raku/ch-1.sh3
-rwxr-xr-xchallenge-197/steve-g-lynn/raku/ch-2.p638
5 files changed, 89 insertions, 0 deletions
diff --git a/challenge-197/steve-g-lynn/blog.txt b/challenge-197/steve-g-lynn/blog.txt
new file mode 100644
index 0000000000..3c668b0130
--- /dev/null
+++ b/challenge-197/steve-g-lynn/blog.txt
@@ -0,0 +1 @@
+https://thiujiac.blogspot.com/2022/12/pwc-197.html
diff --git a/challenge-197/steve-g-lynn/perl/ch-1.sh b/challenge-197/steve-g-lynn/perl/ch-1.sh
new file mode 100755
index 0000000000..a394867d39
--- /dev/null
+++ b/challenge-197/steve-g-lynn/perl/ch-1.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+perl -wl -e 'my @o = sort { ($a == 0) ? 1 : ( ($b == 0) ? -1 : 0 ) } @ARGV; print "@o"' $@
diff --git a/challenge-197/steve-g-lynn/perl/ch-2.pl b/challenge-197/steve-g-lynn/perl/ch-2.pl
new file mode 100755
index 0000000000..d52609f11e
--- /dev/null
+++ b/challenge-197/steve-g-lynn/perl/ch-2.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/env -S perl -wl
+
+use strict;
+use Data::Dumper qw(Dumper);
+
+print Dumper(&wiggle_sort(1,5,1,1,6,4)); #5,6,1,4,1,1
+print Dumper(&wiggle_sort(1,3,2,2,3,1)); #3,3,2,2,1,1
+print Dumper(&wiggle_sort(1,2,3,4,5)); #4,5,2,3,1
+print Dumper(&wiggle_sort(1,2,3,4)); #3,4,1,2
+
+#-- this is not exactly the same as the challenge
+#-- my interpretation is list[0] <= list[1] >= list[2] <= ..
+#-- i.e., weak instead of strict inequalities
+#-- otherwise, there may be no solutions
+#-- e.g., with (1,1,1,1,1)
+#
+#-- also my algorithm does not generate the test examples
+#-- though it generates answers that satisfy the (weaker)
+#-- condition.
+#
+#-- the logic is first sort in descending order
+#-- then exchange the 1st and 2nd items, 3rd and 4th items, ...
+
+
+sub wiggle_sort {
+ #-- subsub
+ local *wiggle = sub {
+ my ($n)=@_;
+ ($n+1, $n);
+ };
+
+ #-- wiggle_sort root sub
+ my @list = @_;
+ my @retval;
+
+ @list = sort {$b <=> $a} @list;
+
+ for (my $i = 0; ($i <= (scalar(@list)-2)); $i += 2) {
+ push @retval, @list[&wiggle($i)];
+ }
+ (@list % 2) && (push @retval, $list[-1]);
+ @retval;
+}
+
diff --git a/challenge-197/steve-g-lynn/raku/ch-1.sh b/challenge-197/steve-g-lynn/raku/ch-1.sh
new file mode 100755
index 0000000000..dc018593fa
--- /dev/null
+++ b/challenge-197/steve-g-lynn/raku/ch-1.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+raku -e 'say @*ARGS.sort( { ($^a == 0) ?? 1 !! ( ($^b == 0) ?? -1 !! 0 ) } )' $@
diff --git a/challenge-197/steve-g-lynn/raku/ch-2.p6 b/challenge-197/steve-g-lynn/raku/ch-2.p6
new file mode 100755
index 0000000000..c652582009
--- /dev/null
+++ b/challenge-197/steve-g-lynn/raku/ch-2.p6
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl6
+
+
+say &wiggle_sort((1,5,1,1,6,4)); #5,6,1,4,1,1
+say &wiggle_sort((1,3,2,2,3,1)); #3,3,2,2,1,1
+say &wiggle_sort((1,2,3,4,5)); #4,5,2,3,1
+say &wiggle_sort((1,2,3,4)); #3,4,1,2
+
+#-- this is not exactly the same as the challenge
+#-- my interpretation is list[0] <= list[1] >= list[2] <= ..
+#-- i.e., weak instead of strict inequalities
+#-- otherwise, there may be no solutions
+#-- e.g., with (1,1,1,1,1)
+#
+#-- also my algorithm does not generate the test examples
+#-- though it generates answers that satisfy the (weaker)
+#-- condition.
+#
+#-- the logic is first sort in descending order
+#-- then exchange the 1st and 2nd items, 3rd and 4th items, ...
+
+
+sub wiggle_sort(@list) {
+ my @retval;
+
+ my @list_ = @list.sort({$^b <=> $^a}).flat;
+
+ loop (my $i = 0; ($i <= (@list_.elems)-2); $i += 2) {
+ push @retval, (@list_[&wiggle($i)]);
+ }
+ (@list_ % 2) && (push @retval, @list_[*-1]);
+ @retval.List.flat;
+}
+
+#-- subsub
+sub wiggle (Int $n) {
+ $n+1, $n;
+}