From 7a82574a741ed3fa340a24b8ad6cfdb9c93a78a9 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Tue, 2 Sep 2025 13:17:15 +0100 Subject: RogerBW solutions for challenge no. 337 --- challenge-337/roger-bell-west/crystal/ch-1.cr | 31 +++ challenge-337/roger-bell-west/crystal/ch-2.cr | 39 ++++ challenge-337/roger-bell-west/javascript/ch-1.js | 73 +++++++ challenge-337/roger-bell-west/javascript/ch-2.js | 52 +++++ challenge-337/roger-bell-west/kotlin/ch-1.kt | 45 ++++ challenge-337/roger-bell-west/kotlin/ch-2.kt | 52 +++++ challenge-337/roger-bell-west/lua/ch-1.lua | 85 ++++++++ challenge-337/roger-bell-west/lua/ch-2.lua | 65 ++++++ challenge-337/roger-bell-west/perl/ch-1.pl | 24 ++ challenge-337/roger-bell-west/perl/ch-2.pl | 32 +++ challenge-337/roger-bell-west/postscript/ch-1.ps | 267 +++++++++++++++++++++++ challenge-337/roger-bell-west/postscript/ch-2.ps | 73 +++++++ challenge-337/roger-bell-west/python/ch-1.py | 31 +++ challenge-337/roger-bell-west/python/ch-2.py | 36 +++ challenge-337/roger-bell-west/raku/ch-1.p6 | 22 ++ challenge-337/roger-bell-west/raku/ch-2.p6 | 29 +++ challenge-337/roger-bell-west/ruby/ch-1.rb | 38 ++++ challenge-337/roger-bell-west/ruby/ch-2.rb | 68 ++++++ challenge-337/roger-bell-west/rust/ch-1.rs | 41 ++++ challenge-337/roger-bell-west/rust/ch-2.rs | 50 +++++ challenge-337/roger-bell-west/scala/ch-1.scala | 47 ++++ challenge-337/roger-bell-west/scala/ch-2.scala | 55 +++++ challenge-337/roger-bell-west/tests.json | 73 +++++++ challenge-337/roger-bell-west/typst/ch-1.typ | 35 +++ challenge-337/roger-bell-west/typst/ch-2.typ | 43 ++++ 25 files changed, 1406 insertions(+) create mode 100755 challenge-337/roger-bell-west/crystal/ch-1.cr create mode 100755 challenge-337/roger-bell-west/crystal/ch-2.cr create mode 100755 challenge-337/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-337/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-337/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-337/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-337/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-337/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-337/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-337/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-337/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-337/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-337/roger-bell-west/python/ch-1.py create mode 100755 challenge-337/roger-bell-west/python/ch-2.py create mode 100755 challenge-337/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-337/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-337/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-337/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-337/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-337/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-337/roger-bell-west/scala/ch-1.scala create mode 100644 challenge-337/roger-bell-west/scala/ch-2.scala create mode 100644 challenge-337/roger-bell-west/tests.json create mode 100644 challenge-337/roger-bell-west/typst/ch-1.typ create mode 100644 challenge-337/roger-bell-west/typst/ch-2.typ diff --git a/challenge-337/roger-bell-west/crystal/ch-1.cr b/challenge-337/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..28347ee415 --- /dev/null +++ b/challenge-337/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,31 @@ +#! /usr/bin/crystal + +def smallerthancurrent(a) + b = a.sort() + m = Hash(Int32, Int32).new + b.each_with_index do |v, i| + if !m.has_key?(v) + m[v] = i + end + end + a.map{|x| m[x]} +end + +require "spec" +describe "smallerthancurrent" do + it "test_ex1" do + smallerthancurrent([6, 5, 4, 8]).should eq [2, 1, 0, 3] + end + it "test_ex2" do + smallerthancurrent([7, 7, 7, 7]).should eq [0, 0, 0, 0] + end + it "test_ex3" do + smallerthancurrent([5, 4, 3, 2, 1]).should eq [4, 3, 2, 1, 0] + end + it "test_ex4" do + smallerthancurrent([-1, 0, 3, -2, 1]).should eq [1, 2, 4, 0, 3] + end + it "test_ex5" do + smallerthancurrent([0, 1, 1, 2, 0]).should eq [0, 2, 2, 4, 0] + end +end diff --git a/challenge-337/roger-bell-west/crystal/ch-2.cr b/challenge-337/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..92301cc1ab --- /dev/null +++ b/challenge-337/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,39 @@ +#! /usr/bin/crystal + +def oddmatrix(rows, cols, points) + rm = Set(Int32).new + cm = Set(Int32).new + points.each do |p| + if rm.includes?(p[0]) + rm.delete(p[0]) + else + rm.add(p[0]) + end + if cm.includes?(p[1]) + cm.delete(p[1]) + else + cm.add(p[1]) + end + end + rm.size * (cols - cm.size) + + cm.size * (rows - rm.size) +end + +require "spec" +describe "oddmatrix" do + it "test_ex1" do + oddmatrix(2, 3, [[0, 1], [1, 1]]).should eq 6 + end + it "test_ex2" do + oddmatrix(2, 2, [[1, 1], [0, 0]]).should eq 0 + end + it "test_ex3" do + oddmatrix(3, 3, [[0, 0], [1, 2], [2, 1]]).should eq 0 + end + it "test_ex4" do + oddmatrix(1, 5, [[0, 2], [0, 4]]).should eq 2 + end + it "test_ex5" do + oddmatrix(4, 2, [[1, 0], [3, 1], [2, 0], [0, 1]]).should eq 8 + end +end diff --git a/challenge-337/roger-bell-west/javascript/ch-1.js b/challenge-337/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..9c51c05c65 --- /dev/null +++ b/challenge-337/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,73 @@ +#! /usr/bin/node + +"use strict" + +function smallerthancurrent(a) { + let b = [...a]; + b.sort(function(a, b) {return a-b}); + let m = new Map; + b.forEach((v, i) => { + if (!m.has(v)) { + m.set(v, i); + } + }); + return a.map( x => m.get(x) ); +} + +// 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(smallerthancurrent([6, 5, 4, 8]), [2, 1, 0, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(smallerthancurrent([7, 7, 7, 7]), [0, 0, 0, 0])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(smallerthancurrent([5, 4, 3, 2, 1]), [4, 3, 2, 1, 0])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(smallerthancurrent([-1, 0, 3, -2, 1]), [1, 2, 4, 0, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(smallerthancurrent([0, 1, 1, 2, 0]), [0, 2, 2, 4, 0])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-337/roger-bell-west/javascript/ch-2.js b/challenge-337/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..d1f0677096 --- /dev/null +++ b/challenge-337/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,52 @@ +#! /usr/bin/node + +"use strict" + +function oddmatrix(rows, cols, points) { + let rm = new Set; + let cm = new Set; + for (let p of points) { + if (rm.has(p[0])) { + rm.delete(p[0]); + } else { + rm.add(p[0]); + } + if (cm.has(p[1])) { + cm.delete(p[1]); + } else { + cm.add(p[1]); + } + } + return rm.size * (cols - cm.size) + cm.size * (rows - rm.size); +} + +if (oddmatrix(2, 3, [[0, 1], [1, 1]]) == 6) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (oddmatrix(2, 2, [[1, 1], [0, 0]]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (oddmatrix(3, 3, [[0, 0], [1, 2], [2, 1]]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (oddmatrix(1, 5, [[0, 2], [0, 4]]) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (oddmatrix(4, 2, [[1, 0], [3, 1], [2, 0], [0, 1]]) == 8) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-337/roger-bell-west/kotlin/ch-1.kt b/challenge-337/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..2341884f7e --- /dev/null +++ b/challenge-337/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,45 @@ +fun smallerthancurrent(a: List): List { + val b = a.sorted() + var m = mutableMapOf() + for ((i, v) in b.withIndex()) { + if (!m.contains(v)) { + m.put(v, i) + } + } + return a.map{ m.getValue(it) }.toList() +} + +fun main() { + + if (smallerthancurrent(listOf(6, 5, 4, 8)) == listOf(2, 1, 0, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallerthancurrent(listOf(7, 7, 7, 7)) == listOf(0, 0, 0, 0)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallerthancurrent(listOf(5, 4, 3, 2, 1)) == listOf(4, 3, 2, 1, 0)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallerthancurrent(listOf(-1, 0, 3, -2, 1)) == listOf(1, 2, 4, 0, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallerthancurrent(listOf(0, 1, 1, 2, 0)) == listOf(0, 2, 2, 4, 0)) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-337/roger-bell-west/kotlin/ch-2.kt b/challenge-337/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..76cc20a5f1 --- /dev/null +++ b/challenge-337/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,52 @@ +fun oddmatrix(rows: Int, cols: Int, points: List>): Int { + var rm = mutableSetOf() + var cm = mutableSetOf() + for (p in points) { + if (rm.contains(p[0])) { + rm.remove(p[0]) + } else { + rm.add(p[0]) + } + if (cm.contains(p[1])) { + cm.remove(p[1]) + } else { + cm.add(p[1]) + } + } + return rm.size * (cols - cm.size) + cm.size * (rows - rm.size) +} + +fun main() { + + if (oddmatrix(2, 3, listOf(listOf(0, 1), listOf(1, 1))) == 6) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (oddmatrix(2, 2, listOf(listOf(1, 1), listOf(0, 0))) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (oddmatrix(3, 3, listOf(listOf(0, 0), listOf(1, 2), listOf(2, 1))) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (oddmatrix(1, 5, listOf(listOf(0, 2), listOf(0, 4))) == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (oddmatrix(4, 2, listOf(listOf(1, 0), listOf(3, 1), listOf(2, 0), listOf(0, 1))) == 8) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-337/roger-bell-west/lua/ch-1.lua b/challenge-337/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..c0c8541010 --- /dev/null +++ b/challenge-337/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,85 @@ +#! /usr/bin/lua + +function smallerthancurrent(a) + local b = {} + for _, x in ipairs(a) do + table.insert(b, x) + end + table.sort(b) + local m = {} + for i, v in ipairs(b) do + if m[v] == nil then + m[v] = i - 1 + end + end + local out = {} + for _, x in ipairs(a) do + table.insert(out, m[x]) + end + return out +end + +-- 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 + +if recursive_compare(smallerthancurrent({6, 5, 4, 8}), {2, 1, 0, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(smallerthancurrent({7, 7, 7, 7}), {0, 0, 0, 0}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(smallerthancurrent({5, 4, 3, 2, 1}), {4, 3, 2, 1, 0}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(smallerthancurrent({-1, 0, 3, -2, 1}), {1, 2, 4, 0, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(smallerthancurrent({0, 1, 1, 2, 0}), {0, 2, 2, 4, 0}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-337/roger-bell-west/lua/ch-2.lua b/challenge-337/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..946d52e7e8 --- /dev/null +++ b/challenge-337/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,65 @@ +#! /usr/bin/lua + +function oddmatrix(rows, cols, points) + local rm = {} + local cm = {} + for _, p in ipairs(points) do + if rm[p[1]] == nil then + rm[p[1]] = true + else + rm[p[1]] = nil + end + if cm[p[2]] == nil then + cm[p[2]] = true + else + cm[p[2]] = nil + end + end + local cml = length(cm) + local rml = length(rm) + return rml * (cols - cml) + cml * (rows -rml) +end + +function length(t) + local l = 0 + for k, v in pairs(t) do + l = l + 1 + end + return l +end + +if oddmatrix(2, 3, {{0, 1}, {1, 1}}) == 6 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if oddmatrix(2, 2, {{1, 1}, {0, 0}}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if oddmatrix(3, 3, {{0, 0}, {1, 2}, {2, 1}}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if oddmatrix(1, 5, {{0, 2}, {0, 4}}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if oddmatrix(4, 2, {{1, 0}, {3, 1}, {2, 0}, {0, 1}}) == 8 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-337/roger-bell-west/perl/ch-1.pl b/challenge-337/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..4283c29a12 --- /dev/null +++ b/challenge-337/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,24 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is_deeply(smallerthancurrent([6, 5, 4, 8]), [2, 1, 0, 3], 'example 1'); +is_deeply(smallerthancurrent([7, 7, 7, 7]), [0, 0, 0, 0], 'example 2'); +is_deeply(smallerthancurrent([5, 4, 3, 2, 1]), [4, 3, 2, 1, 0], 'example 3'); +is_deeply(smallerthancurrent([-1, 0, 3, -2, 1]), [1, 2, 4, 0, 3], 'example 4'); +is_deeply(smallerthancurrent([0, 1, 1, 2, 0]), [0, 2, 2, 4, 0], 'example 5'); + +sub smallerthancurrent($a) { + my @b = sort {$::a <=> $::b} @{$a}; + my %m; + while (my ($i, $v) = each @b) { + unless (exists $m{$v}) { + $m{$v} = $i; + } + } + return [map {$m{$_}} @{$a}]; +} diff --git a/challenge-337/roger-bell-west/perl/ch-2.pl b/challenge-337/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..b9642dd43d --- /dev/null +++ b/challenge-337/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,32 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is(oddmatrix(2, 3, [[0, 1], [1, 1]]), 6, 'example 1'); +is(oddmatrix(2, 2, [[1, 1], [0, 0]]), 0, 'example 2'); +is(oddmatrix(3, 3, [[0, 0], [1, 2], [2, 1]]), 0, 'example 3'); +is(oddmatrix(1, 5, [[0, 2], [0, 4]]), 2, 'example 4'); +is(oddmatrix(4, 2, [[1, 0], [3, 1], [2, 0], [0, 1]]), 8, 'example 5'); + +sub oddmatrix($rows, $cols, $points) { + my %rm; + my %cm; + foreach my $p (@{$points}) { + if (exists $rm{$p->[0]}) { + delete $rm{$p->[0]}; + } else { + $rm{$p->[0]} = 1; + } + if (exists $cm{$p->[1]}) { + delete $cm{$p->[1]}; + } else { + $cm{$p->[1]} = 1; + } + } + (scalar %rm) * ($cols - (scalar %cm)) + + (scalar %cm) * ($rows - (scalar %rm)); +} diff --git a/challenge-337/roger-bell-west/postscript/ch-1.ps b/challenge-337/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..4d07f49f91 --- /dev/null +++ b/challenge-337/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,267 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 cmp 0 ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot cmp 0 le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + end +} bind def + +/quicksort { + { quicksort.cmp } quicksort.with_comparator +} bind def + +/quicksort.with_comparator { % [ a c b ] { comparator } -> [ a b c ] + 2 dict begin + /cmp exch def + /arr exch def + arr length 0 gt { + 0 arr length 1 sub quicksort.main + } if + arr + end +} bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} bind def + +/enumerate.array { + 1 dict begin + /a exch def + [ + 0 1 a length 1 sub { + [ exch dup a exch get ] + } for + ] + 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 + +/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 + +/map { % array proc -> array + 2 dict begin + /p exch def + [ exch + { + p + } forall + ] + 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 + +/quicksort.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} 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 + +/deepcopy { + 2 dict begin + /a exch def + a type /dicttype eq { + << + a keys { + /k exch def + k + a k get deepcopy + } forall + >> + } { + a type /arraytype eq { + [ + a { + deepcopy + } forall + ] + } { + a type /stringtype eq { + a dup length string cvs + } { + a + } ifelse + } ifelse + } ifelse + 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 + + +% end included library code + +/smallerthancurrent { + 0 dict begin + /a exch def + /b a deepcopy quicksort def + /m 0 dict def + b enumerate.array { + aload pop + /v exch def + /i exch def + m v known not { + m v i put + } if + } forall + a { m exch get } map + end +} bind def + +(smallerthancurrent) test.start +[6 5 4 8] smallerthancurrent [2 1 0 3] deepeq test +[7 7 7 7] smallerthancurrent [0 0 0 0] deepeq test +[5 4 3 2 1] smallerthancurrent [4 3 2 1 0] deepeq test +[-1 0 3 -2 1] smallerthancurrent [1 2 4 0 3] deepeq test +[0 1 1 2 0] smallerthancurrent [0 2 2 4 0] deepeq test +test.end diff --git a/challenge-337/roger-bell-west/postscript/ch-2.ps b/challenge-337/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..05832d6c6f --- /dev/null +++ b/challenge-337/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,73 @@ +%!PS + +% begin included library code +% see https://codeberg.org/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 + +/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.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + + +% end included library code + +/oddmatrix { + 0 dict begin + /points exch def + /cols exch def + /rows exch def + /rm 0 dict def + /cm 0 dict def + points { + aload pop + /p1 exch def + /p0 exch def + rm p0 known { + rm p0 undef + } { + rm p0 true put + } ifelse + cm p1 known { + cm p1 undef + } { + cm p1 true put + } ifelse + } forall + rm length cols cm length sub mul + cm length rows rm length sub mul + add + end +} bind def + +(oddmatrix) test.start +2 3 [[0 1] [1 1]] oddmatrix 6 eq test +2 2 [[1 1] [0 0]] oddmatrix 0 eq test +3 3 [[0 0] [1 2] [2 1]] oddmatrix 0 eq test +1 5 [[0 2] [0 4]] oddmatrix 2 eq test +4 2 [[1 0] [3 1] [2 0] [0 1]] oddmatrix 8 eq test +test.end diff --git a/challenge-337/roger-bell-west/python/ch-1.py b/challenge-337/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..7c7ba2fd5c --- /dev/null +++ b/challenge-337/roger-bell-west/python/ch-1.py @@ -0,0 +1,31 @@ +#! /usr/bin/python3 + +def smallerthancurrent(a): + b = a.copy() + b.sort() + m = dict() + for i, v in enumerate(b): + if v not in m: + m[v] = i + return [m[x] for x in a] + +import unittest + +class TestSmallerthancurrent(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(smallerthancurrent([6, 5, 4, 8]), [2, 1, 0, 3], 'example 1') + + def test_ex2(self): + self.assertEqual(smallerthancurrent([7, 7, 7, 7]), [0, 0, 0, 0], 'example 2') + + def test_ex3(self): + self.assertEqual(smallerthancurrent([5, 4, 3, 2, 1]), [4, 3, 2, 1, 0], 'example 3') + + def test_ex4(self): + self.assertEqual(smallerthancurrent([-1, 0, 3, -2, 1]), [1, 2, 4, 0, 3], 'example 4') + + def test_ex5(self): + self.assertEqual(smallerthancurrent([0, 1, 1, 2, 0]), [0, 2, 2, 4, 0], 'example 5') + +unittest.main() diff --git a/challenge-337/roger-bell-west/python/ch-2.py b/challenge-337/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..1137f8491b --- /dev/null +++ b/challenge-337/roger-bell-west/python/ch-2.py @@ -0,0 +1,36 @@ +#! /usr/bin/python3 + +def oddmatrix(rows, cols, points): + rm = set() + cm = set() + for p in points: + if p[0] in rm: + rm.discard(p[0]) + else: + rm.add(p[0]) + if p[1] in cm: + cm.discard(p[1]) + else: + cm.add(p[1]) + return len(rm) * (cols - len(cm)) + len(cm) * (rows - len(rm)) + +import unittest + +class TestOddmatrix(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(oddmatrix(2, 3, [[0, 1], [1, 1]]), 6, 'example 1') + + def test_ex2(self): + self.assertEqual(oddmatrix(2, 2, [[1, 1], [0, 0]]), 0, 'example 2') + + def test_ex3(self): + self.assertEqual(oddmatrix(3, 3, [[0, 0], [1, 2], [2, 1]]), 0, 'example 3') + + def test_ex4(self): + self.assertEqual(oddmatrix(1, 5, [[0, 2], [0, 4]]), 2, 'example 4') + + def test_ex5(self): + self.assertEqual(oddmatrix(4, 2, [[1, 0], [3, 1], [2, 0], [0, 1]]), 8, 'example 5') + +unittest.main() diff --git a/challenge-337/roger-bell-west/raku/ch-1.p6 b/challenge-337/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..9bb15a68de --- /dev/null +++ b/challenge-337/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,22 @@ +#! /usr/bin/raku + +use Test; + +plan 5; + +is-deeply(smallerthancurrent([6, 5, 4, 8]), [2, 1, 0, 3], 'example 1'); +is-deeply(smallerthancurrent([7, 7, 7, 7]), [0, 0, 0, 0], 'example 2'); +is-deeply(smallerthancurrent([5, 4, 3, 2, 1]), [4, 3, 2, 1, 0], 'example 3'); +is-deeply(smallerthancurrent([-1, 0, 3, -2, 1]), [1, 2, 4, 0, 3], 'example 4'); +is-deeply(smallerthancurrent([0, 1, 1, 2, 0]), [0, 2, 2, 4, 0], 'example 5'); + +sub smallerthancurrent(@a) { + my @b = @a.sort({$^a <=> $^b}); + my %m; + for @b.kv -> $i, $v { + unless (%m{$v}:exists) { + %m{$v} = $i; + } + } + return Array(@a.map({%m{$_}})); +} diff --git a/challenge-337/roger-bell-west/raku/ch-2.p6 b/challenge-337/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..dab4b86627 --- /dev/null +++ b/challenge-337/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,29 @@ +#! /usr/bin/raku + +use Test; + +plan 5; + +is(oddmatrix(2, 3, [[0, 1], [1, 1]]), 6, 'example 1'); +is(oddmatrix(2, 2, [[1, 1], [0, 0]]), 0, 'example 2'); +is(oddmatrix(3, 3, [[0, 0], [1, 2], [2, 1]]), 0, 'example 3'); +is(oddmatrix(1, 5, [[0, 2], [0, 4]]), 2, 'example 4'); +is(oddmatrix(4, 2, [[1, 0], [3, 1], [2, 0], [0, 1]]), 8, 'example 5'); + +sub oddmatrix($rows, $cols, @points) { + my %rm = SetHash.new; + my %cm = SetHash.new; + for @points -> @p { + if (%rm{@p[0]}:exists) { + %rm{@p[0]}:delete; + } else { + %rm{@p[0]}++; + } + if (%cm{@p[1]}:exists) { + %cm{@p[1]}:delete; + } else { + %cm{@p[1]}++; + } + } + %rm.elems * ($cols - %cm.elems) + %cm.elems * ($rows - %rm.elems); +} diff --git a/challenge-337/roger-bell-west/ruby/ch-1.rb b/challenge-337/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..7f4bc64c1e --- /dev/null +++ b/challenge-337/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,38 @@ +#! /usr/bin/ruby + +def smallerthancurrent(a) + b = a.sort() + m = Hash.new + b.each_with_index do |v, i| + if !m.has_key?(v) + m[v] = i + end + end + a.map{|x| m[x]} +end + +require 'test/unit' + +class TestSmallerthancurrent < Test::Unit::TestCase + + def test_ex1 + assert_equal([2, 1, 0, 3], smallerthancurrent([6, 5, 4, 8])) + end + + def test_ex2 + assert_equal([0, 0, 0, 0], smallerthancurrent([7, 7, 7, 7])) + end + + def test_ex3 + assert_equal([4, 3, 2, 1, 0], smallerthancurrent([5, 4, 3, 2, 1])) + end + + def test_ex4 + assert_equal([1, 2, 4, 0, 3], smallerthancurrent([-1, 0, 3, -2, 1])) + end + + def test_ex5 + assert_equal([0, 2, 2, 4, 0], smallerthancurrent([0, 1, 1, 2, 0])) + end + +end diff --git a/challenge-337/roger-bell-west/ruby/ch-2.rb b/challenge-337/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..fdd4c29ed7 --- /dev/null +++ b/challenge-337/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,68 @@ +#! /usr/bin/ruby + +require 'set' + +def oddmatrix(rows, cols, points) + rm = Set.new + cm = Set.new + points.each do |p| + if rm.includes?(p[0]) + rm.delete(p[0]) + else + rm.add(p[0]) + end + if cm.includes?(p[1]) + cm.delete(p[1]) + else + cm.add(p[1]) + end + end + rm.size * (cols - cm.size) + + cm.size * (rows - rm.size) +end + +def oddmatrix(rows, cols, points) + mx = [] + 1.upto(rows) do |r| + row = [] + 1.upto(cols) do |c| + row.push(0) + end + mx.push(row) + end + points.each do |p| + 0.upto(rows - 1) do |x| + mx[x][p[1]] += 1 + end + 0.upto(cols - 1) do |y| + mx[p[0]][y] += 1 + end + end + mx.map{|x| x.filter{|y| y % 2 == 1}.length}.sum +end + +require 'test/unit' + +class TestOddmatrix < Test::Unit::TestCase + + def test_ex1 + assert_equal(6, oddmatrix(2, 3, [[0, 1], [1, 1]])) + end + + def test_ex2 + assert_equal(0, oddmatrix(2, 2, [[1, 1], [0, 0]])) + end + + def test_ex3 + assert_equal(0, oddmatrix(3, 3, [[0, 0], [1, 2], [2, 1]])) + end + + def test_ex4 + assert_equal(2, oddmatrix(1, 5, [[0, 2], [0, 4]])) + end + + def test_ex5 + assert_equal(8, oddmatrix(4, 2, [[1, 0], [3, 1], [2, 0], [0, 1]])) + end + +end diff --git a/challenge-337/roger-bell-west/rust/ch-1.rs b/challenge-337/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..f4c70ef447 --- /dev/null +++ b/challenge-337/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,41 @@ +#! /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!(smallerthancurrent(vec![6, 5, 4, 8]), vec![2, 1, 0, 3]); +} + +#[test] +fn test_ex2() { + assert_eq!(smallerthancurrent(vec![7, 7, 7, 7]), vec![0, 0, 0, 0]); +} + +#[test] +fn test_ex3() { + assert_eq!(smallerthancurrent(vec![5, 4, 3, 2, 1]), vec![4, 3, 2, 1, 0]); +} + +#[test] +fn test_ex4() { + assert_eq!(smallerthancurrent(vec![-1, 0, 3, -2, 1]), vec![1, 2, 4, 0, 3]); +} + +#[test] +fn test_ex5() { + assert_eq!(smallerthancurrent(vec![0, 1, 1, 2, 0]), vec![0, 2, 2, 4, 0]); +} + +use std::collections::HashMap; + +fn smallerthancurrent(a: Vec) -> Vec { + let mut b = a.clone(); + b.sort(); + let mut m = HashMap::new(); + for (i, v) in b.into_iter().enumerate() { + if !m.contains_key(&v) { + m.insert(v, i); + } + } + a.iter().map(|x| m.get(x).unwrap()).copied().collect::>() +} diff --git a/challenge-337/roger-bell-west/rust/ch-2.rs b/challenge-337/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..49b55240f7 --- /dev/null +++ b/challenge-337/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,50 @@ +#! /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!(oddmatrix(2, 3, vec![vec![0, 1], vec![1, 1]]), 6); +} + +#[test] +fn test_ex2() { + assert_eq!(oddmatrix(2, 2, vec![vec![1, 1], vec![0, 0]]), 0); +} + +#[test] +fn test_ex3() { + assert_eq!(oddmatrix(3, 3, vec![vec![0, 0], vec![1, 2], vec![2, 1]]), 0); +} + +#[test] +fn test_ex4() { + assert_eq!(oddmatrix(1, 5, vec![vec![0, 2], vec![0, 4]]), 2); +} + +#[test] +fn test_ex5() { + assert_eq!( + oddmatrix(4, 2, vec![vec![1, 0], vec![3, 1], vec![2, 0], vec![0, 1]]), + 8 + ); +} + +use std::collections::HashSet; + +fn oddmatrix(rows: usize, cols: usize, points: Vec>) -> usize { + let mut rm = HashSet::new(); + let mut cm = HashSet::new(); + for p in points { + if rm.contains(&p[0]) { + rm.remove(&p[0]); + } else { + rm.insert(p[0]); + } + if cm.contains(&p[1]) { + cm.remove(&p[1]); + } else { + cm.insert(p[1]); + } + } + rm.len() * (cols - cm.len()) + cm.len() * (rows - rm.len()) +} diff --git a/challenge-337/roger-bell-west/scala/ch-1.scala b/challenge-337/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..caa38789ee --- /dev/null +++ b/challenge-337/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,47 @@ +import scala.collection.mutable + +object Smallerthancurrent { + def smallerthancurrent(a: List[Int]): List[Int] = { + val b = a.sortWith(_ < _) + var m = mutable.Map.empty[Int, Int] + for ((v, i) <- b.zipWithIndex) { + if (!m.contains(v)) { + m += ((v, i)) + } + } + a.map(x => m(x)).toList + } + def main(args: Array[String]) { + if (smallerthancurrent(List(6, 5, 4, 8)) == List(2, 1, 0, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallerthancurrent(List(7, 7, 7, 7)) == List(0, 0, 0, 0)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallerthancurrent(List(5, 4, 3, 2, 1)) == List(4, 3, 2, 1, 0)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallerthancurrent(List(-1, 0, 3, -2, 1)) == List(1, 2, 4, 0, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallerthancurrent(List(0, 1, 1, 2, 0)) == List(0, 2, 2, 4, 0)) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-337/roger-bell-west/scala/ch-2.scala b/challenge-337/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..87f328d4b5 --- /dev/null +++ b/challenge-337/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,55 @@ +import scala.collection.mutable + +object Oddmatrix { + def oddmatrix(rows: Int, cols: Int, points: List[List[Int]]): Int = { + var rm = mutable.Set.empty[Int] + var cm = mutable.Set.empty[Int] + for (p <- points) { + if (rm.contains(p(0))) { + rm -= p(0) + } else { + rm += p(0) + } + if (cm.contains(p(1))) { + cm -= p(1) + } else { + cm += p(1) + } + } + rm.size * (cols - cm.size) + + cm.size * (rows - rm.size) + } + def main(args: Array[String]) { + if (oddmatrix(2, 3, List(List(0, 1), List(1, 1))) == 6) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (oddmatrix(2, 2, List(List(1, 1), List(0, 0))) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (oddmatrix(3, 3, List(List(0, 0), List(1, 2), List(2, 1))) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (oddmatrix(1, 5, List(List(0, 2), List(0, 4))) == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (oddmatrix(4, 2, List(List(1, 0), List(3, 1), List(2, 0), List(0, 1))) == 8) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-337/roger-bell-west/tests.json b/challenge-337/roger-bell-west/tests.json new file mode 100644 index 0000000000..87769649ef --- /dev/null +++ b/challenge-337/roger-bell-west/tests.json @@ -0,0 +1,73 @@ +{ + "ch-1" : [ + { + "function" : "smallerthancurrent", + "arguments" : [ 6, 5, 4, 8 ], + "result" : [ 2, 1, 0, 3 ] + }, + { + "arguments" : [ 7, 7, 7, 7 ], + "result" : [ 0, 0, 0, 0 ] + }, + { + "arguments" : [ 5, 4, 3, 2, 1], + "result" : [ 4, 3, 2, 1, 0] + }, + { + "arguments" : [ -1, 0, 3, -2, 1 ], + "result" : [ 1, 2, 4, 0, 3] + }, + { + "arguments" : [0, 1, 1, 2, 0 ], + "result" : [0, 2, 2, 4, 0 ] + } + ], + "ch-2" : [ + { + "function" : "oddmatrix", + "multiarg" : true, + "arguments" : [ + 2, + 3, + [ [0,1],[1,1] ] + ], + "result" : 6 + }, + { + "multiarg" : true, + "arguments" : [ + 2, + 2, + [ [1,1],[0,0] ] + ], + "result" : 0 + }, + { + "multiarg" : true, + "arguments" : [ + 3, + 3, + [ [0,0],[1,2],[2,1] ] + ], + "result" : 0 + }, + { + "multiarg" : true, + "arguments" : [ + 1, + 5, + [ [0,2],[0,4] ] + ], + "result" : 2 + }, + { + "multiarg" : true, + "arguments" : [ + 4, + 2, + [ [1,0],[3,1],[2,0],[0,1] ] + ], + "result" : 8 + } + ] +} diff --git a/challenge-337/roger-bell-west/typst/ch-1.typ b/challenge-337/roger-bell-west/typst/ch-1.typ new file mode 100644 index 0000000000..d2a22702db --- /dev/null +++ b/challenge-337/roger-bell-west/typst/ch-1.typ @@ -0,0 +1,35 @@ +#let testresult(pass) = { + if pass { + text(fill: green, "Pass") + } else { + text(fill: red, "Fail") + } +} + +#let smallerthancurrent(a) = { + let b = a.sorted() + let m = (:) + for (i, v) in b.enumerate() { + let vs = str(v) + if not vs in m { + m.insert(vs, i) + } + } + a.map(x => int(m.at(str(x)))) +} + +Test 1: + #testresult(smallerthancurrent((6, 5, 4, 8)) == (2, 1, 0, 3)) + +Test 2: + #testresult(smallerthancurrent((7, 7, 7, 7)) == (0, 0, 0, 0)) + +Test 3: + #testresult(smallerthancurrent((5, 4, 3, 2, 1)) == (4, 3, 2, 1, 0)) + +Test 4: + #testresult(smallerthancurrent((-1, 0, 3, -2, 1)) == (1, 2, 4, 0, 3)) + +Test 5: + #testresult(smallerthancurrent((0, 1, 1, 2, 0)) == (0, 2, 2, 4, 0)) + diff --git a/challenge-337/roger-bell-west/typst/ch-2.typ b/challenge-337/roger-bell-west/typst/ch-2.typ new file mode 100644 index 0000000000..9de881ec1a --- /dev/null +++ b/challenge-337/roger-bell-west/typst/ch-2.typ @@ -0,0 +1,43 @@ +#let testresult(pass) = { + if pass { + text(fill: green, "Pass") + } else { + text(fill: red, "Fail") + } +} + +#let oddmatrix(rows, cols, points) = { + let rm = (:) + let cm = (:) + for p in points { + let p0 = str(p.at(0)) + let p1 = str(p.at(1)) + if p0 in rm { + let _ = rm.remove(p0) + } else { + rm.insert(p0, true) + } + if p1 in cm { + let _ = cm.remove(p1) + } else { + cm.insert(p1, true) + } + } + rm.len() * (cols - cm.len()) + cm.len() * (rows - rm.len()) +} + +Test 1: + #testresult(oddmatrix(2, 3, ((0, 1), (1, 1))) == 6) + +Test 2: + #testresult(oddmatrix(2, 2, ((1, 1), (0, 0))) == 0) + +Test 3: + #testresult(oddmatrix(3, 3, ((0, 0), (1, 2), (2, 1))) == 0) + +Test 4: + #testresult(oddmatrix(1, 5, ((0, 2), (0, 4))) == 2) + +Test 5: + #testresult(oddmatrix(4, 2, ((1, 0), (3, 1), (2, 0), (0, 1))) == 8) + -- cgit