diff options
| -rw-r--r-- | challenge-197/steve-g-lynn/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-197/steve-g-lynn/perl/ch-1.sh | 3 | ||||
| -rwxr-xr-x | challenge-197/steve-g-lynn/perl/ch-2.pl | 44 | ||||
| -rwxr-xr-x | challenge-197/steve-g-lynn/raku/ch-1.sh | 3 | ||||
| -rwxr-xr-x | challenge-197/steve-g-lynn/raku/ch-2.p6 | 38 |
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; +} |
