aboutsummaryrefslogtreecommitdiff
path: root/challenge-197
diff options
context:
space:
mode:
authorRoger Bell_West <roger@firedrake.org>2022-12-26 09:34:52 +0000
committerRoger Bell_West <roger@firedrake.org>2022-12-26 09:34:52 +0000
commit778efedbf61fb496100a930f68ad320cc7da0e33 (patch)
treeee6e045992422b99333e9c36e3ce1ab0c1d0fff6 /challenge-197
parent63fb76188e132564e50feefd2d9d5b8491568948 (diff)
downloadperlweeklychallenge-club-778efedbf61fb496100a930f68ad320cc7da0e33.tar.gz
perlweeklychallenge-club-778efedbf61fb496100a930f68ad320cc7da0e33.tar.bz2
perlweeklychallenge-club-778efedbf61fb496100a930f68ad320cc7da0e33.zip
Solutions for challenge #197
Diffstat (limited to 'challenge-197')
-rwxr-xr-xchallenge-197/roger-bell-west/javascript/ch-1.js57
-rwxr-xr-xchallenge-197/roger-bell-west/javascript/ch-2.js91
-rw-r--r--challenge-197/roger-bell-west/kotlin/ch-1.kt28
-rw-r--r--challenge-197/roger-bell-west/kotlin/ch-2.kt90
-rwxr-xr-xchallenge-197/roger-bell-west/lua/ch-1.lua55
-rwxr-xr-xchallenge-197/roger-bell-west/lua/ch-2.lua103
-rwxr-xr-xchallenge-197/roger-bell-west/perl/ch-1.pl17
-rwxr-xr-xchallenge-197/roger-bell-west/perl/ch-2.pl54
-rw-r--r--challenge-197/roger-bell-west/postscript/ch-1.ps123
-rw-r--r--challenge-197/roger-bell-west/postscript/ch-2.ps170
-rwxr-xr-xchallenge-197/roger-bell-west/python/ch-1.py21
-rwxr-xr-xchallenge-197/roger-bell-west/python/ch-2.py61
-rwxr-xr-xchallenge-197/roger-bell-west/raku/ch-1.p615
-rwxr-xr-xchallenge-197/roger-bell-west/raku/ch-2.p652
-rwxr-xr-xchallenge-197/roger-bell-west/ruby/ch-1.rb23
-rwxr-xr-xchallenge-197/roger-bell-west/ruby/ch-2.rb76
-rwxr-xr-xchallenge-197/roger-bell-west/rust/ch-1.rs24
-rwxr-xr-xchallenge-197/roger-bell-west/rust/ch-2.rs80
18 files changed, 1140 insertions, 0 deletions
diff --git a/challenge-197/roger-bell-west/javascript/ch-1.js b/challenge-197/roger-bell-west/javascript/ch-1.js
new file mode 100755
index 0000000000..7abb2f5e95
--- /dev/null
+++ b/challenge-197/roger-bell-west/javascript/ch-1.js
@@ -0,0 +1,57 @@
+#! /usr/bin/node
+
+"use strict"
+
+// by Frank Tan
+// https://stackoverflow.com/questions/38400594/javascript-deep-comparison
+function deepEqual(a,b)
+{
+ if( (typeof a == 'object' && a != null) &&
+ (typeof b == 'object' && b != null) )
+ {
+ var count = [0,0];
+ for( var key in a) count[0]++;
+ for( var key in b) count[1]++;
+ if( count[0]-count[1] != 0) {return false;}
+ for( var key in a)
+ {
+ if(!(key in b) || !deepEqual(a[key],b[key])) {return false;}
+ }
+ for( var key in b)
+ {
+ if(!(key in a) || !deepEqual(b[key],a[key])) {return false;}
+ }
+ return true;
+ }
+ else
+ {
+ return a === b;
+ }
+}
+
+function movezero(l) {
+ let o = l.filter(i => i != 0);
+ o.push(...Array(l.length - o.length).fill(0));
+ return o;
+}
+
+if (deepEqual(movezero([1, 0, 3, 0, 0, 5]), [1, 3, 5, 0, 0, 0])) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+
+if (deepEqual(movezero([1, 6, 4]), [1, 6, 4])) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+
+if (deepEqual(movezero([0, 1, 0, 2, 0]), [1, 2, 0, 0, 0])) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write("\n");
diff --git a/challenge-197/roger-bell-west/javascript/ch-2.js b/challenge-197/roger-bell-west/javascript/ch-2.js
new file mode 100755
index 0000000000..d26c769054
--- /dev/null
+++ b/challenge-197/roger-bell-west/javascript/ch-2.js
@@ -0,0 +1,91 @@
+#! /usr/bin/node
+
+"use strict"
+
+function is_wigglesorted(l) {
+ for (let i = 0; i < l.length-1; i++) {
+ if (i % 2 == 0) {
+ if (l[i] >= l[i+1]) {
+ return false;
+ }
+ } else {
+ if (l[i] <= l[i+1]) {
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+function wigglesort(l) {
+ let s = l;
+ s.sort();
+ let le = s.length;
+ let p = Math.floor(le / 2);
+ let a = s.slice(0, p - 1);
+ let b = s.slice(p, -1);
+ let i = 0;
+ let o = [];
+ if (le % 2 == 1) {
+ o.push(s[p]);
+ b = s.slice(p + 1, -1);
+ i = 1;
+ }
+ for (let j = i; j < s.length; j++) {
+ if (j % 2 == 0) {
+ o.push(a.pop());
+ } else {
+ o.push(b.pop());
+ }
+ }
+ return o;
+}
+
+if (!is_wigglesorted([1,5,1,1,6,4])) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (is_wigglesorted([1,6,1,5,1,4])) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (!is_wigglesorted([1,3,2,2,3,1])) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (is_wigglesorted([2,3,1,3,1,2])) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (!is_wigglesorted([1,3,2,2,3,1])) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (is_wigglesorted(wigglesort([1,5,1,1,6,4]))) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (is_wigglesorted(wigglesort([1,3,2,2,3,1]))) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (is_wigglesorted(wigglesort([1,3,2,2,2,3,1]))) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write("\n");
diff --git a/challenge-197/roger-bell-west/kotlin/ch-1.kt b/challenge-197/roger-bell-west/kotlin/ch-1.kt
new file mode 100644
index 0000000000..316a4afb49
--- /dev/null
+++ b/challenge-197/roger-bell-west/kotlin/ch-1.kt
@@ -0,0 +1,28 @@
+fun movezero(l: List<Int>): List<Int> {
+ var o = ArrayList(l.filter { it != 0 })
+ o.addAll(Array(l.size - o.size) {_ -> 0})
+ return o.toList()
+}
+
+fun main() {
+ if (movezero(listOf(1, 0, 3, 0, 0, 5)) == listOf(1, 3, 5, 0, 0, 0)) {
+ print("Pass")
+ } else {
+ print("FAIL")
+ }
+ print(" ")
+
+ if (movezero(listOf(1, 6, 4)) == listOf(1, 6, 4)) {
+ print("Pass")
+ } else {
+ print("FAIL")
+ }
+ print(" ")
+
+ if (movezero(listOf(0, 1, 0, 2, 0)) == listOf(1, 2, 0, 0, 0)) {
+ print("Pass")
+ } else {
+ print("FAIL")
+ }
+ println("")
+}
diff --git a/challenge-197/roger-bell-west/kotlin/ch-2.kt b/challenge-197/roger-bell-west/kotlin/ch-2.kt
new file mode 100644
index 0000000000..9d548c06a2
--- /dev/null
+++ b/challenge-197/roger-bell-west/kotlin/ch-2.kt
@@ -0,0 +1,90 @@
+fun is_wigglesorted(l: List<Int>): Boolean {
+ for (i in 0..l.size-2) {
+ if (i % 2 == 0) {
+ if (l[i] >= l[i+1]) {
+ return false
+ }
+ } else {
+ if (l[i] <= l[i+1]) {
+ return false
+ }
+ }
+ }
+ return true
+}
+
+fun wigglesort(l: List<Int>): List<Int> {
+ var s = ArrayList(l)
+ s.sort()
+ val le = s.size
+ val p = le / 2
+ var a = ArrayList(s.slice(0 .. p - 1))
+ var b = ArrayList(s.slice(p .. le - 1))
+ var i = 0
+ var o = ArrayList<Int>()
+ if (le % 2 == 1) {
+ o.add(s[p])
+ b = ArrayList(s.slice(p + 1 .. le - 1))
+ i = 1
+ }
+ for (j in i .. le - 1) {
+ if (j % 2 == 0) {
+ o.add(a.removeLast())
+ } else {
+ o.add(b.removeLast())
+ }
+ }
+ return o.toList()
+}
+
+fun main() {
+ if (!is_wigglesorted(listOf(1,5,1,1,6,4))) {
+ print("Pass")
+ } else {
+ print("FAIL")
+ }
+ print(" ")
+ if (is_wigglesorted(listOf(1,6,1,5,1,4))) {
+ print("Pass")
+ } else {
+ print("FAIL")
+ }
+ print(" ")
+ if (!is_wigglesorted(listOf(1,3,2,2,3,1))) {
+ print("Pass")
+ } else {
+ print("FAIL")
+ }
+ print(" ")
+ if (is_wigglesorted(listOf(2,3,1,3,1,))) {
+ print("Pass")
+ } else {
+ print("FAIL")
+ }
+ print(" ")
+ if (!is_wigglesorted(listOf(1,3,2,2,3,1))) {
+ print("Pass")
+ } else {
+ print("FAIL")
+ }
+ print(" ")
+ if (is_wigglesorted(wigglesort(listOf(1,5,1,1,6,4)))) {
+ print("Pass")
+ } else {
+ print("FAIL")
+ }
+ print(" ")
+ if (is_wigglesorted(wigglesort(listOf(1,3,2,2,3,1)))) {
+ print("Pass")
+ } else {
+ print("FAIL")
+ }
+ print(" ")
+ if (is_wigglesorted(wigglesort(listOf(1,3,2,2,2,3,1)))) {
+ print("Pass")
+ } else {
+ print("FAIL")
+ }
+ print(" ")
+ println("")
+}
diff --git a/challenge-197/roger-bell-west/lua/ch-1.lua b/challenge-197/roger-bell-west/lua/ch-1.lua
new file mode 100755
index 0000000000..5757c8ea71
--- /dev/null
+++ b/challenge-197/roger-bell-west/lua/ch-1.lua
@@ -0,0 +1,55 @@
+#! /usr/bin/lua
+
+-- by Michael Anderson at
+-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua
+function recursive_compare(t1,t2)
+ if t1==t2 then return true end
+ if (type(t1)~="table") then return false end
+ local mt1 = getmetatable(t1)
+ local mt2 = getmetatable(t2)
+ if( not recursive_compare(mt1,mt2) ) then return false end
+ for k1,v1 in pairs(t1) do
+ local v2 = t2[k1]
+ if( not recursive_compare(v1,v2) ) then return false end
+ end
+ for k2,v2 in pairs(t2) do
+ local v1 = t1[k2]
+ if( not recursive_compare(v1,v2) ) then return false end
+ end
+ return true
+end
+
+function movezero(l)
+ local o = {}
+ local z = 0
+ for i, v in ipairs(l) do
+ if v == 0 then
+ z = z + 1
+ else
+ table.insert(o, v)
+ end
+ end
+ for i = 1, z do
+ table.insert(o, 0)
+ end
+ return o
+end
+
+if recursive_compare(movezero({1, 0, 3, 0, 0, 5}), {1, 3, 5, 0, 0, 0}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+if recursive_compare(movezero({1, 6, 4}), {1, 6, 4}) then
+io.write(" ")
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+if recursive_compare(movezero({0, 1, 0, 2, 0}), {1, 2, 0, 0, 0}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")
diff --git a/challenge-197/roger-bell-west/lua/ch-2.lua b/challenge-197/roger-bell-west/lua/ch-2.lua
new file mode 100755
index 0000000000..6ffc9648b7
--- /dev/null
+++ b/challenge-197/roger-bell-west/lua/ch-2.lua
@@ -0,0 +1,103 @@
+#! /usr/bin/lua
+
+function is_wigglesorted(l)
+ for i = 1, #l-1 do
+ if i % 2 == 1 then
+ if l[i] >= l[i+1] then
+ return false
+ end
+ else
+ if l[i] <= l[i+1] then
+ return false
+ end
+ end
+ end
+ return true
+end
+
+function wigglesort(l)
+ local s = l
+ table.sort(s)
+ local le = #s
+ local p = math.floor(le / 2)
+ local a = {}
+ local b = {}
+ for ii, v in ipairs(s) do
+ if ii < p then
+ table.insert(a, v)
+ else
+ table.insert(b, v)
+ end
+ end
+ local i = 1
+ local o = {}
+ if le % 2 == 1 then
+ table.insert(o, table.remove(b, 1))
+ i = 2
+ end
+ for j = i, #s do
+ if j % 2 == 1 then
+ table.insert(o, table.remove(a))
+ else
+ table.insert(o, table.remove(b))
+ end
+ end
+ return o
+end
+
+
+if not is_wigglesorted({1,5,1,1,6,4}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if is_wigglesorted({1,6,1,5,1,4}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if not is_wigglesorted({1,3,2,2,3,1}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if is_wigglesorted({2,3,1,3,1,2}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if not is_wigglesorted({1,3,2,2,3,1}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if is_wigglesorted(wigglesort({1,5,1,1,6,4})) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if is_wigglesorted(wigglesort({1,3,2,2,3,1})) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if is_wigglesorted(wigglesort({1,3,2,2,2,3,1})) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")
diff --git a/challenge-197/roger-bell-west/perl/ch-1.pl b/challenge-197/roger-bell-west/perl/ch-1.pl
new file mode 100755
index 0000000000..a2bab71fea
--- /dev/null
+++ b/challenge-197/roger-bell-west/perl/ch-1.pl
@@ -0,0 +1,17 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+use experimental 'signatures';
+
+use Test::More tests => 3;
+
+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');
+
+sub movezero($l) {
+ my @o = grep {$_ != 0} @{$l};
+ push @o,(0) x (scalar (@{$l}) - scalar (@o));
+ return \@o;
+}
diff --git a/challenge-197/roger-bell-west/perl/ch-2.pl b/challenge-197/roger-bell-west/perl/ch-2.pl
new file mode 100755
index 0000000000..580cc93310
--- /dev/null
+++ b/challenge-197/roger-bell-west/perl/ch-2.pl
@@ -0,0 +1,54 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+use experimental 'signatures';
+
+use Test::More tests => 8;
+
+is(is_wigglesorted([1,5,1,1,6,4]), 0, 'example 1');
+is(is_wigglesorted([1,6,1,5,1,4]), 1, 'example 2');
+is(is_wigglesorted([1,3,2,2,3,1]), 0, 'example 3');
+is(is_wigglesorted([2,3,1,3,1,2]), 1, 'example 4');
+is(is_wigglesorted([1,3,2,2,3,1]), 0, 'example 5');
+is(is_wigglesorted(wigglesort([1,5,1,1,6,4])), 1, 'example 6');
+is(is_wigglesorted(wigglesort([1,3,2,2,3,1])), 1, 'example 7');
+is(is_wigglesorted(wigglesort([1,3,2,2,2,3,1])), 1, 'example 8');
+
+sub is_wigglesorted($l) {
+ foreach my $i (0..scalar @{$l}-2) {
+ if ($i % 2 == 0) {
+ if ($l->[$i] >= $l->[$i+1]) {
+ return 0;
+ }
+ } else {
+ if ($l->[$i] <= $l->[$i+1]) {
+ return 0;
+ }
+ }
+ }
+ return 1;
+}
+
+sub wigglesort($l) {
+ my @s = sort @{$l};
+ my $le = scalar @s;
+ my $p = int($le / 2);
+ my @a = @s[0 .. $p - 1];
+ my @b = @s[$p .. $#s];
+ my $i = 0;
+ my @o;
+ if ($le % 2 == 1) {
+ push @o,$s[$p];
+ @b = @s[$p + 1 .. $#s];
+ $i = 1;
+ }
+ foreach my $j ($i .. $#s) {
+ if ($j % 2 == 0) {
+ push @o, pop @a;
+ } else {
+ push @o, pop @b;
+ }
+ }
+ return \@o;
+}
diff --git a/challenge-197/roger-bell-west/postscript/ch-1.ps b/challenge-197/roger-bell-west/postscript/ch-1.ps
new file mode 100644
index 0000000000..cfc320081a
--- /dev/null
+++ b/challenge-197/roger-bell-west/postscript/ch-1.ps
@@ -0,0 +1,123 @@
+%!PS
+
+% begin included library code
+% see https://github.com/Firedrake/postscript-libraries/
+/test.end {
+ ( ) print
+ test.count 0 gt {
+ (Passed ) print
+ test.pass (...) cvs print
+ (/) print
+ test.count (...) cvs print
+ ( \() print
+ test.pass 100 mul test.count idiv (...) cvs print
+ (%\)) print
+ (\r\n) print
+ } if
+} bind def
+
+/filter { % array proc(bool) -> array
+ 1 dict begin
+ /p exch def
+ [ exch
+ {
+ dup p not
+ {
+ pop
+ } if
+ } forall
+ ]
+ end
+} bind def
+
+/deepeq {
+ 2 dict begin
+ /a exch def
+ /b exch def
+ a type b type eq {
+ a type /dicttype eq {
+ a length b length eq {
+ <<
+ a {
+ pop
+ true
+ } forall
+ b {
+ pop
+ true
+ } forall
+ >>
+ true exch
+ {
+ pop
+ dup a exch known {
+ dup b exch known {
+ dup a exch get exch b exch get deepeq not {
+ pop false
+ } if
+ } {
+ false
+ } ifelse
+ } {
+ false
+ } ifelse
+ } forall
+ } {
+ false
+ } ifelse
+ } {
+ a type dup /arraytype eq exch /stringtype eq or {
+ a length b length eq {
+ true
+ 0 1 a length 1 sub {
+ dup a exch get exch b exch get deepeq not {
+ pop false
+ exit
+ } if
+ } for
+ } {
+ false
+ } ifelse
+ } {
+ a b eq
+ } ifelse
+ } ifelse
+ } {
+ false
+ } ifelse
+ end
+} bind def
+
+/test.start {
+ print (:) print
+ /test.pass 0 def
+ /test.count 0 def
+} bind def
+
+/test {
+ /test.count test.count 1 add def
+ {
+ /test.pass test.pass 1 add def
+ } {
+ ( ) print
+ test.count (....) cvs print
+ (-fail) print
+ } ifelse
+} bind def
+
+
+% end included library code
+
+/movezero {
+ 1 dict begin
+ /i exch def
+ [ i length { 0 } repeat ]
+ dup 0 i { 0 ne } filter putinterval
+ end
+} bind def
+
+(movezero) test.start
+[ 1 0 3 0 0 5 ] movezero [ 1 3 5 0 0 0 ] deepeq test
+[ 1 6 4 ] movezero [ 1 6 4 ] deepeq test
+[ 0 1 0 2 0 ] movezero [ 1 2 0 0 0 ] deepeq test
+test.end
diff --git a/challenge-197/roger-bell-west/postscript/ch-2.ps b/challenge-197/roger-bell-west/postscript/ch-2.ps
new file mode 100644
index 0000000000..e2ab190793
--- /dev/null
+++ b/challenge-197/roger-bell-west/postscript/ch-2.ps
@@ -0,0 +1,170 @@
+%!PS
+
+% begin included library code
+% see https://github.com/Firedrake/postscript-libraries/
+/apop.right { % [a b c] -> [a b] c
+ [ exch aload length 1 add 1 roll ] exch
+} bind def
+
+/quicksort.swap {
+ 2 dict begin
+ /bi exch def
+ /ai exch def
+ arr ai get
+ arr bi get
+ arr exch ai exch put
+ arr exch bi exch put
+ end
+} bind def
+
+/quicksort { % [ a c b ] -> [ a b c ]
+ 1 dict begin
+ /arr exch def
+ arr length 0 gt {
+ 0 arr length 1 sub quicksort.main
+ } if
+ arr
+ end
+} bind def
+
+/test {
+ /test.count test.count 1 add def
+ {
+ /test.pass test.pass 1 add def
+ } {
+ ( ) print
+ test.count (....) cvs print
+ (-fail) print
+ } ifelse
+} bind def
+
+/quicksort.partition {
+ 3 dict begin
+ /pivot arr hi lo add 2 idiv get def
+ /i lo 1 sub def
+ /j hi 1 add def
+ {
+ {
+ /i i 1 add def
+ arr i get pivot ge {
+ exit
+ } if
+ } loop
+ {
+ /j j 1 sub def
+ arr j get pivot le {
+ exit
+ } if
+ } loop
+ i j ge {
+ j
+ exit
+ } if
+ i j quicksort.swap
+ } loop
+ end
+} bind def
+
+/test.start {
+ print (:) print
+ /test.pass 0 def
+ /test.count 0 def
+} bind def
+
+/test.end {
+ ( ) print
+ test.count 0 gt {
+ (Passed ) print
+ test.pass (...) cvs print
+ (/) print
+ test.count (...) cvs print
+ ( \() print
+ test.pass 100 mul test.count idiv (...) cvs print
+ (%\)) print
+ (\r\n) print
+ } if
+} bind def
+
+/apop.left { % [a b c] -> [b c] a
+ dup 0 get exch
+ [ exch aload length -1 roll pop ] exch
+} bind def
+
+/quicksort.main { % lo hi -> (null)
+ 3 dict begin
+ /hi exch def
+ /lo exch def
+ /xit false def
+ lo 0 lt {
+ /xit true def
+ } if
+ hi 0 lt {
+ /xit true def
+ } if
+ lo hi ge {
+ /xit true def
+ } if
+ xit not {
+ /p quicksort.partition def
+ lo p quicksort.main
+ p 1 add hi quicksort.main
+ } if
+ end
+} bind def
+
+
+% end included library code
+
+/is_wigglesorted {
+ 2 dict begin
+ /l exch def
+ true
+ 0 1 l length 2 sub {
+ /i exch def
+ i 2 mod 0 eq {
+ l i get l i 1 add get ge {
+ pop false exit
+ } if
+ } {
+ l i get l i 1 add get le {
+ pop false exit
+ } if
+ } ifelse
+ } for
+ end
+} bind def
+
+/wigglesort {
+ 6 dict begin
+ /s exch quicksort def
+ /lg s length def
+ /p lg 2 idiv def
+ /a [ 0 1 p 1 sub { s exch get } for ] def
+ /b [ p 1 lg 1 sub { s exch get } for ] def
+ /i 0 def
+ [
+ lg 2 mod 1 eq {
+ b apop.left exch /b exch def
+ /i 1 def
+ } if
+ i 1 lg 1 sub {
+ 2 mod 0 eq {
+ a apop.right exch /a exch def
+ } {
+ b apop.right exch /b exch def
+ } ifelse
+ } for
+ ]
+ end
+} bind def
+
+(wigglesort) test.start
+[ 1 5 1 1 6 4 ] is_wigglesorted not test
+[ 1 6 1 5 1 4 ] is_wigglesorted test
+[ 1 3 2 2 3 1 ] is_wigglesorted not test
+[ 2 3 1 3 1 2 ] is_wigglesorted test
+[ 1 3 2 2 3 1 ] is_wigglesorted not test
+[ 1 5 1 1 6 4 ] wigglesort is_wigglesorted test
+[ 1 3 2 2 3 1 ] wigglesort is_wigglesorted test
+[ 1 3 2 2 2 3 1 ] wigglesort is_wigglesorted test
+test.end
diff --git a/challenge-197/roger-bell-west/python/ch-1.py b/challenge-197/roger-bell-west/python/ch-1.py
new file mode 100755
index 0000000000..99a9b9f533
--- /dev/null
+++ b/challenge-197/roger-bell-west/python/ch-1.py
@@ -0,0 +1,21 @@
+#! /usr/bin/python3
+
+import unittest
+
+def movezero(l):
+ o = [i for i in l if i != 0]
+ o.extend([0] * (len(l) - len(o)))
+ return o
+
+class TestMovezero(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(movezero([1, 0, 3, 0, 0, 5]), [1, 3, 5, 0, 0, 0], 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(movezero([1, 6, 4]), [1, 6, 4], 'example 2')
+
+ def test_ex3(self):
+ self.assertEqual(movezero([0, 1, 0, 2, 0]), [1, 2, 0, 0, 0], 'example 3')
+
+unittest.main()
diff --git a/challenge-197/roger-bell-west/python/ch-2.py b/challenge-197/roger-bell-west/python/ch-2.py
new file mode 100755
index 0000000000..5be5f0dd62
--- /dev/null
+++ b/challenge-197/roger-bell-west/python/ch-2.py
@@ -0,0 +1,61 @@
+#! /usr/bin/python3
+
+import unittest
+
+def is_wigglesorted(l):
+ for i in range(len(l) - 1):
+ if i % 2 == 0:
+ if l[i] >= l[i+1]:
+ return False
+ else:
+ if l[i] <= l[i+1]:
+ return False
+ return True
+
+def wigglesort(l):
+ s = l
+ s.sort()
+ le = len(s)
+ p = le // 2
+ a = s[0 : p]
+ b = s[p : le]
+ i = 0
+ o = []
+ if le % 2 == 1:
+ o.append(s[p])
+ b = s[p + 1 : le]
+ i = 1
+ for j in range(i, le):
+ if j % 2 == 0:
+ o.append(a.pop())
+ else:
+ o.append(b.pop())
+ return o
+
+class TestRangelist(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(is_wigglesorted([1,5,1,1,6,4]), 0, 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(is_wigglesorted([1,6,1,5,1,4]), 1, 'example 2')
+
+ def test_ex3(self):
+ self.assertEqual(is_wigglesorted([1,3,2,2,3,1]), 0, 'example 3')
+
+ def test_ex4(self):
+ self.assertEqual(is_wigglesorted([2,3,1,3,1,2]), 1, 'example 4')
+
+ def test_ex5(self):
+ self.assertEqual(is_wigglesorted([1,3,2,2,3,1]), 0, 'example 5')
+
+ def test_ex6(self):
+ self.assertEqual(is_wigglesorted(wigglesort([1,5,1,1,6,4])), 1, 'example 6')
+
+ def test_ex7(self):
+ self.assertEqual(is_wigglesorted(wigglesort([1,3,2,2,3,1])), 1, 'example 7')
+
+ def test_ex8(self):
+ self.assertEqual(is_wigglesorted(wigglesort([1,3,2,2,2,3,1])), 1, 'example 8')
+
+unittest.main()
diff --git a/challenge-197/roger-bell-west/raku/ch-1.p6 b/challenge-197/roger-bell-west/raku/ch-1.p6
new file mode 100755
index 0000000000..f2c8458c39
--- /dev/null
+++ b/challenge-197/roger-bell-west/raku/ch-1.p6
@@ -0,0 +1,15 @@
+#! /usr/bin/perl6
+
+use Test;
+
+plan 3;
+
+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');
+
+sub movezero(@l) {
+ my @o = grep {$_ != 0}, @l;
+ @o.push(((0) xx (@l.elems - @o.elems)).Slip);
+ return @o;
+}
diff --git a/challenge-197/roger-bell-west/raku/ch-2.p6 b/challenge-197/roger-bell-west/raku/ch-2.p6
new file mode 100755
index 0000000000..c28e5c597d
--- /dev/null
+++ b/challenge-197/roger-bell-west/raku/ch-2.p6
@@ -0,0 +1,52 @@
+#! /usr/bin/perl6
+
+use Test;
+
+plan 8;
+
+is(is_wigglesorted([1,5,1,1,6,4]), False, 'example 1');
+is(is_wigglesorted([1,6,1,5,1,4]), True, 'example 2');
+is(is_wigglesorted([1,3,2,2,3,1]), False, 'example 3');
+is(is_wigglesorted([2,3,1,3,1,2]), True, 'example 4');
+is(is_wigglesorted([1,3,2,2,3,1]), False, 'example 5');
+is(is_wigglesorted(wigglesort([1,5,1,1,6,4])), True, 'example 6');
+is(is_wigglesorted(wigglesort([1,3,2,2,3,1])), True, 'example 7');
+is(is_wigglesorted(wigglesort([1,3,2,2,2,3,1])), True, 'example 8');
+
+sub is_wigglesorted(@l) {
+ for (0..@l.elems-2) -> $i {
+ if ($i % 2 == 0) {
+ if (@l[$i] >= @l[$i+1]) {
+ return False;
+ }
+ } else {
+ if (@l[$i] <= @l[$i+1]) {
+ return False;
+ }
+ }
+ }
+ return True;
+}
+
+sub wigglesort(@l) {
+ my @s = sort @l;
+ my $le = @s.elems;
+ my $p = floor($le / 2);
+ my @a = @s[0 .. $p - 1];
+ my @b = @s[$p .. @s.elems - 1];
+ my $i = 0;
+ my @o;
+ if ($le % 2 == 1) {
+ @o.push(@s[$p]);
+ @b = @s[$p + 1 .. @s.elems - 1];
+ $i = 1;
+ }
+ for ($i .. @s.elems - 1) -> $j {
+ if ($j % 2 == 0) {
+ @o.push(@a.pop);
+ } else {
+ @o.push(@b.pop);
+ }
+ }
+ return @o;
+}
diff --git a/challenge-197/roger-bell-west/ruby/ch-1.rb b/challenge-197/roger-bell-west/ruby/ch-1.rb
new file mode 100755
index 0000000000..837d7faa38
--- /dev/null
+++ b/challenge-197/roger-bell-west/ruby/ch-1.rb
@@ -0,0 +1,23 @@
+#! /usr/bin/ruby
+
+require 'test/unit'
+
+def movezero(l)
+ o = l.find_all{|i| i != 0}
+ return o.concat(Array.new(l.length-o.length, 0))
+end
+
+class TestMovezero < Test::Unit::TestCase
+
+ def test_ex1
+ assert_equal([1, 3, 5, 0, 0, 0], movezero([1, 0, 3, 0, 0, 5]))
+ end
+
+ def test_ex2
+ assert_equal([1, 6, 4], movezero([1, 6, 4]))
+ end
+
+ def test_ex3
+ assert_equal([1, 2, 0, 0, 0], movezero([0, 1, 0, 2, 0]))
+ end
+end
diff --git a/challenge-197/roger-bell-west/ruby/ch-2.rb b/challenge-197/roger-bell-west/ruby/ch-2.rb
new file mode 100755
index 0000000000..ac385f3b2c
--- /dev/null
+++ b/challenge-197/roger-bell-west/ruby/ch-2.rb
@@ -0,0 +1,76 @@
+#! /usr/bin/ruby
+
+require 'test/unit'
+