From 4de649256f3bad9f0331d183c7efcc7ce83b4fb0 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Tue, 20 Feb 2024 11:37:46 +0000 Subject: RogerBW solutions for challenge no. 257 --- challenge-257/roger-bell-west/javascript/ch-1.js | 69 +++++ challenge-257/roger-bell-west/javascript/ch-2.js | 125 +++++++++ challenge-257/roger-bell-west/kotlin/ch-1.kt | 40 +++ challenge-257/roger-bell-west/kotlin/ch-2.kt | 95 +++++++ challenge-257/roger-bell-west/lua/ch-1.lua | 75 ++++++ challenge-257/roger-bell-west/lua/ch-2.lua | 125 +++++++++ challenge-257/roger-bell-west/perl/ch-1.pl | 24 ++ challenge-257/roger-bell-west/perl/ch-2.pl | 53 ++++ challenge-257/roger-bell-west/postscript/ch-1.ps | 228 +++++++++++++++++ challenge-257/roger-bell-west/postscript/ch-2.ps | 313 +++++++++++++++++++++++ challenge-257/roger-bell-west/python/ch-1.py | 28 ++ challenge-257/roger-bell-west/python/ch-2.py | 57 +++++ challenge-257/roger-bell-west/raku/ch-1.p6 | 22 ++ challenge-257/roger-bell-west/raku/ch-2.p6 | 50 ++++ challenge-257/roger-bell-west/ruby/ch-1.rb | 34 +++ challenge-257/roger-bell-west/ruby/ch-2.rb | 74 ++++++ challenge-257/roger-bell-west/rust/ch-1.rs | 36 +++ challenge-257/roger-bell-west/rust/ch-2.rs | 127 +++++++++ challenge-257/roger-bell-west/scala/ch-1.scala | 41 +++ challenge-257/roger-bell-west/scala/ch-2.scala | 98 +++++++ challenge-257/roger-bell-west/tests.yaml | 163 ++++++++++++ 21 files changed, 1877 insertions(+) create mode 100755 challenge-257/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-257/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-257/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-257/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-257/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-257/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-257/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-257/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-257/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-257/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-257/roger-bell-west/python/ch-1.py create mode 100755 challenge-257/roger-bell-west/python/ch-2.py create mode 100755 challenge-257/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-257/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-257/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-257/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-257/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-257/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-257/roger-bell-west/scala/ch-1.scala create mode 100644 challenge-257/roger-bell-west/scala/ch-2.scala create mode 100644 challenge-257/roger-bell-west/tests.yaml diff --git a/challenge-257/roger-bell-west/javascript/ch-1.js b/challenge-257/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..f7d2801884 --- /dev/null +++ b/challenge-257/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,69 @@ +#! /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 smallerthancurrent(a) { + let s = [...a]; + s.sort(function(a,b) { + return a-b; + }); + let l = new Map; + s.forEach((n, i) => { + if (!l.has(n)) { + l.set(n, i); + } + }); + return a.map(n => l.get(n)); +} + +if (deepEqual(smallerthancurrent([5, 2, 1, 6]), [2, 1, 0, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(smallerthancurrent([1, 2, 0, 3]), [1, 2, 0, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(smallerthancurrent([0, 1]), [0, 1])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(smallerthancurrent([9, 4, 9, 2]), [2, 1, 2, 0])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-257/roger-bell-west/javascript/ch-2.js b/challenge-257/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..30d85505c0 --- /dev/null +++ b/challenge-257/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,125 @@ +#! /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 reducedrowechelon(a) { + let leadingone = []; + let valid = true; + for (let row of a) { + let lp = -1; + row.forEach((cell, cn) => { + if (lp == -1) { + if (cell == 1) { + lp = cn; + } else if (cell != 0) { + valid = false; + } + } + }); + leadingone.push(lp); + } + if (!valid) { + return false; + } + while (leadingone[leadingone.length-1] == -1) { + leadingone.pop(); + } + let c = [...leadingone]; + c.sort(function(a,b) { + return a-b; + }); + if (c[0] == -1) { + return false; + } + if (!deepEqual(c, leadingone)) { + return false; + } + for (let i of c) { + let col = a.map(r => r[i]); + col.sort(function(a,b) { + return a-b; + }); + if (col[col.length-1] != 1 || + col[col.length-2] != 0 || + col[0] != 0) { + return false; + } + } + return true; +} + +if (!reducedrowechelon([[1, 1, 0], [0, 1, 0], [0, 0, 0]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (reducedrowechelon([[0, 1, -2, 0, 1], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (reducedrowechelon([[1, 0, 0, 4], [0, 1, 0, 7], [0, 0, 1, -1]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!reducedrowechelon([[0, 1, -2, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!reducedrowechelon([[0, 1, 0], [0, 1, 0], [0, 0, 0]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!reducedrowechelon([[4, 0, 0, 0], [0, 1, 0, 7], [0, 0, 1, -1]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!reducedrowechelon([[1, 0, 0, 4], [1, 0, 0, 7], [0, 0, 1, -1]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!reducedrowechelon([[1, -2, 0, 4], [0, 1, 0, 7], [0, 0, 1, -1]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-257/roger-bell-west/kotlin/ch-1.kt b/challenge-257/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..09a1b389ba --- /dev/null +++ b/challenge-257/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,40 @@ +fun smallerthancurrent(a: List): List { + var s = ArrayList(a) + s.sort() + var l = mutableMapOf() + s.forEachIndexed {i, n -> + if (!l.contains(n)) { + l.put(n, i) + } + } + return a.map { l.getValue(it) }.toList() +} + +fun main() { + + if (smallerthancurrent(listOf(5, 2, 1, 6)) == listOf(2, 1, 0, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallerthancurrent(listOf(1, 2, 0, 3)) == listOf(1, 2, 0, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallerthancurrent(listOf(0, 1)) == listOf(0, 1)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallerthancurrent(listOf(9, 4, 9, 2)) == listOf(2, 1, 2, 0)) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-257/roger-bell-west/kotlin/ch-2.kt b/challenge-257/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..f5d3abccda --- /dev/null +++ b/challenge-257/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,95 @@ + +fun reducedrowechelon(a: List>): Boolean { + var leadingone = ArrayList() + var valid = true + for (row in a) { + var lp = -1 + row.forEachIndexed{cn, cell -> + if (lp == -1) { + if (cell == 1) { + lp = cn + } else if (cell != 0) { + valid = false + } + } + } + leadingone.add(lp) + if (!valid) { + return false + } + } + while (leadingone[leadingone.size-1] == -1) { + leadingone.removeLast() + } + var c = ArrayList(leadingone) + c.sort() + if (c[0] == -1) { + return false + } + if (c != leadingone) { + return false + } + for (i in c) { + var col = ArrayList(a.map { it[i] }) + col.sort() + if (col[col.size-1] != 1 || + col[col.size-2] != 0 || + col[0] != 0) { + return false + } + } + return true +} + +fun main() { + + if (!reducedrowechelon(listOf(listOf(1, 1, 0), listOf(0, 1, 0), listOf(0, 0, 0)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (reducedrowechelon(listOf(listOf(0, 1, -2, 0, 1), listOf(0, 0, 0, 1, 3), listOf(0, 0, 0, 0, 0), listOf(0, 0, 0, 0, 0)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (reducedrowechelon(listOf(listOf(1, 0, 0, 4), listOf(0, 1, 0, 7), listOf(0, 0, 1, -1)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!reducedrowechelon(listOf(listOf(0, 1, -2, 0, 1), listOf(0, 0, 0, 0, 0), listOf(0, 0, 0, 1, 3), listOf(0, 0, 0, 0, 0)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!reducedrowechelon(listOf(listOf(0, 1, 0), listOf(0, 1, 0), listOf(0, 0, 0)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!reducedrowechelon(listOf(listOf(4, 0, 0, 0), listOf(0, 1, 0, 7), listOf(0, 0, 1, -1)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!reducedrowechelon(listOf(listOf(1, 0, 0, 4), listOf(1, 0, 0, 7), listOf(0, 0, 1, -1)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!reducedrowechelon(listOf(listOf(1, -2, 0, 4), listOf(0, 1, 0, 7), listOf(0, 0, 1, -1)))) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-257/roger-bell-west/lua/ch-1.lua b/challenge-257/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..fbd0bc40b4 --- /dev/null +++ b/challenge-257/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,75 @@ +#! /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 smallerthancurrent(a) + local s = a + table.sort(s, function (i, j) return i < j end) + local l = {} + for i, n in ipairs(s) do + if l[n] == nil then + l[n] = i + end + end + local out = {} + for _, n in ipairs(a) do + table.insert(out, l[n]) + end + return out +end + +if recursive_compare(smallerthancurrent({5, 2, 1, 6}), {2, 1, 0, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(smallerthancurrent({1, 2, 0, 3}), {1, 2, 0, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(smallerthancurrent({0, 1}), {0, 1}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(smallerthancurrent({9, 4, 9, 2}), {2, 1, 2, 0}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-257/roger-bell-west/lua/ch-2.lua b/challenge-257/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..436dc433e3 --- /dev/null +++ b/challenge-257/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,125 @@ +#! /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 reducedrowechelon(a) + local leadingone = {} + for _, row in ipairs(a) do + local lp = -1 + for cn, cell in ipairs(row) do + if cell == 1 then + lp = cn - 1 + break + elseif cell ~= 0 then + return false + end + end + table.insert(leadingone, lp) + end + while leadingone[#leadingone] == -1 do + table.remove(leadingone) + end + local c = leadingone + table.sort(c, function (i, j) return i < j end) + if c[1] == -1 then + return false + end + if not recursive_compare(c, leadingone) then + return false + end + for _, i in ipairs(c) do + local col = {} + for _b, row in ipairs(a) do + table.insert(col, row[i + 1]) + end + table.sort(col, function (i, j) return i < j end) + if col[#col] ~= 1 or col[#col - 1] ~= 0 or col[1] ~= 0 then + return false + end + end + return true +end + +if not reducedrowechelon({{1, 1, 0}, {0, 1, 0}, {0, 0, 0}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if reducedrowechelon({{0, 1, -2, 0, 1}, {0, 0, 0, 1, 3}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if reducedrowechelon({{1, 0, 0, 4}, {0, 1, 0, 7}, {0, 0, 1, -1}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not reducedrowechelon({{0, 1, -2, 0, 1}, {0, 0, 0, 0, 0}, {0, 0, 0, 1, 3}, {0, 0, 0, 0, 0}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not reducedrowechelon({{0, 1, 0}, {0, 1, 0}, {0, 0, 0}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not reducedrowechelon({{4, 0, 0, 0}, {0, 1, 0, 7}, {0, 0, 1, -1}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not reducedrowechelon({{1, 0, 0, 4}, {1, 0, 0, 7}, {0, 0, 1, -1}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not reducedrowechelon({{1, -2, 0, 4}, {0, 1, 0, 7}, {0, 0, 1, -1}}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-257/roger-bell-west/perl/ch-1.pl b/challenge-257/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..3ebebbc636 --- /dev/null +++ b/challenge-257/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 => 4; + +is_deeply(smallerthancurrent([5, 2, 1, 6]), [2, 1, 0, 3], 'example 1'); +is_deeply(smallerthancurrent([1, 2, 0, 3]), [1, 2, 0, 3], 'example 2'); +is_deeply(smallerthancurrent([0, 1]), [0, 1], 'example 3'); +is_deeply(smallerthancurrent([9, 4, 9, 2]), [2, 1, 2, 0], 'example 4'); + +sub smallerthancurrent($a) { + my %l; + my @s = sort {$::a <=> $::b} @{$a}; + foreach my $i (0..$#s) { + my $n = $s[$i]; + unless (exists $l{$n}) { + $l{$n} = $i; + } + } + return [map {$l{$_}} @{$a}]; +} diff --git a/challenge-257/roger-bell-west/perl/ch-2.pl b/challenge-257/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..43a4621110 --- /dev/null +++ b/challenge-257/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,53 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 8; +use Test::Deep::NoTest; + +is(reducedrowechelon([[1, 1, 0], [0, 1, 0], [0, 0, 0]]), 0, 'example 1'); +is(reducedrowechelon([[0, 1, -2, 0, 1], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]), 1, 'example 2'); +is(reducedrowechelon([[1, 0, 0, 4], [0, 1, 0, 7], [0, 0, 1, -1]]), 1, 'example 3'); +is(reducedrowechelon([[0, 1, -2, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0]]), 0, 'example 4'); +is(reducedrowechelon([[0, 1, 0], [0, 1, 0], [0, 0, 0]]), 0, 'example 5'); +is(reducedrowechelon([[4, 0, 0, 0], [0, 1, 0, 7], [0, 0, 1, -1]]), 0, 'example 6'); +is(reducedrowechelon([[1, 0, 0, 4], [1, 0, 0, 7], [0, 0, 1, -1]]), 0, 'example 7'); +is(reducedrowechelon([[1, -2, 0, 4], [0, 1, 0, 7], [0, 0, 1, -1]]), 0, 'example 8'); + +sub reducedrowechelon($a) { + my @leadingone; + foreach my $row (@{$a}) { + my $lp = -1; + foreach my $cn (0 .. $#{$row}) { + my $cell = $row->[$cn]; + if ($cell == 1) { + $lp = $cn; + last; + } elsif ($cell != 0) { + return 0; + } + } + push @leadingone, $lp; + } + while ($leadingone[-1] == -1) { + pop @leadingone; + } + my @c = sort {$::a <=> $::b} @leadingone; + if ($c[0] == -1) { + return 0; + } + if (!eq_deeply(\@c, \@leadingone)) { + return 0; + } + foreach my $i (@c) { + my @col = sort {$::a <=> $::b} map {$_->[$i]} @{$a}; + if ($col[-1] != 1 || + $col[-2] != 0 || + $col[0] != 0) { + return 0; + } + } + return 1; +} diff --git a/challenge-257/roger-bell-west/postscript/ch-1.ps b/challenge-257/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..edaebf28b0 --- /dev/null +++ b/challenge-257/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,228 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/quicksort.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} 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 + +/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 + +/enumerate.array { + 1 dict begin + /a exch def + [ + 0 1 a length 1 sub { + [ exch dup a exch get ] + } for + ] + 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 + +/quicksort { + { quicksort.cmp } quicksort.with_comparator +} 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.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 + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} 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 + +/map { % array proc -> array + 2 dict begin + /p exch def + [ exch + { + p + } forall + ] + end +} 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 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 + + +% end included library code + +/smallerthancurrent { + 0 dict begin + dup + /l 0 dict def + [ exch aload pop ] quicksort enumerate.array { + aload pop + /n exch def + /i exch def + l n known not { + l n i put + } if + } forall + { l exch get } map + end +} bind def + +(smallerthancurrent) test.start +[5 2 1 6] smallerthancurrent [2 1 0 3] deepeq test +[1 2 0 3] smallerthancurrent [1 2 0 3] deepeq test +[0 1] smallerthancurrent [0 1] deepeq test +[9 4 9 2] smallerthancurrent [2 1 2 0] deepeq test +test.end diff --git a/challenge-257/roger-bell-west/postscript/ch-2.ps b/challenge-257/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..ea6ed2bf63 --- /dev/null +++ b/challenge-257/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,313 @@ +%!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 + +/map { % array proc -> array + 2 dict begin + /p exch def + [ exch + { + p + } forall + ] + end +} 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 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 + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/quicksort { + { quicksort.cmp } quicksort.with_comparator +} 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 + +/apop.right { % [a b c] -> [a b] c + [ exch aload length 1 add 1 roll ] exch +} 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 + +/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 + +/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 + +/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 + +/quicksort.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} bind def + + +% end included library code + +/reducedrowechelon { + 0 dict begin + /a exch def + /valid true def + /leadingone [ + a { + /lp -1 def + enumerate.array { + aload pop + /cell exch def + /cn exch def + cell 1 eq { + /lp cn def + exit + } { + cell 0 ne { + /valid false def + exit + } if + } ifelse + } forall + lp + } forall + ] def + valid { + { + leadingone leadingone length 1 sub get -1 eq { + /leadingone leadingone apop.right pop def + } { + exit + } ifelse + } loop + /c leadingone deepcopy quicksort def + c 0 get -1 eq { + /valid false def + } if + c leadingone deepeq not { + /valid false def + } if + } if + valid { + c { + /i exch def + /col a { i get } map quicksort def + col col length 1 sub get 1 ne + col col length 2 sub get 0 ne + col 0 get 0 ne or or { + /valid false def + exit + } if + } forall + } if + valid + end +} bind def + +(reducedrowechelon) test.start +[[1 1 0] [0 1 0] [0 0 0]] reducedrowechelon not test +[[0 1 -2 0 1] [0 0 0 1 3] [0 0 0 0 0] [0 0 0 0 0]] reducedrowechelon test +[[1 0 0 4] [0 1 0 7] [0 0 1 -1]] reducedrowechelon test +[[0 1 -2 0 1] [0 0 0 0 0] [0 0 0 1 3] [0 0 0 0 0]] reducedrowechelon not test +[[0 1 0] [0 1 0] [0 0 0]] reducedrowechelon not test +[[4 0 0 0] [0 1 0 7] [0 0 1 -1]] reducedrowechelon not test +[[1 0 0 4] [1 0 0 7] [0 0 1 -1]] reducedrowechelon not test +[[1 -2 0 4] [0 1 0 7] [0 0 1 -1]] reducedrowechelon not test +test.end diff --git a/challenge-257/roger-bell-west/python/ch-1.py b/challenge-257/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..e2b9e4cc90 --- /dev/null +++ b/challenge-257/roger-bell-west/python/ch-1.py @@ -0,0 +1,28 @@ +#! /usr/bin/python3 + +def smallerthancurrent(a): + s = a.copy() + s.sort() + l = dict() + for i, n in enumerate(s): + if n not in l: + l[n] = i + return [l[n] for n in a] + +import unittest + +class TestSmallerthancurrent(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(smallerthancurrent([5, 2, 1, 6]), [2, 1, 0, 3], 'example 1') + + def test_ex2(self): + self.assertEqual(smallerthancurrent([1, 2, 0, 3]), [1, 2, 0, 3], 'example 2') + + def test_ex3(self): + self.assertEqual(smallerthancurrent([0, 1]), [0, 1], 'example 3') + + def test_ex4(self): + self.assertEqual(smallerthancurrent([9, 4, 9, 2]), [2, 1, 2, 0], 'example 4') + +unittest.main() diff --git a/challenge-257/roger-bell-west/python/ch-2.py b/challenge-257/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..1088ca4d4b --- /dev/null +++ b/challenge-257/roger-bell-west/python/ch-2.py @@ -0,0 +1,57 @@ +#! /usr/bin/python3 + +def reducedrowechelon(a): + leadingone = [] + for row in a: + lp = -1 + for cn, cell in enumerate(row): + if cell == 1: + lp = cn + break + elif cell != 0: + return False + leadingone.append(lp) + while leadingone[-1] == -1: + leadingone.pop() + c = leadingone.copy() + c.sort() + if c[0] == -1: + return False + if c != leadingone: + return False + for i in c: + col = [r[i] for r in a] + col.sort() + if col[-1] != 1 or col[-2] != 0 or col[0] != 0: + return False + return True + +import unittest + +class TestReducedrowechelon(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(reducedrowechelon([[1, 1, 0], [0, 1, 0], [0, 0, 0]]), False, 'example 1') + + def test_ex2(self): + self.assertEqual(reducedrowechelon([[0, 1, -2, 0, 1], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]), True, 'example 2') + + def test_ex3(self): + self.assertEqual(reducedrowechelon([[1, 0, 0, 4], [0, 1, 0, 7], [0, 0, 1, -1]]), True, 'example 3') + + def test_ex4(self): + self.assertEqual(reducedrowechelon([[0, 1, -2, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0]]), False, 'example 4') + + def test_ex5(self): + self.assertEqual(reducedrowechelon([[0, 1, 0], [0, 1, 0], [0, 0, 0]]), False, 'example 5') + + def test_ex6(self): + self.assertEqual(reducedrowechelon([[4, 0, 0, 0], [0, 1, 0, 7], [0, 0, 1, -1]]), False, 'example 6') + + def test_ex7(self): + self.assertEqual(reducedrowechelon([[1, 0, 0, 4], [1, 0, 0, 7], [0, 0, 1, -1]]), False, 'example 7') + + def test_ex8(self): + self.assertEqual(reducedrowechelon([[1, -2, 0, 4], [0, 1, 0, 7], [0, 0, 1, -1]]), False, 'example 8') + +unittest.main() diff --git a/challenge-257/roger-bell-west/raku/ch-1.p6 b/challenge-257/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..9c310f6469 --- /dev/null +++ b/challenge-257/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,22 @@ +#! /usr/bin/raku + +use Test; + +plan 4; + +is-deeply(smallerthancurrent([5, 2, 1, 6]), [2, 1, 0, 3], 'example 1'); +is-deeply(smallerthancurrent([1, 2, 0, 3]), [1, 2, 0, 3], 'example 2'); +is-deeply(smallerthancurrent([0, 1]), [0, 1], 'example 3'); +is-deeply(smallerthancurrent([9, 4, 9, 2]), [2, 1, 2, 0], 'example 4'); + +sub smallerthancurrent(@a) { + my %l; + my @s = @a.sort({$^a <=> $^b}); + for (0..@s.end) -> $i { + my $n = @s[$i]; + unless (%l{$n}:exists) { + %l{$n} = $i; + } + } + return Array(@a.map({%l{$_}})); +} diff --git a/challenge-257/roger-bell-west/raku/ch-2.p6 b/challenge-257/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..1340c5316d --- /dev/null +++ b/challenge-257/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,50 @@ +#! /usr/bin/raku + +use Test; + +plan 8; + +is(reducedrowechelon([[1, 1, 0], [0, 1, 0], [0, 0, 0]]), False, 'example 1'); +is(reducedrowechelon([[0, 1, -2, 0, 1], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]), True, 'example 2'); +is(reducedrowechelon([[1, 0, 0, 4], [0, 1, 0, 7], [0, 0, 1, -1]]), True, 'example 3'); +is(reducedrowechelon([[0, 1, -2, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0]]), False, 'example 4'); +is(reducedrowechelon([[0, 1, 0], [0, 1, 0], [0, 0, 0]]), False, 'example 5'); +is(reducedrowechelon([[4, 0, 0, 0], [0, 1, 0, 7], [0, 0, 1, -1]]), False, 'example 6'); +is(reducedrowechelon([[1, 0, 0, 4], [1, 0, 0, 7], [0, 0, 1, -1]]), False, 'example 7'); +is(reducedrowechelon([[1, -2, 0, 4], [0, 1, 0, 7], [0, 0, 1, -1]]), False, 'example 8'); + +sub reducedrowechelon(@a) { + my @leadingone; + for @a -> @row { + my $lp = -1; + for 0 .. @row.end -> $cn { + my $cell = @row[$cn]; + if ($cell == 1) { + $lp = $cn; + last; + } elsif ($cell != 0) { + return False; + } + } + @leadingone.push($lp); + } + while (@leadingone[*-1] == -1) { + @leadingone.pop; + } + my @c = @leadingone.sort({$^a <=> $^b}); + if (@c[0] == -1) { + return False; + } + if (!(@c eqv @leadingone)) { + return False; + } + for @c -> $i { + my @col = @a.map({$_[$i]}).sort({$^a <=> $^b}); + if (@col[*-1] != 1 || + @col[*-2] != 0 || + @col[0] != 0) { + return False; + } + } + return True; +} diff --git a/challenge-257/roger-bell-west/ruby/ch-1.rb b/challenge-257/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..88244b1d85 --- /dev/null +++ b/challenge-257/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,34 @@ +#! /usr/bin/ruby + +def smallerthancurrent(a) + s = a.sort + l = Hash.new + s.each_with_index do |n, i| + if !l.has_key?(n) then + l[n] = i + end + end + return a.collect { |n| l[n] } +end + +require 'test/unit' + +class TestSmallerthancurrent < Test::Unit::TestCase + + def test_ex1 + assert_equal([2, 1, 0, 3], smallerthancurrent([5, 2, 1, 6])) + end + + def test_ex2 + assert_equal([1, 2, 0, 3], smallerthancurrent([1, 2, 0, 3])) + end + + def test_ex3 + assert_equal([0, 1], smallerthancurrent([0, 1])) + end + + def test_ex4 + assert_equal([2, 1, 2, 0], smallerthancurrent([9, 4, 9, 2])) + end + +end diff --git a/challenge-257/roger-bell-west/ruby/ch-2.rb b/challenge-257/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..4ed711ab31 --- /dev/null +++ b/challenge-257/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,74 @@ +#! /usr/bin/ruby + +def reducedrowechelon(a) + leadingone = [] + a.each do |row| + lp = -1 + row.each_with_index do |cell, cn| + if cell == 1 then + lp = cn + break + elsif cell != 0 then + return false + end + end + leadingone.push(lp) + end + while leadingone[-1] == -1 do + leadingone.pop + end + c = leadingone.sort + if c[0] == -1 then + return false + end + if c != leadingone then + return false + end + c.each do |i| + col = a.map { |r| r[i] }.sort + if col[-1] != 1 || + col[-2] != 0 || + col[0] != 0 then + return false + end + end + return true +end + +require 'test/unit' + +class TestReducedrowechelon < Test::Unit::TestCase + + def test_ex1 + assert_equal(false, reducedrowechelon([[1, 1, 0], [0, 1, 0], [0, 0, 0]])) + end + + def test_ex2 + assert_equal(true, reducedrowechelon([[0, 1, -2, 0, 1], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])) + end + + def test_ex3 + assert_equal(true, reducedrowechelon([[1, 0, 0, 4], [0, 1, 0, 7], [0, 0, 1, -1]])) + end + + def test_ex4 + assert_equal(false, reducedrowechelon([[0, 1, -2, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0]])) + end + + def test_ex5 + assert_equal(false, reducedrowechelon([[0, 1, 0], [0, 1, 0], [0, 0, 0]])) + end + + def test_ex6 + assert_equal(false, reducedrowechelon([[4, 0, 0, 0], [0, 1, 0, 7], [0, 0, 1, -1]])) + end + + def test_ex7 + assert_equal(false, reducedrowechelon([[1, 0, 0, 4], [1, 0, 0, 7], [0, 0, 1, -1]])) + end + + def test_ex8 + assert_equal(false, reducedrowechelon([[1, -2, 0, 4], [0, 1, 0, 7], [0, 0, 1, -1]])) + end + +end diff --git a/challenge-257/roger-bell-west/rust/ch-1.rs b/challenge-257/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..e112e53a90 --- /dev/null +++ b/challenge-257/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,36 @@ +#! /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![5, 2, 1, 6]), vec![2, 1, 0, 3]); +} + +#[test] +fn test_ex2() { + assert_eq!(smallerthancurrent(vec![1, 2, 0, 3]), vec![1, 2, 0, 3]); +} + +#[test] +fn test_ex3() { + assert_eq!(smallerthancurrent(vec![0, 1]), vec![0, 1]); +} + +#[test] +fn test_ex4() { + assert_eq!(smallerthancurrent(vec![9, 4, 9, 2]), vec![2, 1, 2, 0]); +} + +use std::collections::HashMap; + +fn smallerthancurrent(a: Vec) -> Vec { + let mut s = a.clone(); + s.sort(); + let mut l: HashMap = HashMap::new(); + for (i, n) in s.iter().enumerate() { + if !l.contains_key(&n) { + l.insert(*n, i as u32); + } + } + a.into_iter().map(|n| l.get(&n).unwrap()).copied().collect::>() +} diff --git a/challenge-257/roger-bell-west/rust/ch-2.rs b/challenge-257/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..aefc67cee1 --- /dev/null +++ b/challenge-257/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,127 @@ +#! /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!( + reducedrowechelon(vec![vec![1, 1, 0], vec![0, 1, 0], vec![0, 0, 0]]), + false + ); +} + +#[test] +fn test_ex2() { + assert_eq!( + reducedrowechelon(vec![ + vec![0, 1, -2, 0, 1], + vec![0, 0, 0, 1, 3], + vec![0, 0, 0, 0, 0], + vec![0, 0, 0, 0, 0] + ]), + true + ); +} + +#[test] +fn test_ex3() { + assert_eq!( + reducedrowechelon(vec![ + vec![1, 0, 0, 4], + vec![0, 1, 0, 7], + vec![0, 0, 1, -1] + ]), + true + ); +} + +#[test] +fn test_ex4() { + assert_eq!( + reducedrowechelon(vec![ + vec![0, 1, -2, 0, 1], + vec![0, 0, 0, 0, 0], + vec![0, 0, 0, 1, 3], + vec![0, 0, 0, 0, 0] + ]), + false + ); +} + +#[test] +fn test_ex5() { + assert_eq!( + reducedrowechelon(vec![vec![0, 1, 0], vec![1, 0, 0], vec![0, 0, 0]]), + false + ); +} + +#[test] +fn test_ex6() { + assert_eq!( + reducedrowechelon(vec![ + vec![4, 0, 0, 0], + vec![0, 1, 0, 7], + vec![0, 0, 1, -1] + ]), + false + ); +} + +#[test] +fn test_ex7() { + assert_eq!( + reducedrowechelon(vec![ + vec![1, 0, 0, 4], + vec![1, 0, 0, 7], + vec![0, 0, 1, -1] + ]), + false + ); +} + +#[test] +fn test_ex8() { + assert_eq!( + reducedrowechelon(vec![ + vec![1, -2, 0, 4], + vec![0, 1, 0, 7], + vec![0, 0, 1, -1] + ]), + false + ); +} + +fn reducedrowechelon(a: Vec>) -> bool { + let mut leadingone = Vec::new(); + for row in a.iter() { + let mut lp = -1; + for (cn, &cell) in row.iter().enumerate() { + if cell == 1 { + lp = cn as i32; + break; + } else if cell != 0 { + return false; + } + } + leadingone.push(lp); + } + while leadingone[leadingone.len() - 1] == -1 { + leadingone.pop(); + } + let mut c = leadingone.clone(); + c.sort(); + if c[0] == -1 { + return false; + } + if c != leadingone { + return false; + } + for i in c { + let mut col = a.iter().map(|r| r[i as usize]).collect::>(); + col.sort(); + if col[col.len() - 1] != 1 || col[col.len() - 2] != 0 || col[0] != 0 { + return false; + } + } + true +} diff --git a/challenge-257/roger-bell-west/scala/ch-1.scala b/challenge-257/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..faac1a46f3 --- /dev/null +++ b/challenge-257/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,41 @@ +import scala.collection.mutable + +object Smallerthancurrent { + def smallerthancurrent(a: List[Int]): List[Int] = { + val s: List[Int] = a.sortWith(_ < _) + var l = mutable.Map.empty[Int, Int] + for ((n, i) <- s.zipWithIndex) { + if (!l.contains(n)) { + l += (n -> i) + } + } + a.map { n => l(n) }.toList + } + def main(args: Array[String]) { + if (smallerthancurrent(List(5, 2, 1, 6)) == List(2, 1, 0, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallerthancurrent(List(1, 2, 0, 3)) == List(1, 2, 0, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallerthancurrent(List(0, 1)) == List(0, 1)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallerthancurrent(List(9, 4, 9, 2)) == List(2, 1, 2, 0)) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-257/roger-bell-west/scala/ch-2.scala b/challenge-257/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..b12aca0a8a --- /dev/null +++ b/challenge-257/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,98 @@ +import scala.collection.mutable.ListBuffer + +object Reducedrowechelon { + def reducedrowechelon(a: List[List[Int]]): Boolean = { + var leadingone = new ListBuffer[Int]() + var valid = true + for (row <- a) { + var lp = -1 + for ((cell, cn) <- row.zipWithIndex) { + if (lp == -1) { + if (cell == 1) { + lp = cn + } else if (cell != 0) { + valid = false + } + } + } + leadingone += lp + } + var c = new ListBuffer[Int] + if (valid) { + while (leadingone(leadingone.size-1) == -1) { + leadingone.remove(leadingone.size-1) + } + c = leadingone.to[ListBuffer] + c = c.sortWith(_ < _) + if (c(0) == -1) { + valid = false + } + if (c != leadingone) { + valid = false + } + } + if (valid) { + for (i <- c) { + var col = a.map(n => n(i)).to[ListBuffer] + col = col.sortWith(_ < _) + if (col(col.size-1) != 1 || + col(col.size-2) != 0|| + col(0) != 0) { + valid = false + } + } + } + valid + } + def main(args: Array[String]) { + if (!reducedrowechelon(List(List(1, 1, 0), List(0, 1, 0), List(0, 0, 0)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (reducedrowechelon(List(List(0, 1, -2, 0, 1), List(0, 0, 0, 1, 3), List(0, 0, 0, 0, 0), List(0, 0, 0, 0, 0)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (reducedrowechelon(List(List(1, 0, 0, 4), List(0, 1, 0, 7), List(0, 0, 1, -1)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!reducedrowechelon(List(List(0, 1, -2, 0, 1), List(0, 0, 0, 0, 0), List(0, 0, 0, 1, 3), List(0, 0, 0, 0, 0)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!reducedrowechelon(List(List(0, 1, 0), List(0, 1, 0), List(0, 0, 0)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!reducedrowechelon(List(List(4, 0, 0, 0), List(0, 1, 0, 7), List(0, 0, 1, -1)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!reducedrowechelon(List(List(1, 0, 0, 4), List(1, 0, 0, 7), List(0, 0, 1, -1)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!reducedrowechelon(List(List(1, -2, 0, 4), List(0, 1, 0, 7), List(0, 0, 1, -1)))) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-257/roger-bell-west/tests.yaml b/challenge-257/roger-bell-west/tests.yaml new file mode 100644 index 0000000000..6192fb49d2 --- /dev/null +++ b/challenge-257/roger-bell-west/tests.yaml @@ -0,0 +1,163 @@ +--- +ch-1: + - function: smallerthancurrent + arguments: + - 5 + - 2 + - 1 + - 6 + result: + - 2 + - 1 + - 0 + - 3 + - arguments: + - 1 + - 2 + - 0 + - 3 + result: + - 1 + - 2 + - 0 + - 3 + - arguments: + - 0 + - 1 + result: + - 0 + - 1 + - arguments: + - 9 + - 4 + - 9 + - 2 + result: + - 2 + - 1 + - 2 + - 0 +ch-2: + - function: reducedrowechelon + arguments: + - - 1 + - 1 + - 0 + - - 0 + - 1 + - 0 + - - 0 + - 0 + - 0 + result: false + - arguments: + - - 0 + - 1 + - -2 + - 0 + - 1 + - - 0 + - 0 + - 0 + - 1 + - 3 + - - 0 + - 0 + - 0 + - 0 + - 0 + - - 0 + - 0 + - 0 + - 0 + - 0 + result: true + - arguments: + - - 1 + - 0 + - 0 + - 4 + - - 0 + - 1 + - 0 + - 7 + - - 0 + - 0 + - 1 + - -1 + result: true + - arguments: + - - 0 + - 1 + - -2 + - 0 + - 1 + - - 0 + - 0 + - 0 + - 0 + - 0 + - - 0 + - 0 + - 0 + - 1 + - 3 + - - 0 + - 0 + - 0 + - 0 + - 0 + result: false + - arguments: + - - 0 + - 1 + - 0 + - - 0 + - 1 + - 0 + - - 0 + - 0 + - 0 + result: false + - arguments: + - - 4 + - 0 + - 0 + - 0 + - - 0 + - 1 + - 0 + - 7 + - - 0 + - 0 + - 1 + - -1 + result: false + - arguments: + - - 1 + - 0 + - 0 + - 4 + - - 1 + - 0 + - 0 + - 7 + - - 0 + - 0 + - 1 + - -1 + result: false + - arguments: + - - 1 + - -2 + - 0 + - 4 + - - 0 + - 1 + - 0 + - 7 + - - 0 + - 0 + - 1 + - -1 + result: false -- cgit