From 1dfb2a7a1ec053840c9d83d31d6b18a9c2ac666e Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Mon, 26 Dec 2022 16:16:12 +0800 Subject: challenge 197, raku solutions --- challenge-197/feng-chang/raku/ch-1.raku | 8 ++++++++ challenge-197/feng-chang/raku/ch-2.raku | 12 ++++++++++++ 2 files changed, 20 insertions(+) create mode 100755 challenge-197/feng-chang/raku/ch-1.raku create mode 100755 challenge-197/feng-chang/raku/ch-2.raku diff --git a/challenge-197/feng-chang/raku/ch-1.raku b/challenge-197/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..8bab342a05 --- /dev/null +++ b/challenge-197/feng-chang/raku/ch-1.raku @@ -0,0 +1,8 @@ +#!/bin/env raku + +unit sub MAIN(*@N); + +for +@N ^... 0 -> \j { + @N.push(@N.splice(j, 1)) if @N[j] == 0; +} +put @N.join(', '); diff --git a/challenge-197/feng-chang/raku/ch-2.raku b/challenge-197/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..8eb42b696b --- /dev/null +++ b/challenge-197/feng-chang/raku/ch-2.raku @@ -0,0 +1,12 @@ +#!/bin/env raku + +unit sub MAIN(*@N); + +@N .= sort; +my @a = @N.splice((+@N + 1) div 2); + +loop (my $i = 1; +@a > 0; $i += 2) { + @N.splice($i, 0, @a.shift); +} + +put @N.join(', '); -- cgit From 778efedbf61fb496100a930f68ad320cc7da0e33 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 26 Dec 2022 09:34:52 +0000 Subject: Solutions for challenge #197 --- challenge-197/roger-bell-west/javascript/ch-1.js | 57 ++++++++ challenge-197/roger-bell-west/javascript/ch-2.js | 91 ++++++++++++ challenge-197/roger-bell-west/kotlin/ch-1.kt | 28 ++++ challenge-197/roger-bell-west/kotlin/ch-2.kt | 90 ++++++++++++ challenge-197/roger-bell-west/lua/ch-1.lua | 55 ++++++++ challenge-197/roger-bell-west/lua/ch-2.lua | 103 ++++++++++++++ challenge-197/roger-bell-west/perl/ch-1.pl | 17 +++ challenge-197/roger-bell-west/perl/ch-2.pl | 54 +++++++ challenge-197/roger-bell-west/postscript/ch-1.ps | 123 ++++++++++++++++ challenge-197/roger-bell-west/postscript/ch-2.ps | 170 +++++++++++++++++++++++ challenge-197/roger-bell-west/python/ch-1.py | 21 +++ challenge-197/roger-bell-west/python/ch-2.py | 61 ++++++++ challenge-197/roger-bell-west/raku/ch-1.p6 | 15 ++ challenge-197/roger-bell-west/raku/ch-2.p6 | 52 +++++++ challenge-197/roger-bell-west/ruby/ch-1.rb | 23 +++ challenge-197/roger-bell-west/ruby/ch-2.rb | 76 ++++++++++ challenge-197/roger-bell-west/rust/ch-1.rs | 24 ++++ challenge-197/roger-bell-west/rust/ch-2.rs | 80 +++++++++++ 18 files changed, 1140 insertions(+) create mode 100755 challenge-197/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-197/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-197/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-197/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-197/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-197/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-197/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-197/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-197/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-197/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-197/roger-bell-west/python/ch-1.py create mode 100755 challenge-197/roger-bell-west/python/ch-2.py create mode 100755 challenge-197/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-197/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-197/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-197/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-197/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-197/roger-bell-west/rust/ch-2.rs 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): List { + 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): 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): List { + 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() + 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' + +def is_wigglesorted(l) + 0.upto(l.length-2) do |i| + if i % 2 == 0 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 + +def wigglesort(l) + s = l.sort + le = s.length + p = le.div(2) + a = s[0 .. p-1] + b = s[p .. -1] + i = 0 + o = [] + if le % 2 == 1 then + o.push(s[p]) + b = s[p+1 .. -1] + i = 1 + end + i.upto(le-1) do |j| + if j % 2 == 0 then + o.push(a.pop) + else + o.push(b.pop) + end + end + return o +end + +class TestRangeList < Test::Unit::TestCase + def test_ex1 + assert_equal(false, is_wigglesorted([1,5,1,1,6,4])) + end + + def test_ex2 + assert_equal(true, is_wigglesorted([1,6,1,5,1,4])) + end + + def test_ex3 + assert_equal(false, is_wigglesorted([1,3,2,2,3,1])) + end + + def test_ex4 + assert_equal(true, is_wigglesorted([2,3,1,3,1,2])) + end + + def test_ex5 + assert_equal(false, is_wigglesorted([1,3,2,2,3,1])) + end + + def test_ex6 + assert_equal(true, is_wigglesorted(wigglesort([1,5,1,1,6,4]))) + end + + def test_ex7 + assert_equal(true, is_wigglesorted(wigglesort([1,3,2,2,3,1]))) + end + + def test_ex8 + assert_equal(true, is_wigglesorted(wigglesort([1,3,2,2,2,3,1]))) + end + +end diff --git a/challenge-197/roger-bell-west/rust/ch-1.rs b/challenge-197/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..326a48501b --- /dev/null +++ b/challenge-197/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,24 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(movezero(vec![1, 0, 3, 0, 0, 5]), vec![1, 3, 5, 0, 0, 0]); +} + +#[test] +fn test_ex2() { + assert_eq!(movezero(vec![1, 6, 4]), vec![1, 6, 4]); +} + +#[test] +fn test_ex3() { + assert_eq!(movezero(vec![0, 1, 0, 2, 0]), vec![1, 2, 0, 0, 0]); +} + +fn movezero(l: Vec) -> Vec { + let t = l.len(); + let mut o: Vec = l.into_iter().filter(|i| *i != 0).collect(); + o.append(&mut vec![0; t-o.len()]); + o +} diff --git a/challenge-197/roger-bell-west/rust/ch-2.rs b/challenge-197/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..d84a3f29e9 --- /dev/null +++ b/challenge-197/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,80 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(is_wigglesorted(vec![1,5,1,1,6,4]), false); +} + +#[test] +fn test_ex2() { + assert_eq!(is_wigglesorted(vec![1,6,1,5,1,4]), true); +} + +#[test] +fn test_ex3() { + assert_eq!(is_wigglesorted(vec![1,3,2,2,3,1]), false); +} + +#[test] +fn test_ex4() { + assert_eq!(is_wigglesorted(vec![2,3,1,3,1,2]), true); +} + +#[test] +fn test_ex5() { + assert_eq!(is_wigglesorted(vec![1,3,2,2,3,1]), false); +} + +#[test] +fn test_ex6() { + assert_eq!(is_wigglesorted(wigglesort(vec![1,5,1,1,6,4])), true); +} + +#[test] +fn test_ex7() { + assert_eq!(is_wigglesorted(wigglesort(vec![1,3,2,2,3,1])), true); +} + +#[test] +fn test_ex8() { + assert_eq!(is_wigglesorted(wigglesort(vec![1,3,2,2,2,3,1])), true); +} + +fn is_wigglesorted(l: Vec) -> bool { + for i in 0..l.len()-1 { + if i % 2 == 0 { + if l[i] >= l[i+1] { + return false; + } + } else { + if l[i] <= l[i+1] { + return false; + } + } + } + true +} + +fn wigglesort(l: Vec) -> Vec { + let mut a = l; + a.sort(); + let le = a.len(); + let p = le / 2; + let mut b = a.split_off(p); + let mut i = 0; + let mut o = Vec::new(); + if le % 2 == 1 { + o.push(b[0]); + b.remove(0); + i = 1; + } + for j in i .. le { + if j % 2 == 0 { + o.push(a.pop().unwrap()); + } else { + o.push(b.pop().unwrap()); + } + } + o +} -- cgit From 5e53ede2741debff8e09453e241c823979f0ea16 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 26 Dec 2022 10:23:34 +0000 Subject: Challenge 197 Solutions (Raku) --- challenge-197/mark-anderson/raku/ch-1.raku | 13 +++++++++++++ challenge-197/mark-anderson/raku/ch-2.raku | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 challenge-197/mark-anderson/raku/ch-1.raku create mode 100644 challenge-197/mark-anderson/raku/ch-2.raku diff --git a/challenge-197/mark-anderson/raku/ch-1.raku b/challenge-197/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..07d7e9cfb5 --- /dev/null +++ b/challenge-197/mark-anderson/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku +use Test; + +is-deeply move-zero(1,0,3,0,0,5), (1,3,5,0,0,0); +is-deeply move-zero(1,6,4), (1,6,4); +is-deeply move-zero(0,1,0,2,0), (1,2,0,0,0); + +sub move-zero(*@arr) +{ + my $c := classify { $_ == 0 ?? 'zeros' !! 'non-zeros' }, @arr; + $c.append: flat $c if $c; + $c.List +} diff --git a/challenge-197/mark-anderson/raku/ch-2.raku b/challenge-197/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..01ad3d233f --- /dev/null +++ b/challenge-197/mark-anderson/raku/ch-2.raku @@ -0,0 +1,17 @@ +#!/usr/bin/env raku +use Test; + +# My output might be different but I think it's still a wiggle sort. + +is-deeply wiggle-sort(1,5,1,1,6,4), (1,4,1,5,1,6); +is-deeply wiggle-sort(1,3,2,2,3,1), (1,2,1,3,2,3); +is-deeply wiggle-sort(1,1,1,2,2,2), (1,2,1,2,1,2); +is-deeply wiggle-sort(1,2,3,4,5,6,7), (1,5,2,6,3,7,4); +is-deeply wiggle-sort(1,2,3,4,5,6,7,8), (1,5,2,6,3,7,4,8); + +sub wiggle-sort(*@a) +{ + @a .= sort; + my $d = @a / 2; + flat roundrobin @a.head($d.ceiling), @a.tail($d.floor); +} -- cgit From c1629a287d53018f2126b4bd62a3ec3c73e89dc5 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 26 Dec 2022 12:49:26 +0000 Subject: Challenge 197 Solutions (Raku) --- challenge-197/mark-anderson/raku/ch-2.raku | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/challenge-197/mark-anderson/raku/ch-2.raku b/challenge-197/mark-anderson/raku/ch-2.raku index 01ad3d233f..0aa4b6e32f 100644 --- a/challenge-197/mark-anderson/raku/ch-2.raku +++ b/challenge-197/mark-anderson/raku/ch-2.raku @@ -1,17 +1,15 @@ #!/usr/bin/env raku use Test; -# My output might be different but I think it's still a wiggle sort. - -is-deeply wiggle-sort(1,5,1,1,6,4), (1,4,1,5,1,6); -is-deeply wiggle-sort(1,3,2,2,3,1), (1,2,1,3,2,3); +is-deeply wiggle-sort(1,5,1,1,6,4), (1,6,1,5,1,4); +is-deeply wiggle-sort(1,3,2,2,3,1), (2,3,1,3,1,2); is-deeply wiggle-sort(1,1,1,2,2,2), (1,2,1,2,1,2); -is-deeply wiggle-sort(1,2,3,4,5,6,7), (1,5,2,6,3,7,4); -is-deeply wiggle-sort(1,2,3,4,5,6,7,8), (1,5,2,6,3,7,4,8); +is-deeply wiggle-sort(1,2,3,4,5,6,7), (4,7,3,6,2,5,1); +is-deeply wiggle-sort(1,2,3,4,5,6,7,8), (4,8,3,7,2,6,1,5); sub wiggle-sort(*@a) { - @a .= sort; + @a .= sort: -*; my $d = @a / 2; - flat roundrobin @a.head($d.ceiling), @a.tail($d.floor); + flat roundrobin @a.tail($d.ceiling), @a.head($d.floor) } -- cgit From 0a8e87c380f1dddca4a8105abdacd7d339b39718 Mon Sep 17 00:00:00 2001 From: Thomas Köhler Date: Mon, 26 Dec 2022 13:54:50 +0100 Subject: Add solution 197 by Thomas Köhler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Köhler --- challenge-197/jeanluc2020/blog-1.txt | 1 + challenge-197/jeanluc2020/blog-2.txt | 1 + challenge-197/jeanluc2020/perl/ch-1.pl | 39 ++++++++++++++++++++++++++++++ challenge-197/jeanluc2020/perl/ch-2.pl | 44 ++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 challenge-197/jeanluc2020/blog-1.txt create mode 100644 challenge-197/jeanluc2020/blog-2.txt create mode 100755 challenge-197/jeanluc2020/perl/ch-1.pl create mode 100755 challenge-197/jeanluc2020/perl/ch-2.pl 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; +} -- cgit From 98d75487e0088ba942d873a437fd74cf560f146f Mon Sep 17 00:00:00 2001 From: Jan Krňávek Date: Mon, 26 Dec 2022 17:27:39 +0100 Subject: solutions week 196 --- challenge-196/wambash/raku/ch-1.raku | 20 ++++++++++++++++++++ challenge-196/wambash/raku/ch-2.raku | 29 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 challenge-196/wambash/raku/ch-1.raku create mode 100644 challenge-196/wambash/raku/ch-2.raku diff --git a/challenge-196/wambash/raku/ch-1.raku b/challenge-196/wambash/raku/ch-1.raku new file mode 100644 index 0000000000..cd867a9c2c --- /dev/null +++ b/challenge-196/wambash/raku/ch-1.raku @@ -0,0 +1,20 @@ +#!/usr/bin/env raku + +sub pattern132 (+@list) { + @list + andthen .combinations: 3 + andthen .first: -> ($i, $j, $k) { $i < $k < $j }\ +} + +multi MAIN (Bool :test($)!) { + use Test; + is pattern132(3,1,4,2),(1,4,2); + is pattern132(1,2,3,4), Nil; + is pattern132(1, 3, 2, 4, 6, 5), (1,3,2); + is pattern132(1, 3, 4, 2), (1,3,2); + done-testing; +} + +multi MAIN (*@list) { + say pattern132 @list +} diff --git a/challenge-196/wambash/raku/ch-2.raku b/challenge-196/wambash/raku/ch-2.raku new file mode 100644 index 0000000000..d4efe9f08e --- /dev/null +++ b/challenge-196/wambash/raku/ch-2.raku @@ -0,0 +1,29 @@ +#!/usr/bin/env raku + +sub range-list-reducer (Capture $a, Int $b) { + $a.tail.tail == $b.pred + ?? \( |$a.head(*-1), (|$a.tail,$b,) ) + !! \( |$a, $b ) +} + +sub range-list (+@list) { + @list + andthen \(.head,), |.skip + andthen .reduce: &range-list-reducer + andthen .grep: Positional + andthen .map: {.head .. .tail} +} + +multi MAIN (Bool :test($)!) { + use Test; + is-deeply range-list-reducer( \(1,(3,4)), 5 ), \(1,(3,4, 5)); + is-deeply range-list-reducer( \(1,(3,4)), 6 ), \(1,(3,4),6 ); + is-deeply range-list(1,3,4,5,7), (3..5,); + is-deeply range-list(1,2,3,6,7,9), (1..3,6..7); + is-deeply range-list(0,1,2,4,5,6,8,9), (0..2, 4..6, 8..9); + done-testing; +} + +multi MAIN (*@list) { + say range-list( @list».Int ) +} -- cgit From 58d2e54e0381157072a8b30d60a6ea06e4f900ae Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 26 Dec 2022 11:46:58 -0500 Subject: Week 197 --- challenge-197/zapwai/perl/ch-1.pl | 28 ++++++++++++++++++++++++++++ challenge-197/zapwai/perl/ch-2.pl | 25 +++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100755 challenge-197/zapwai/perl/ch-1.pl create mode 100755 challenge-197/zapwai/perl/ch-2.pl diff --git a/challenge-197/zapwai/perl/ch-1.pl b/challenge-197/zapwai/perl/ch-1.pl new file mode 100755 index 0000000000..fae666d7d0 --- /dev/null +++ b/challenge-197/zapwai/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +use v5.30.0; +my @list = (1, 0, 3, 0, 0, 5); +# my @list = (0, 1, 0, 2, 0); + +say "Input: (".join(",", @list).")"; +my $numzeros = 0; +foreach (@list) { + $numzeros++ if $_ == 0; +} +my $nonzero = @list - $numzeros - 1; # index of last nonzero entry +sub swap { + for (0 .. $#list - 1) { + if ($list[$_] == 0) { + $list[$_] = $list[$_ + 1]; + $list[$_ + 1] = 0; + } + } +} +my $flag; +do { + $flag=0; + swap(); + foreach (0 .. $#list) { + $flag++ if (($_ <= $nonzero) && ($list[$_] == 0)); + } +} while ($flag); +say "Output: (".join(",", @list).")"; diff --git a/challenge-197/zapwai/perl/ch-2.pl b/challenge-197/zapwai/perl/ch-2.pl new file mode 100755 index 0000000000..0b1dfb4313 --- /dev/null +++ b/challenge-197/zapwai/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl +use v5.30.0; +my $list1 = [1,5,1,1,6,4]; +my $list2 = [1,3,2,2,3,1]; +for my $ref ($list1, $list2) { + my @list = @$ref; + say "Input: \@list: (".join(",", @list).")"; + my $cnt; + do { + $cnt=0; + for (0 .. $#list) { + if ($_ % 2 == 0) { + wiggle(\$cnt, \@list) if ($list[$_ + 1] < $list[$_]); + } else { + wiggle(\$cnt, \@list) if ($list[$_ + 1] > $list[$_]); + } + } + } while ($cnt > 0); + say "Output: (".join(",", @list).")"; +} +sub wiggle { + my ($cnt, $list) = @_; + $$cnt++; + ($$list[$_],$$list[$_ + 1]) = ($$list[$_+1], $$list[$_]); +} -- cgit From 44da709707868ab19c3880a76e584b7cbd587ab7 Mon Sep 17 00:00:00 2001 From: carlos157oliveira Date: Mon, 26 Dec 2022 22:07:45 -0300 Subject: feat: solution to challenge 197 --- challenge-197/carlos-oliveira/perl/ch-1.pl | 14 ++++++++++++++ challenge-197/carlos-oliveira/perl/ch-2.pl | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 challenge-197/carlos-oliveira/perl/ch-1.pl create mode 100644 challenge-197/carlos-oliveira/perl/ch-2.pl diff --git a/challenge-197/carlos-oliveira/perl/ch-1.pl b/challenge-197/carlos-oliveira/perl/ch-1.pl new file mode 100644 index 0000000000..61e4473855 --- /dev/null +++ b/challenge-197/carlos-oliveira/perl/ch-1.pl @@ -0,0 +1,14 @@ +use strict; +use warnings; + +use Data::Dump; + +sub moveZero { + my @listWithoutZeros = grep { $_ != 0 } @_; + return @listWithoutZeros, (0) x (@_ - @listWithoutZeros); +} + +dd moveZero 1, 0, 3, 0, 0, 5; +dd moveZero 1, 6, 4; +dd moveZero 0, 1, 0, 2, 0; + diff --git a/challenge-197/carlos-oliveira/perl/ch-2.pl b/challenge-197/carlos-oliveira/perl/ch-2.pl new file mode 100644 index 0000000000..6219c64d9c --- /dev/null +++ b/challenge-197/carlos-oliveira/perl/ch-2.pl @@ -0,0 +1,27 @@ +use strict; +use warnings; +use 5.36.0; +use Data::Dump; + + +sub wiggle; + + +dd wiggle 1,5,1,1,6,4; +dd wiggle 1,3,2,2,3,1; +dd wiggle 9,8,7,6,5,4,3,2,1; + + +sub wiggle { + my $originalLength = @_; + @_ = sort @_; + my $offset = int @_ / 2; + my @pivots = splice @_, -$offset, $offset; + + for my $i ( reverse 1 .. $#_ + ($originalLength % 2 == 0) ) { + my $pivot = pop @pivots; + splice @_, $i, 0, $pivot; + } + + return @_; +} -- cgit From 02325bd34adafeb1ca0bd33e143217530972ac9a Mon Sep 17 00:00:00 2001 From: LoneWolfiNTj Date: Mon, 26 Dec 2022 22:33:27 -0800 Subject: Robbie Hatley's Perl solutions and blog for Weekly challenge #197 --- challenge-197/robbie-hatley/blog.txt | 1 + challenge-197/robbie-hatley/perl/ch-1.pl | 55 ++++++++++++++++++++++++ challenge-197/robbie-hatley/perl/ch-2.pl | 73 ++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 challenge-197/robbie-hatley/blog.txt create mode 100755 challenge-197/robbie-hatley/perl/ch-1.pl create mode 100755 challenge-197/robbie-hatley/perl/ch-2.pl diff --git a/challenge-197/robbie-hatley/blog.txt b/challenge-197/robbie-hatley/blog.txt new file mode 100644 index 0000000000..e0c5f13b00 --- /dev/null +++ b/challenge-197/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2022/12/robbie-hatleys-solutions-to-perl-weekly.html \ No newline at end of file diff --git a/challenge-197/robbie-hatley/perl/ch-1.pl b/challenge-197/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..bb165912c8 --- /dev/null +++ b/challenge-197/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,55 @@ +#! /usr/bin/perl + +# Robbie Hatley's Perl solution to challenge 197-1 + +=pod + +Task 1: Move Zero +Submitted by: Mohammad S Anwar +You are given a list of integers, @list. Write a script to move all zeros, +if any exist, to the end of the list, while maintaining the relative order +of non-zero elements. + +Example 1: Input: (1, 0, 3, 0, 0, 5) Output: (1, 3, 5, 0, 0, 0) +Example 2: Input: (1, 6, 4) Output: (1, 6, 4) +Example 3: Input: (0, 1, 0, 2, 0) Output: (1, 2, 0, 0, 0) + +=cut + +# NOTE: Input is from built-in array or command-line args. +# If using args, they should be space-separated integers. + +# NOTE: Output is to stdout and will be a printout of each input array, +# followed by the same with all 0s moved to end. + +# Preliminaries: +use v5.36; +use List::Util 'sum0'; + +# Default Inputs: +my @lists = + ( + [1, 0, 3, 0, 0, 5], + [1, 6, 4], + [0, 1, 0, 2, 0] + ); + +# Non-Default Input: +if (@ARGV) {@lists=([@ARGV])} + +# Move zero! Move zero! For great justice, take off every zero! +# ~~AYBABTU + +for (@lists){ + my @list = @{$_}; # Input. + my $z; # A placeholder for a 0. + say ''; # Print blank line. + say "Input = (@list)"; # Announce input. + for ( my $i = 0 ; $i <= $#list ; ++$i ){ # Riffle through list. + last if 0 == sum0(@list[$i..$#list]); # Stop if all remaining elements are 0. + if ( 0 == $list[$i] ){ # If we find a 0, + $z = splice @list, $i, 1; # splice it from current position + splice @list, @list, 0, $z; # and paste it on end. + --$i;}} # Back-up one spot, + # because we altered current location. + say "Output = (@list)";} # Print output. \ No newline at end of file diff --git a/challenge-197/robbie-hatley/perl/ch-2.pl b/challenge-197/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..7a048efef4 --- /dev/null +++ b/challenge-197/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,73 @@ +#! /usr/bin/perl + +# Robbie Hatley's Perl solution to challenge 197-2 + +=pod + +Task 2: +Submitted by: Mohammad S Anwar +You are given a list of integers, @list. +Write a script to perform a Wiggle Sort on the given list. +(A "Wiggle Sort" would be such as list[0] < list[1] > list[2] < list[3]....) + +Example 1: Input: (1,5,1,1,6,4) Output: (1,6,1,5,1,4) +Example 2: Input: (1,3,2,2,3,1) Output: (2,3,1,3,1,2) + +WARNING: This is not the usual way of defining "Wiggle Sort", which + normally uses "<=" and ">=" instead of "<" and ">". + Let's call this version "Strong Wiggle Sort". + +WARNING: Not all inputs are CAPABLE of being Strong-Wiggle-Sorted. + +WARNING: Both Weak and Strong Wiggle Sort are non-deterministic, + so there are often multiple valid wiggle-sortings of an array. + +=cut + +# NOTE: Input is from built-in array or command-line args. +# If using args, they should be space-separated integers. + +# NOTE: Output is to stdout and will be a printout of each input array, +# followed by either a strong-wiggle-sorted version of the input, +# or by "timed out" if no valid sorting could be found in 1000 attempts. + +# Preliminaries: +use v5.36; + +# Default Inputs: +my @arrays= +( + [1,5,1,1,6,4], + [1,3,2,2,3,1] +); + +# Non-Default Input: +if (@ARGV) {@arrays=([@ARGV])} + +# Main body of script: +ARRAY : for (@arrays){ # For each input array. + my @array = @{$_}; # Current input array. + say ''; # Print blank line. + say "Input = (@array)"; # Print input. + my $flag = 1; # We have work to do. + my $shifts = 0; # How many shifts have we worked? + while ( $flag ){ # Loop while we have work to do. + if ( $shifts >=1000 ){ # But if we've worked 1000 shifts, + say "timed out"; # and yet the work still ain't done, + next ARRAY;} # then give up. + ++$shifts; # Yet another work shift. + $flag = 0; # Work is done, maybe? + for ( my $i = 0 ; $i <= $#array - 1 ; ++$i ){ # For each index pair: + if ( $i % 2 ){ # If (odd,even) pair: + if ( $array[$i] <= $array[$i+1] ){ # If pair isn't downward-trending, + $flag = 1; # work's not done. + my $temp = $array[$i]; # swap + $array[$i] = $array[$i+1]; # swap + $array[$i+1] = $temp;}} # swap + else { # Else if (even,odd) pair: + if ( $array[$i] >= $array[$i+1] ){ # If pair isn't upward-trending, + $flag = 1; # work's not done. + my $temp = $array[$i]; # swap + $array[$i] = $array[$i+1]; # swap + $array[$i+1] = $temp;}}}} # swap + say "Output = (@array)";} # Print output. \ No newline at end of file -- cgit From c22de68f0b4b07034ac2ed02fce5a6df04508c89 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Tue, 27 Dec 2022 09:11:50 +0100 Subject: Task 1 done --- challenge-197/luca-ferrari/raku/ch-1.p6 | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 challenge-197/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-197/luca-ferrari/raku/ch-1.p6 b/challenge-197/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..2bf7eb3e3c --- /dev/null +++ b/challenge-197/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,9 @@ +#!raku + +# Perl Weekly Challenge 197 + +sub MAIN( *@list where { @list.grep( * ~~ Int ).elems == @list.elems } ) { + my ( @swapped ); + @swapped = | @list.grep( * !~~ 0 ), | @list.grep( * ~~ 0 ); + @swapped.join( ',' ).say; +} -- cgit From 772fb588198fa4e4ddb2a7e6b322c0abfe4c8b80 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Tue, 27 Dec 2022 09:47:05 +0100 Subject: Task 2 done --- challenge-197/luca-ferrari/raku/ch-2.p6 | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 challenge-197/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-197/luca-ferrari/raku/ch-2.p6 b/challenge-197/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..050a1d9c1a --- /dev/null +++ b/challenge-197/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,38 @@ +#!raku + +# Perl Weekly Challenge 197 + +sub MAIN( *@list where { @list.grep( * ~~ Int ).elems == @list.elems } ) { + my @sorted = @list; + my $done = False; + + # list[0] < list[1] > list[2] < list[3]…. + while ( ! $done ) { + $done = True; + for 0 ..^ @sorted.elems - 1 -> $i { + if ( $i %% 2 ) { + if ( @sorted[ $i ] >= @sorted[ $i + 1 ] ) { + # need to change + my $temp = @sorted[ $i ]; + @sorted[ $i ] = @sorted[ $i + 1 ]; + @sorted[ $i + 1 ] = $temp; + $done = False; + } + } + else { + if ( @sorted[ $i ] <= @sorted[ $i + 1 ] ) { + # need to change + my $temp = @sorted[ $i ]; + @sorted[ $i ] = @sorted[ $i + 1 ]; + @sorted[ $i + 1 ] = $temp; + $done = False; + + } + } + + } + } + + @list.join( ',' ).say; + @sorted.join(',').say; +} -- cgit From 1ed55b1aa7084fca33cda1b3c3ce683a16a65ac5 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Tue, 27 Dec 2022 09:57:20 +0100 Subject: Task 1 plperl done --- challenge-197/luca-ferrari/postgresql/ch-1.plperl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 challenge-197/luca-ferrari/postgresql/ch-1.plperl diff --git a/challenge-197/luca-ferrari/postgresql/ch-1.plperl b/challenge-197/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..b1c1ac7c4c --- /dev/null +++ b/challenge-197/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,23 @@ +-- Perl Weekly Challenge 197 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc197; + +/* +estdb=> select pwc197.task1_plperl( array[1,2,3,0,4,5,0,9,0,10]::int[] ); + task1_plperl +------------------------ + {1,2,3,4,5,9,10,0,0,0} + +*/ + +CREATE OR REPLACE FUNCTION +pwc197.task1_plperl( int[] ) +RETURNS int[] +AS $CODE$ +my ( $list ) = @_; +my @sorted = ( grep( { $_ != 0 } $list->@* ), + grep( { $_ == 0 } $list->@* ) ); +return [ @sorted ]; +$CODE$ +LANGUAGE plperl; -- cgit From a3330f36884d9948dfa587adceda9aa9b2d0e9a9 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Tue, 27 Dec 2022 10:11:10 +0100 Subject: Task 2 plperl --- challenge-197/luca-ferrari/postgresql/ch-2.plperl | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 challenge-197/luca-ferrari/postgresql/ch-2.plperl diff --git a/challenge-197/luca-ferrari/postgresql/ch-2.plperl b/challenge-197/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..6da49c355b --- /dev/null +++ b/challenge-197/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,33 @@ +-- Perl Weekly Challenge 197 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc197; + +CREATE OR REPLACE FUNCTION +pwc197.task2_plperl( int[] ) +RETURNS int[] +AS $CODE$ +my ( $array ) = @_; +my $sorted = [ $array->@* ]; +my $need_swap = 1; + +while ( $need_swap ) { + $need_swap = 0; + for my $i ( 0 .. $sorted->@* - 1 ) { + my $need_swap = ( ( $i % 2 == 0 ) && ( $sorted->[ $i ] >= $sorted->[ $i + 1 ] ) ) + || ( ( $i % 2 != 0 ) && ( $sorted->[ $i ] <= $sorted->[ $i + 1 ] ) ); + + if ( $need_swap ) { + my $temp = $sorted->[ $i ]; + $sorted->[ $i ] = $sorted->[ $i + 1 ]; + $sorted->[ $i + 1 ] = $temp; + } + + + } +} + +return $sorted; + +$CODE$ +LANGUAGE plperl; -- cgit From b9e7ca7fd5f39d5554f2f0e19cb40fc669b8abf9 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Tue, 27 Dec 2022 10:14:22 +0100 Subject: Task 1 plpgsql --- challenge-197/luca-ferrari/postgresql/ch-1.sql | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 challenge-197/luca-ferrari/postgresql/ch-1.sql diff --git a/challenge-197/luca-ferrari/postgresql/ch-1.sql b/challenge-197/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..e96464bc11 --- /dev/null +++ b/challenge-197/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,32 @@ +-- Perl Weekly Challenge 197 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc197; + +CREATE OR REPLACE FUNCTION +pwc197.task1_plpgsql( l int[] ) +RETURNS int[] +AS $CODE$ +DECLARE + i int; + v int[]; + zeros int := 0; +BEGIN + FOREACH i IN ARRAY l LOOP + IF i = 0 THEN + zeros := zeros + 1; + CONTINUE; + END IF; + + v := v || i; + END LOOP; + + WHILE zeros > 0 LOOP + v := v || 0; + zeros := zeros - 1; + END LOOP; + + RETURN v; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From dfde3cabf69b575de3f8bcfbc09854fcaeb5743d Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Tue, 27 Dec 2022 10:25:38 +0100 Subject: Task 2 plpgsql --- challenge-197/luca-ferrari/postgresql/ch-2.sql | 49 ++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 challenge-197/luca-ferrari/postgresql/ch-2.sql diff --git a/challenge-197/luca-ferrari/postgresql/ch-2.sql b/challenge-197/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..b68f56beea --- /dev/null +++ b/challenge-197/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,49 @@ +-- Perl Weekly Challenge 197 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc197; + +CREATE OR REPLACE FUNCTION +pwc197.task2_plpgsql( l int[] ) +RETURNS int[] +AS $CODE$ +DECLARE + i int; + v int[]; + need_change bool := false; + t int; +BEGIN + need_change := true; + v := l; + raise info 'array %', v; + + WHILE need_change LOOP + need_change := false; + FOR i IN 0 .. array_length( v, 1 ) - 1 LOOP + + IF i % 2 = 0 THEN + IF v[ i ] <= v[ i + 1 ] THEN + need_change := true; + END IF; + ELSE + IF v[i] >= v[ i + 1 ] THEN + need_change := true; + END IF; + END IF; + + + IF need_change THEN + t := v[i]; + v[i] := v[i + 1]; + v[ i + 1 ]:= t; + END IF; + END LOOP; + + END LOOP; + + + + RETURN v; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 8e0af9c25bb164f276229b9288f762b23c6dbdbb Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Tue, 27 Dec 2022 10:39:13 +0100 Subject: Blog references --- challenge-197/luca-ferrari/blog-1.txt | 1 + challenge-197/luca-ferrari/blog-2.txt | 1 + challenge-197/luca-ferrari/blog-3.txt | 1 + challenge-197/luca-ferrari/blog-4.txt | 1 + challenge-197/luca-ferrari/blog-5.txt | 1 + challenge-197/luca-ferrari/blog-6.txt | 1 + 6 files changed, 6 insertions(+) create mode 100644 challenge-197/luca-ferrari/blog-1.txt create mode 100644 challenge-197/luca-ferrari/blog-2.txt create mode 100644 challenge-197/luca-ferrari/blog-3.txt create mode 100644 challenge-197/luca-ferrari/blog-4.txt create mode 100644 challenge-197/luca-ferrari/blog-5.txt create mode 100644 challenge-197/luca-ferrari/blog-6.txt diff --git a/challenge-197/luca-ferrari/blog-1.txt b/challenge-197/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..e77d7d7e28 --- /dev/null +++ b/challenge-197/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/27/PerlWeeklyChallenge197.html#task1 diff --git a/challenge-197/luca-ferrari/blog-2.txt b/challenge-197/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..e736905357 --- /dev/null +++ b/challenge-197/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/27/PerlWeeklyChallenge197.html#task2 diff --git a/challenge-197/luca-ferrari/blog-3.txt b/challenge-197/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..70e98fc193 --- /dev/null +++ b/challenge-197/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/27/PerlWeeklyChallenge197.html#task1plperl diff --git a/challenge-197/luca-ferrari/blog-4.txt b/challenge-197/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..84f58849b0 --- /dev/null +++ b/challenge-197/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/27/PerlWeeklyChallenge197.html#task2plperl diff --git a/challenge-197/luca-ferrari/blog-5.txt b/challenge-197/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..d6fdc85651 --- /dev/null +++ b/challenge-197/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/27/PerlWeeklyChallenge197.html#task1plpgsql diff --git a/challenge-197/luca-ferrari/blog-6.txt b/challenge-197/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..9f4359a5a3 --- /dev/null +++ b/challenge-197/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/27/PerlWeeklyChallenge197.html#task2plpgsql -- cgit From 31d74d24779de6f2b89ebddd78507887943cccdc Mon Sep 17 00:00:00 2001 From: Stephen Lynn Date: Tue, 27 Dec 2022 20:28:30 +0800 Subject: pwc 197 --- challenge-197/steve-g-lynn/blog.txt | 1 + challenge-197/steve-g-lynn/perl/ch-1.sh | 3 +++ challenge-197/steve-g-lynn/perl/ch-2.pl | 44 +++++++++++++++++++++++++++++++++ challenge-197/steve-g-lynn/raku/ch-1.sh | 3 +++ challenge-197/steve-g-lynn/raku/ch-2.p6 | 38 ++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+) create mode 100644 challenge-197/steve-g-lynn/blog.txt create mode 100755 challenge-197/steve-g-lynn/perl/ch-1.sh create mode 100755 challenge-197/steve-g-lynn/perl/ch-2.pl create mode 100755 challenge-197/steve-g-lynn/raku/ch-1.sh create mode 100755 challenge-197/steve-g-lynn/raku/ch-2.p6 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