From a57a3a568368c406944dd042728ed141b66a02a3 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Tue, 5 Aug 2025 12:09:33 +0100 Subject: RogerBW solutions for challenge no. 333 --- challenge-333/roger-bell-west/crystal/ch-1.cr | 44 +++++++++ challenge-333/roger-bell-west/crystal/ch-2.cr | 37 ++++++++ challenge-333/roger-bell-west/javascript/ch-1.js | 68 ++++++++++++++ challenge-333/roger-bell-west/javascript/ch-2.js | 78 +++++++++++++++ challenge-333/roger-bell-west/kotlin/ch-1.kt | 55 +++++++++++ challenge-333/roger-bell-west/kotlin/ch-2.kt | 51 ++++++++++ challenge-333/roger-bell-west/lua/ch-1.lua | 70 ++++++++++++++ challenge-333/roger-bell-west/lua/ch-2.lua | 83 ++++++++++++++++ challenge-333/roger-bell-west/perl/ch-1.pl | 52 ++++++++++ challenge-333/roger-bell-west/perl/ch-2.pl | 30 ++++++ challenge-333/roger-bell-west/postscript/ch-1.ps | 98 +++++++++++++++++++ challenge-333/roger-bell-west/postscript/ch-2.ps | 115 +++++++++++++++++++++++ challenge-333/roger-bell-west/python/ch-1.py | 48 ++++++++++ challenge-333/roger-bell-west/python/ch-2.py | 34 +++++++ challenge-333/roger-bell-west/raku/ch-1.p6 | 50 ++++++++++ challenge-333/roger-bell-west/raku/ch-2.p6 | 28 ++++++ challenge-333/roger-bell-west/ruby/ch-1.rb | 52 ++++++++++ challenge-333/roger-bell-west/ruby/ch-2.rb | 44 +++++++++ challenge-333/roger-bell-west/rust/ch-1.rs | 64 +++++++++++++ challenge-333/roger-bell-west/rust/ch-2.rs | 47 +++++++++ challenge-333/roger-bell-west/scala/ch-1.scala | 58 ++++++++++++ challenge-333/roger-bell-west/scala/ch-2.scala | 49 ++++++++++ challenge-333/roger-bell-west/tests.json | 44 +++++++++ challenge-333/roger-bell-west/typst/ch-1.typ | 47 +++++++++ challenge-333/roger-bell-west/typst/ch-2.typ | 40 ++++++++ 25 files changed, 1386 insertions(+) create mode 100755 challenge-333/roger-bell-west/crystal/ch-1.cr create mode 100755 challenge-333/roger-bell-west/crystal/ch-2.cr create mode 100755 challenge-333/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-333/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-333/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-333/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-333/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-333/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-333/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-333/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-333/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-333/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-333/roger-bell-west/python/ch-1.py create mode 100755 challenge-333/roger-bell-west/python/ch-2.py create mode 100755 challenge-333/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-333/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-333/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-333/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-333/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-333/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-333/roger-bell-west/scala/ch-1.scala create mode 100644 challenge-333/roger-bell-west/scala/ch-2.scala create mode 100644 challenge-333/roger-bell-west/tests.json create mode 100644 challenge-333/roger-bell-west/typst/ch-1.typ create mode 100644 challenge-333/roger-bell-west/typst/ch-2.typ diff --git a/challenge-333/roger-bell-west/crystal/ch-1.cr b/challenge-333/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..46e0894f1a --- /dev/null +++ b/challenge-333/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,44 @@ +#! /usr/bin/crystal + +def straightline(a) + b = a.to_set.to_a + if b.size < 3 + return true + end + p = b[0][0] + q = b[1][0] - b[0][0] + r = b[0][1] + s = b[1][1] - b[0][1] + b[2, b.size].each do |tpair| + if q == 0.0 && tpair[0] != b[0][0] + return false + end + if s == 0.0 && tpair[1] != b[0][1] + return false + end + if q != 0.0 && s != 0.0 + n1 = (tpair[0] - p) / q + n2 = (tpair[1] - r) / s + if n1 != n2 + return false + end + end + end + true +end + +require "spec" +describe "straightline" do + it "test_ex1" do + straightline([[2, 1], [2, 3], [2, 5]]).should eq true + end + it "test_ex2" do + straightline([[1, 4], [3, 4], [10, 4]]).should eq true + end + it "test_ex3" do + straightline([[0, 0], [1, 1], [2, 3]]).should eq false + end + it "test_ex4" do + straightline([[1, 1], [1, 1], [1, 1]]).should eq true + end +end diff --git a/challenge-333/roger-bell-west/crystal/ch-2.cr b/challenge-333/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..0fd80ad1df --- /dev/null +++ b/challenge-333/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,37 @@ +#! /usr/bin/crystal + +def duplicatezeros(a) + b = Array(Int32).new + a.each do |n| + b.push(n) + if a.size == b.size + break + end + if n == 0 + b.push(0) + if a.size == b.size + break + end + end + end + b +end + +require "spec" +describe "duplicatezeros" do + it "test_ex1" do + duplicatezeros([1, 0, 2, 3, 0, 4, 5, 0]).should eq [1, 0, 0, 2, 3, 0, 0, 4] + end + it "test_ex2" do + duplicatezeros([1, 2, 3]).should eq [1, 2, 3] + end + it "test_ex3" do + duplicatezeros([1, 2, 3, 0]).should eq [1, 2, 3, 0] + end + it "test_ex4" do + duplicatezeros([0, 0, 1, 2]).should eq [0, 0, 0, 0] + end + it "test_ex5" do + duplicatezeros([1, 2, 0, 3, 4]).should eq [1, 2, 0, 0, 3] + end +end diff --git a/challenge-333/roger-bell-west/javascript/ch-1.js b/challenge-333/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..81a6e2af02 --- /dev/null +++ b/challenge-333/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,68 @@ +#! /usr/bin/node + +"use strict" + +function straightline(a) { + let b = []; + for (let xy of a) { + let u = true; + for (let bxy of b) { + if (xy[0] == bxy[0] && xy[1] == bxy[1]) { + u = false; + break; + } + } + if (u) { + b.push(xy); + } + } + if (b.length < 3) { + return true; + } + const p = b[0][0]; + const q = b[1][0] - b[0][0]; + const r = b[0][1]; + const s = b[1][1] - b[0][1]; + for (let tp = 2; tp < b.length; b++) { + const tpair = b[tp]; + if (q == 0 && tpair[0] != b[0][0]) { + return false; + } + if (s == 0 && tpair[1] != b[0][1]) { + return false; + } + if (q != 0 && s != 0) { + const n1 = (tpair[0] - p ) / q; + const n2 = (tpair[1] - r ) / s; + if (n1 != n2) { + return false; + } + } + } + return true; +} + +if (straightline([[2, 1], [2, 3], [2, 5]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (straightline([[1, 4], [3, 4], [10, 4]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!straightline([[0, 0], [1, 1], [2, 3]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (straightline([[1, 1], [1, 1], [1, 1]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-333/roger-bell-west/javascript/ch-2.js b/challenge-333/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..899fd0576b --- /dev/null +++ b/challenge-333/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,78 @@ +#! /usr/bin/node + +"use strict" + +function duplicatezeros(a) { + let b = []; + for (let n of a) { + b.push(n) + if (a.length == b.length) { + break; + } + if (n == 0) { + b.push(0); + if (a.length == b.length) { + break; + } + } + } + return b +} + +// 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; + } +} + +if (deepEqual(duplicatezeros([1, 0, 2, 3, 0, 4, 5, 0]), [1, 0, 0, 2, 3, 0, 0, 4])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(duplicatezeros([1, 2, 3]), [1, 2, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(duplicatezeros([1, 2, 3, 0]), [1, 2, 3, 0])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(duplicatezeros([0, 0, 1, 2]), [0, 0, 0, 0])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(duplicatezeros([1, 2, 0, 3, 4]), [1, 2, 0, 0, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-333/roger-bell-west/kotlin/ch-1.kt b/challenge-333/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..fe02b12588 --- /dev/null +++ b/challenge-333/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,55 @@ +fun straightline(a: List>): Boolean { + val b = a.distinct() + if (b.size < 3) { + return true + } + val p = b[0][0] + val q = b[1][0] - b[0][0] + val r = b[0][1] + val s = b[1][1] - b[0][1] + for (tpair in b.drop(2)) { + if (q == 0.0 && tpair[0] != b[0][0]) { + return false + } + if (s == 0.0 && tpair[1] != b[0][1]) { + return false + } + if (q != 0.0 && s != 0.0) { + val n1 = (tpair[0] - p ) / q + val n2 = (tpair[1] - r ) / s + if (n1 != n2) { + return false + } + } + } + return true +} + +fun main() { + + if (straightline(listOf(listOf(2.0, 1.0), listOf(2.0, 3.0), listOf(2.0, 5.0)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (straightline(listOf(listOf(1.0, 4.0), listOf(3.0, 4.0), listOf(10.0, 4.0)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!straightline(listOf(listOf(0.0, 0.0), listOf(1.0, 1.0), listOf(2.0, 3.0)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (straightline(listOf(listOf(1.0, 1.0), listOf(1.0, 1.0), listOf(1.0, 1.0)))) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-333/roger-bell-west/kotlin/ch-2.kt b/challenge-333/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..fa0b92a96c --- /dev/null +++ b/challenge-333/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,51 @@ +fun duplicatezeros(a: List): List { + var b = ArrayList() + for (n in a) { + b.add(n) + if (a.size == b.size) { + break + } + if (n == 0) { + b.add(0) + if (a.size == b.size) { + break + } + } + } + return b.toList() +} + +fun main() { + + if (duplicatezeros(listOf(1, 0, 2, 3, 0, 4, 5, 0)) == listOf(1, 0, 0, 2, 3, 0, 0, 4)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (duplicatezeros(listOf(1, 2, 3)) == listOf(1, 2, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (duplicatezeros(listOf(1, 2, 3, 0)) == listOf(1, 2, 3, 0)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (duplicatezeros(listOf(0, 0, 1, 2)) == listOf(0, 0, 0, 0)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (duplicatezeros(listOf(1, 2, 0, 3, 4)) == listOf(1, 2, 0, 0, 3)) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-333/roger-bell-west/lua/ch-1.lua b/challenge-333/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..7a5ff61942 --- /dev/null +++ b/challenge-333/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,70 @@ +#! /usr/bin/lua + +function straightline(a) + local b = {} + for _, xy in ipairs(a) do + local u = true + for __, bxy in ipairs(b) do + if xy[1] == bxy[1] and xy[2] == bxy[2] then + u = false + break + end + end + if u then + table.insert(b, xy) + end + end + if #b < 3 then + return true + end + local p = b[1][1] + local q = b[2][1] - b[1][1] + local r = b[1][2] + local s = b[2][2] - b[1][2] + for tp = 2, #b do + local tpair = b[tp] + if q == 0 and tpair[1] ~= b[1][1] then + return false + end + if s == 0 and tpair[2] ~= b[1][2] then + return false + end + if q ~= 0 and s ~= 0 then + local n1 = (tpair[1] - p) / q + local n2 = (tpair[2] - r) / s + if n1 ~= n2 then + return false + end + end + end + return true +end + +if straightline({{2, 1}, {2, 3}, {2, 5}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if straightline({{1, 4}, {3, 4}, {10, 4}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not straightline({{0, 0}, {1, 1}, {2, 3}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if straightline({{1, 1}, {1, 1}, {1, 1}}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-333/roger-bell-west/lua/ch-2.lua b/challenge-333/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..0496f2566a --- /dev/null +++ b/challenge-333/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,83 @@ +#! /usr/bin/lua + +-- by Michael Anderson at +-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua +-- modified by Roger +function recursive_compare(t1,t2) + -- Use usual comparison first. + if t1==t2 then return true end + -- We only support non-default behavior for tables + if (type(t1)~="table") then return false end + -- They better have the same metatables + local mt1 = getmetatable(t1) + local mt2 = getmetatable(t2) + if( not recursive_compare(mt1,mt2) ) then return false end + -- Build list of all keys + local kk = {} + for k1, _ in pairs(t1) do + kk[k1] = true + end + for k2, _ in pairs(t2) do + kk[k2] = true + end + -- Check each key that exists in at least one table + for _, k in ipairs(kk) do + if (not recursive_compare(t1[k], t2[k])) then + return false + end + end + return true +end + +function duplicatezeros(a) + local b = {} + for _, n in ipairs(a) do + table.insert(b, n) + if #a == #b then + break + end + if n == 0 then + table.insert(b, 0) + if #a == #b then + break + end + end + end + return b +end + +if recursive_compare(duplicatezeros({1, 0, 2, 3, 0, 4, 5, 0}), {1, 0, 0, 2, 3, 0, 0, 4}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(duplicatezeros({1, 2, 3}), {1, 2, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(duplicatezeros({1, 2, 3, 0}), {1, 2, 3, 0}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(duplicatezeros({0, 0, 1, 2}), {0, 0, 0, 0}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(duplicatezeros({1, 2, 0, 3, 4}), {1, 2, 0, 0, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-333/roger-bell-west/perl/ch-1.pl b/challenge-333/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..fab3bf7dbe --- /dev/null +++ b/challenge-333/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,52 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 4; + +is(straightline([[2, 1], [2, 3], [2, 5]]), 1, 'example 1'); +is(straightline([[1, 4], [3, 4], [10, 4]]), 1, 'example 2'); +is(straightline([[0, 0], [1, 1], [2, 3]]), 0, 'example 3'); +is(straightline([[1, 1], [1, 1], [1, 1]]), 1, 'example 4'); + +sub straightline($a) { + my @b; + foreach my $xy (@{$a}) { + my $u = 1; + foreach my $bxy (@b) { + if ($xy->[0] == $bxy->[0] && $xy->[1] == $bxy->[1]) { + $u = 0; + last; + } + } + if ($u) { + push @b, $xy; + } + } + if (scalar @b < 3) { + return 1; + } + my $p = $b[0][0]; + my $q = $b[1][0] - $b[0][0]; + my $r = $b[0][1]; + my $s = $b[1][1] - $b[0][1]; + foreach my $tp (2 .. $#b) { + my $tpair = $b[$tp]; + if ($q == 0 && $tpair->[0] != $b[0][0]) { + return 0; + } + if ($s == 0 && $tpair->[1] != $b[0][1]) { + return 0; + } + if ($q != 0 && $s != 0) { + my $n1 = ($tpair->[0] - $p ) / $q; + my $n2 = ($tpair->[1] - $r ) / $s; + if ($n1 != $n2) { + return 0; + } + } + } + 1; +} diff --git a/challenge-333/roger-bell-west/perl/ch-2.pl b/challenge-333/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..0be84b2fdd --- /dev/null +++ b/challenge-333/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,30 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is_deeply(duplicatezeros([1, 0, 2, 3, 0, 4, 5, 0]), [1, 0, 0, 2, 3, 0, 0, 4], 'example 1'); +is_deeply(duplicatezeros([1, 2, 3]), [1, 2, 3], 'example 2'); +is_deeply(duplicatezeros([1, 2, 3, 0]), [1, 2, 3, 0], 'example 3'); +is_deeply(duplicatezeros([0, 0, 1, 2]), [0, 0, 0, 0], 'example 4'); +is_deeply(duplicatezeros([1, 2, 0, 3, 4]), [1, 2, 0, 0, 3], 'example 5'); + +sub duplicatezeros($a) { + my @b; + foreach my $n (@{$a}) { + push @b, $n; + if ($#{$a} ==$#b) { + last; + } + if ($n == 0) { + push @b, 0; + if ($#{$a} ==$#b) { + last; + } + } + } + \@b; +} diff --git a/challenge-333/roger-bell-west/postscript/ch-1.ps b/challenge-333/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..86e75eca38 --- /dev/null +++ b/challenge-333/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,98 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 + +/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 + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/apush.right { % [a b] c -> [a b c] + exch + [ exch aload length 2 add -1 roll ] +} bind def + + +% end included library code + +/straightline { + 0 dict begin + /a exch def + /b 0 array def + a { + /xy exch def + /u true def + b { + /bxy exch def + xy 0 get bxy 0 get eq xy 1 get bxy 1 get eq and { + /u false def + exit + } if + } forall + u { + /b b xy apush.right def + } if + } forall + b length 3 lt { + true + } { + true + /p b 0 get 0 get def + /q b 1 get 0 get b 0 get 0 get sub def + /r b 0 get 1 get def + /s b 1 get 1 get b 0 get 1 get sub def + 2 1 b length 1 sub { + /tpair exch b exch get def + q 0 eq tpair 0 get b 0 get 0 get ne and { + pop false + exit + } if + s 0 eq tpair 1 get b 0 get 1 get ne and { + pop false + exit + } if + q 0 ne s 0 ne and { + tpair 0 get p sub q div + tpair 1 get r sub s div + ne { + pop false + exit + } if + } if + } for + } ifelse + end +} bind def + +(straightline) test.start +[[2 1] [2 3] [2 5]] straightline test +[[1 4] [3 4] [10 4]] straightline test +[[0 0] [1 1] [2 3]] straightline not test +[[1 1] [1 1] [1 1]] straightline test +test.end diff --git a/challenge-333/roger-bell-west/postscript/ch-2.ps b/challenge-333/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..f3737eeb59 --- /dev/null +++ b/challenge-333/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,115 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 + +/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.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 + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + + +% end included library code + +/duplicatezeros { + dup length exch + [ exch + { + dup 0 eq { + 0 + } if + } forall + ] + exch 0 exch getinterval +} bind def + +(duplicatezeros) test.start +[1 0 2 3 0 4 5 0] duplicatezeros [1 0 0 2 3 0 0 4] deepeq test +[1 2 3] duplicatezeros [1 2 3] deepeq test +[1 2 3 0] duplicatezeros [1 2 3 0] deepeq test +[0 0 1 2] duplicatezeros [0 0 0 0] deepeq test +[1 2 0 3 4] duplicatezeros [1 2 0 0 3] deepeq test +test.end diff --git a/challenge-333/roger-bell-west/python/ch-1.py b/challenge-333/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..4887160e44 --- /dev/null +++ b/challenge-333/roger-bell-west/python/ch-1.py @@ -0,0 +1,48 @@ +#! /usr/bin/python3 + +def straightline(a): + b = [] + for xy in a: + u = True + for bxy in b: + if xy[0] == bxy[0] and xy[1] == bxy[1]: + u = False + break + if u: + b.append(xy) + if len(b) < 3: + return True + p = b[0][0] + q = b[1][0] - b[0][0] + r = b[0][1] + s = b[1][1] - b[0][1] + for tp in range(2, len(b)): + tpair = b[tp] + if q == 0. and tpair[0] != b[0][0]: + return False + if s == 0. and tpair[1] != b[0][1]: + return False + if q != 0 and s != 0: + n1 = (tpair[0] - p) / q + n2 = (tpair[1] - r) / s + if n1 != n2: + return False + return True + +import unittest + +class TestStraightline(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(straightline([[2, 1], [2, 3], [2, 5]]), True, 'example 1') + + def test_ex2(self): + self.assertEqual(straightline([[1, 4], [3, 4], [10, 4]]), True, 'example 2') + + def test_ex3(self): + self.assertEqual(straightline([[0, 0], [1, 1], [2, 3]]), False, 'example 3') + + def test_ex4(self): + self.assertEqual(straightline([[1, 1], [1, 1], [1, 1]]), True, 'example 4') + +unittest.main() diff --git a/challenge-333/roger-bell-west/python/ch-2.py b/challenge-333/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..6d94cdcea9 --- /dev/null +++ b/challenge-333/roger-bell-west/python/ch-2.py @@ -0,0 +1,34 @@ +#! /usr/bin/python3 + +def duplicatezeros(a): + b = [] + for n in a: + b.append(n) + if len(a) == len(b): + break + if n == 0: + b.append(0) + if len(a) == len(b): + break + return b + +import unittest + +class TestDuplicatezeros(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(duplicatezeros([1, 0, 2, 3, 0, 4, 5, 0]), [1, 0, 0, 2, 3, 0, 0, 4], 'example 1') + + def test_ex2(self): + self.assertEqual(duplicatezeros([1, 2, 3]), [1, 2, 3], 'example 2') + + def test_ex3(self): + self.assertEqual(duplicatezeros([1, 2, 3, 0]), [1, 2, 3, 0], 'example 3') + + def test_ex4(self): + self.assertEqual(duplicatezeros([0, 0, 1, 2]), [0, 0, 0, 0], 'example 4') + + def test_ex5(self): + self.assertEqual(duplicatezeros([1, 2, 0, 3, 4]), [1, 2, 0, 0, 3], 'example 5') + +unittest.main() diff --git a/challenge-333/roger-bell-west/raku/ch-1.p6 b/challenge-333/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..869dae695c --- /dev/null +++ b/challenge-333/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,50 @@ +#! /usr/bin/raku + +use Test; + +plan 4; + +is(straightline([[2, 1], [2, 3], [2, 5]]), True, 'example 1'); +is(straightline([[1, 4], [3, 4], [10, 4]]), True, 'example 2'); +is(straightline([[0, 0], [1, 1], [2, 3]]), False, 'example 3'); +is(straightline([[1, 1], [1, 1], [1, 1]]), True, 'example 4'); + +sub straightline(@a) { + my @b; + for @a -> @xy { + my $u = True; + for @b -> @bxy { + if (@xy[0] == @bxy[0] && @xy[1] == @bxy[1]) { + $u = False; + last; + } + } + if ($u) { + @b.push(@xy); + } + } + if (@b.elems < 3) { + return True; + } + my $p = @b[0][0]; + my $q = @b[1][0] - @b[0][0]; + my $r = @b[0][1]; + my $s = @b[1][1] - @b[0][1]; + for (2 .. @b.end) -> $tp { + my @tpair = @b[$tp].flat; + if ($q == 0 && @tpair[0] != @b[0][0]) { + return False; + } + if ($s == 0 && @tpair[1] != @b[0][1]) { + return False; + } + if ($q != 0 && $s != 0) { + my $n1 = (@tpair[0] - $p ) / $q; + my $n2 = (@tpair[1] - $r ) / $s; + if ($n1 != $n2) { + return False; + } + } + } + True; +} diff --git a/challenge-333/roger-bell-west/raku/ch-2.p6 b/challenge-333/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..84010f0b62 --- /dev/null +++ b/challenge-333/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,28 @@ +#! /usr/bin/raku + +use Test; + +plan 5; + +is-deeply(duplicatezeros([1, 0, 2, 3, 0, 4, 5, 0]), [1, 0, 0, 2, 3, 0, 0, 4], 'example 1'); +is-deeply(duplicatezeros([1, 2, 3]), [1, 2, 3], 'example 2'); +is-deeply(duplicatezeros([1, 2, 3, 0]), [1, 2, 3, 0], 'example 3'); +is-deeply(duplicatezeros([0, 0, 1, 2]), [0, 0, 0, 0], 'example 4'); +is-deeply(duplicatezeros([1, 2, 0, 3, 4]), [1, 2, 0, 0, 3], 'example 5'); + +sub duplicatezeros(@a) { + my @b; + for @a -> $n { + @b.push($n); + if (@a.elems == @b.elems) { + last; + } + if ($n == 0) { + @b.push(0); + if (@a.elems == @b.elems) { + last; + } + } + } + @b; +} diff --git a/challenge-333/roger-bell-west/ruby/ch-1.rb b/challenge-333/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..f75a3ad329 --- /dev/null +++ b/challenge-333/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,52 @@ +#! /usr/bin/ruby + +require 'set' + +def straightline(a) + b = a.to_set.to_a + if b.length < 3 + return true + end + p = b[0][0] + q = b[1][0] - b[0][0] + r = b[0][1] + s = b[1][1] - b[0][1] + b.slice(2, b.length).each do |tpair| + if q == 0.0 and tpair[0] != b[0][0] + return false + end + if s == 0.0 and tpair[1] != b[0][1] + return false + end + if q != 0.0 and s != 0.0 + n1 = (tpair[0] - p) / q + n2 = (tpair[1] - r) / s + if n1 != n2 + return false + end + end + end + true +end + +require 'test/unit' + +class TestStraightline < Test::Unit::TestCase + + def test_ex1 + assert_equal(true, straightline([[2, 1], [2, 3], [2, 5]])) + end + + def test_ex2 + assert_equal(true, straightline([[1, 4], [3, 4], [10, 4]])) + end + + def test_ex3 + assert_equal(false, straightline([[0, 0], [1, 1], [2, 3]])) + end + + def test_ex4 + assert_equal(true, straightline([[1, 1], [1, 1], [1, 1]])) + end + +end diff --git a/challenge-333/roger-bell-west/ruby/ch-2.rb b/challenge-333/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..a8c1066aa4 --- /dev/null +++ b/challenge-333/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,44 @@ +#! /usr/bin/ruby + +def duplicatezeros(a) + b = [] + a.each do |n| + b.push(n) + if a.size == b.size + break + end + if n == 0 + b.push(0) + if a.size == b.size + break + end + end + end + b +end + +require 'test/unit' + +class TestDuplicatezeros < Test::Unit::TestCase + + def test_ex1 + assert_equal([1, 0, 0, 2, 3, 0, 0, 4], duplicatezeros([1, 0, 2, 3, 0, 4, 5, 0])) + end + + def test_ex2 + assert_equal([1, 2, 3], duplicatezeros([1, 2, 3])) + end + + def test_ex3 + assert_equal([1, 2, 3, 0], duplicatezeros([1, 2, 3, 0])) + end + + def test_ex4 + assert_equal([0, 0, 0, 0], duplicatezeros([0, 0, 1, 2])) + end + + def test_ex5 + assert_equal([1, 2, 0, 0, 3], duplicatezeros([1, 2, 0, 3, 4])) + end + +end diff --git a/challenge-333/roger-bell-west/rust/ch-1.rs b/challenge-333/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..88fd3f6ca1 --- /dev/null +++ b/challenge-333/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,64 @@ +#! /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!( + straightline(vec![vec![2., 1.], vec![2., 3.], vec![2., 5.]]), + true + ); +} + +#[test] +fn test_ex2() { + assert_eq!( + straightline(vec![vec![1., 4.], vec![3., 4.], vec![10., 4.]]), + true + ); +} + +#[test] +fn test_ex3() { + assert_eq!( + straightline(vec![vec![0., 0.], vec![1., 1.], vec![2., 3.]]), + false + ); +} + +#[test] +fn test_ex4() { + assert_eq!( + straightline(vec![vec![1., 1.], vec![1., 1.], vec![1., 1.]]), + true + ); +} + +fn straightline(a: Vec>) -> bool { + let mut b = a.clone(); + b.sort_by(|a, b| a.partial_cmp(b).unwrap()); + b.dedup(); + if b.len() < 3 { + return true; + } + // x = p + qn; y = r + sn + let p = b[0][0]; + let q = b[1][0] - b[0][0]; + let r = b[0][1]; + let s = b[1][1] - b[0][1]; + for tpair in b.iter().skip(2) { + if q == 0. && tpair[0] != b[0][0] { + return false; + } + if s == 0. && tpair[1] != b[0][1] { + return false; + } + if q != 0. && s != 0. { + let n1 = (tpair[0] - p) / q; + let n2 = (tpair[1] - r) / s; + if n1 != n2 { + return false; + } + } + } + true +} diff --git a/challenge-333/roger-bell-west/rust/ch-2.rs b/challenge-333/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..ec11190244 --- /dev/null +++ b/challenge-333/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,47 @@ +#! /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!( + duplicatezeros(vec![1, 0, 2, 3, 0, 4, 5, 0]), + vec![1, 0, 0, 2, 3, 0, 0, 4] + ); +} + +#[test] +fn test_ex2() { + assert_eq!(duplicatezeros(vec![1, 2, 3]), vec![1, 2, 3]); +} + +#[test] +fn test_ex3() { + assert_eq!(duplicatezeros(vec![1, 2, 3, 0]), vec![1, 2, 3, 0]); +} + +#[test] +fn test_ex4() { + assert_eq!(duplicatezeros(vec![0, 0, 1, 2]), vec![0, 0, 0, 0]); +} + +#[test] +fn test_ex5() { + assert_eq!(duplicatezeros(vec![1, 2, 0, 3, 4]), vec![1, 2, 0, 0, 3]); +} + +fn duplicatezeros(a: Vec) -> Vec { + let mut b = Vec::new(); + for n in &a { + b.push(*n); + if a.len() == b.len() { + break; + } + if *n == 0 { + b.push(0); + if a.len() == b.len() { + break; + } + } + } + b +} diff --git a/challenge-333/roger-bell-west/scala/ch-1.scala b/challenge-333/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..3c1400eae2 --- /dev/null +++ b/challenge-333/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,58 @@ + +object Straightline { + def straightline(a: List[List[Double]]): Boolean = { + var ret = true + val b = a.distinct + if (b.size > 2) { + val p = b(0)(0) + val q = b(1)(0) - b(0)(0) + val r = b(0)(1) + val s = b(1)(1) - b(0)(1) + for (tpair <- b.drop(2)) { + if (ret) { + if (q == 0.0 && tpair(0) != b(0)(0)) { + ret = false + } + if (s == 0.0 && tpair(1) != b(0)(1)) { + ret = false + } + if (q != 0.0 && s != 0.0) { + val n1 = (tpair(0) - p ) / q + val n2 = (tpair(1) - r ) / s + if (n1 != n2) { + ret = false + } + } + } + } + } + ret + } + def main(args: Array[String]) { + if (straightline(List(List(2, 1), List(2, 3), List(2, 5)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (straightline(List(List(1, 4), List(3, 4), List(10, 4)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!straightline(List(List(0, 0), List(1, 1), List(2, 3)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (straightline(List(List(1, 1), List(1, 1), List(1, 1)))) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-333/roger-bell-west/scala/ch-2.scala b/challenge-333/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..2b339c3f90 --- /dev/null +++ b/challenge-333/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,49 @@ +import scala.collection.mutable.ListBuffer + +object Duplicatezeros { + def duplicatezeros(a: List[Int]): List[Int] = { + var b = new ListBuffer[Int] + for (n <- a) { + if (a.size > b.size) { + b += n + if (a.size > b.size && n == 0) { + b += 0 + } + } + } + b.toList + } + def main(args: Array[String]) { + if (duplicatezeros(List(1, 0, 2, 3, 0, 4, 5, 0)) == List(1, 0, 0, 2, 3, 0, 0, 4)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (duplicatezeros(List(1, 2, 3)) == List(1, 2, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (duplicatezeros(List(1, 2, 3, 0)) == List(1, 2, 3, 0)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (duplicatezeros(List(0, 0, 1, 2)) == List(0, 0, 0, 0)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (duplicatezeros(List(1, 2, 0, 3, 4)) == List(1, 2, 0, 0, 3)) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-333/roger-bell-west/tests.json b/challenge-333/roger-bell-west/tests.json new file mode 100644 index 0000000000..e0deefc983 --- /dev/null +++ b/challenge-333/roger-bell-west/tests.json @@ -0,0 +1,44 @@ +{ + "ch-1" : [ + { + "function" : "straightline", + "arguments" : [[2, 1], [2, 3], [2, 5]], + "result" : true + }, + { + "arguments" : [[1, 4], [3, 4], [10, 4]], + "result" : true + }, + { + "arguments" : [[0, 0], [1, 1], [2, 3]], + "result" : false + }, + { + "arguments" : [[1, 1], [1, 1], [1, 1]], + "result" : true + } + ], + "ch-2" : [ + { + "function" : "duplicatezeros", + "arguments" : [1, 0, 2, 3, 0, 4, 5, 0], + "result" : [1, 0, 0, 2, 3, 0, 0, 4] + }, + { + "arguments" : [1, 2, 3], + "result" : [1, 2, 3] + }, + { + "arguments" : [1, 2, 3, 0], + "result" : [1, 2, 3, 0] + }, + { + "arguments" : [0, 0, 1, 2], + "result" : [0, 0, 0, 0] + }, + { + "arguments" : [1, 2, 0, 3, 4], + "result" : [1, 2, 0, 0, 3] + } + ] +} diff --git a/challenge-333/roger-bell-west/typst/ch-1.typ b/challenge-333/roger-bell-west/typst/ch-1.typ new file mode 100644 index 0000000000..96292dc416 --- /dev/null +++ b/challenge-333/roger-bell-west/typst/ch-1.typ @@ -0,0 +1,47 @@ +#let testresult(pass) = { + if pass { + text(fill: green, "Pass") + } else { + text(fill: red, "Fail") + } +} + +#let straightline(a) = { + let b = a.dedup() + if b.len() < 3 { + return true + } + let p = b.at(0).at(1) + let q = b.at(1).at(0) - b.at(0).at(0) + let r = b.at(0).at(1) + let s = b.at(1).at(1) - b.at(0).at(1) + for tpair in range(2, b.len()).map(n => b.at(n)) { + if q == 0. and tpair.at(0) != b.at(0).at(0) { + return false + } + if s == 0. and tpair.at(1) != b.at(0).at(1) { + return false + } + if q != 0. and s != 0. { + let n1 = (tpair.at(0) - p) / q + let n2 = (tpair.at(1) - r) / s + if n1 != n2 { + return false + } + } + } + true +} + +Test 1: + #testresult(straightline(((2, 1), (2, 3), (2, 5)))) + +Test 2: + #testresult(straightline(((1, 4), (3, 4), (10, 4)))) + +Test 3: + #testresult(not straightline(((0, 0), (1, 1), (2, 3)))) + +Test 4: + #testresult(straightline(((1, 1), (1, 1), (1, 1)))) + diff --git a/challenge-333/roger-bell-west/typst/ch-2.typ b/challenge-333/roger-bell-west/typst/ch-2.typ new file mode 100644 index 0000000000..31adf0d095 --- /dev/null +++ b/challenge-333/roger-bell-west/typst/ch-2.typ @@ -0,0 +1,40 @@ +#let testresult(pass) = { + if pass { + text(fill: green, "Pass") + } else { + text(fill: red, "Fail") + } +} + +#let duplicatezeros(a) = { + let b = () + for n in a { + b.push(n) + if a.len() == b.len() { + break + } + if n == 0 { + b.push(0) + if a.len() == b.len() { + break + } + } + } + b +} + +Test 1: + #testresult(duplicatezeros((1, 0, 2, 3, 0, 4, 5, 0)) == (1, 0, 0, 2, 3, 0, 0, 4)) + +Test 2: + #testresult(duplicatezeros((1, 2, 3)) == (1, 2, 3)) + +Test 3: + #testresult(duplicatezeros((1, 2, 3, 0)) == (1, 2, 3, 0)) + +Test 4: + #testresult(duplicatezeros((0, 0, 1, 2)) == (0, 0, 0, 0)) + +Test 5: + #testresult(duplicatezeros((1, 2, 0, 3, 4)) == (1, 2, 0, 0, 3)) + -- cgit