diff options
21 files changed, 1187 insertions, 0 deletions
diff --git a/challenge-266/roger-bell-west/javascript/ch-1.js b/challenge-266/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..67635d2e7e --- /dev/null +++ b/challenge-266/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,78 @@ +#! /usr/bin/node + +"use strict" + +function getlistset(a) { + let la = a.split(" "); + let ca = new Map; + for (let w of la) { + if (ca.has(w)) { + ca.set(w, ca.get(w) + 1); + } else { + ca.set(w, 1); + } + } + la = la.filter(w => ca.get(w) == 1); + const cb = new Set(ca.keys()); + return [la, cb]; +} + +function uncommonwords(a, b) { + const [la, sa] = getlistset(a); + const [lb, sb] = getlistset(b); + let out = []; + for (let [wl, t] of [[la, sb], [lb, sa]]) { + for (let w of wl) { + if (!t.has(w)) { + out.push(w); + } + } + } + return out; +} + +// 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(uncommonwords('Mango is sweet', 'Mango is sour'), ['sweet', 'sour'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(uncommonwords('Mango Mango', 'Orange'), ['Orange'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(uncommonwords('Mango is Mango', 'Orange is Orange'), [])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-266/roger-bell-west/javascript/ch-2.js b/challenge-266/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..bb6a4a4f43 --- /dev/null +++ b/challenge-266/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,41 @@ +#! /usr/bin/node + +"use strict" + +function xmatrix(a) { + const order = a.length - 1; + let valid = true; + a.forEach((row, y) => { + row.forEach((value, x) => { + if (x == y || x == order - y) { + if (value == 0) { + valid = false; + } + } else { + if (value != 0) { + valid = false; + } + } + }) + }); + return valid; +} + +if (xmatrix([[1, 0, 0, 2], [0, 3, 4, 0], [0, 5, 6, 0], [7, 0, 0, 1]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!xmatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (xmatrix([[1, 0, 2], [0, 3, 0], [4, 0, 5]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-266/roger-bell-west/kotlin/ch-1.kt b/challenge-266/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..f80e212dad --- /dev/null +++ b/challenge-266/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,51 @@ +fun getlistset(a: String): Pair<List<String>, Set<String>> { + var la = a.split(" ").toList() + var ca = mutableMapOf<String, Int>().withDefault({0}) + for (w in la) { + ca.set(w, ca.getValue(w) + 1) + } + la = la.filter{ca.getValue(it) == 1}.toList() + val cb = ca.keys.toSet(); + return Pair(la, cb) +} + +fun uncommonwords(a: String, b: String): List<String> { + val xa = getlistset(a) + val la = xa.first + val sa = xa.second + val xb = getlistset(b) + val lb = xb.first + val sb = xb.second + var out = ArrayList<String>() + for (x in listOf(Pair(la, sb), Pair(lb, sa))) { + for (w in x.first) { + if (!x.second.contains(w)) { + out.add(w) + } + } + } + return out.toList() +} + +fun main() { + + if (uncommonwords("Mango is sweet", "Mango is sour") == listOf("sweet", "sour")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (uncommonwords("Mango Mango", "Orange") == listOf("Orange")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (uncommonwords("Mango is Mango", "Orange is Orange") == emptyList<String>()) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-266/roger-bell-west/kotlin/ch-2.kt b/challenge-266/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..c8e4900dc8 --- /dev/null +++ b/challenge-266/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,41 @@ +fun xmatrix(a: List<List<Int>>): Boolean { + val order = a.size - 1 + var valid = true + a.forEachIndexed{y, row -> + row.forEachIndexed{x, value -> + if (x == y || x == order - y) { + if (value == 0) { + valid = false; + } + } else { + if (value != 0) { + valid = false; + } + } + } + } + return valid +} + +fun main() { + + if (xmatrix(listOf(listOf(1, 0, 0, 2), listOf(0, 3, 4, 0), listOf(0, 5, 6, 0), listOf(7, 0, 0, 1)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!xmatrix(listOf(listOf(1, 2, 3), listOf(4, 5, 6), listOf(7, 8, 9)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (xmatrix(listOf(listOf(1, 0, 2), listOf(0, 3, 0), listOf(4, 0, 5)))) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-266/roger-bell-west/lua/ch-1.lua b/challenge-266/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..c681f835bd --- /dev/null +++ b/challenge-266/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,103 @@ +#! /usr/bin/lua + +-- bart at https://stackoverflow.com/questions/1426954/split-string-in-lua +function split(inputstr, sep) + sep=sep or '%s' + local t={} + for field,s in string.gmatch(inputstr, "([^"..sep.."]*)("..sep.."?)") do + table.insert(t,field) + if s=="" then + return t + end + end +end + +function getlistset(a) + local la = split(a) + local ca = {} + for _, w in ipairs(la) do + if ca[w] == nil then + ca[w] = 1 + else + ca[w] = ca[w] + 1 + end + end + local lb = {} + for _, w in ipairs(la) do + if ca[w] == 1 then + table.insert(lb, w) + end + end + return {lb, ca} +end + +function uncommonwords(a, b) + local ta = getlistset(a) + local la = ta[1] + local sa = ta[2] + local tb = getlistset(b) + local lb = tb[1] + local sb = tb[2] + local out = {} + for _, wx in pairs({{la,sb}, {lb, sa}}) do + local wl = wx[1] + local t = wx[2] + for _, w in ipairs(wl) do + if t[w] == nil then + table.insert(out, w) + end + end + 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(uncommonwords("Mango is sweet", "Mango is sour"), {"sweet", "sour"}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(uncommonwords("Mango Mango", "Orange"), {"Orange"}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(uncommonwords("Mango is Mango", "Orange is Orange"), {}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-266/roger-bell-west/lua/ch-2.lua b/challenge-266/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..c16415bb87 --- /dev/null +++ b/challenge-266/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,48 @@ +#! /usr/bin/lua + +function xmatrix(a) + local order = #a + 1 + local valid = true + for y, row in ipairs(a) do + for x, value in ipairs(row) do + if x == y or x == order - y then + if value == 0 then + valid = false + end + else + if value ~= 0 then + valid = false + end + end + if not valid then + break + end + end + if not valid then + break + end + end + return valid +end + +if xmatrix({{1, 0, 0, 2}, {0, 3, 4, 0}, {0, 5, 6, 0}, {7, 0, 0, 1}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not xmatrix({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if xmatrix({{1, 0, 2}, {0, 3, 0}, {4, 0, 5}}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-266/roger-bell-west/perl/ch-1.pl b/challenge-266/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..7b0693de18 --- /dev/null +++ b/challenge-266/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,34 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is_deeply(uncommonwords('Mango is sweet', 'Mango is sour'), ['sweet', 'sour'], 'example 1'); +is_deeply(uncommonwords('Mango Mango', 'Orange'), ['Orange'], 'example 2'); +is_deeply(uncommonwords('Mango is Mango', 'Orange is Orange'), [], 'example 3'); + +sub getlistset($a) { + my @la = split ' ', $a; + my %ca; + map {$ca{$_}++} @la; + @la = grep {$ca{$_} == 1} @la; + return (\@la, \%ca); +} + +sub uncommonwords($a, $b) { + my ($la, $sa) = getlistset($a); + my ($lb, $sb) = getlistset($b); + my @out; + foreach my $r ([$la, $sb], [$lb, $sa]) { + my ($wl, $t) = @{$r}; + foreach my $w (@{$wl}) { + unless (exists $t->{$w}) { + push @out, $w; + } + } + } + return \@out; +} diff --git a/challenge-266/roger-bell-west/perl/ch-2.pl b/challenge-266/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..2e1d3d0f93 --- /dev/null +++ b/challenge-266/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,36 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(xmatrix([[1, 0, 0, 2], [0, 3, 4, 0], [0, 5, 6, 0], [7, 0, 0, 1]]), 1, 'example 1'); +is(xmatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), 0, 'example 2'); +is(xmatrix([[1, 0, 2], [0, 3, 0], [4, 0, 5]]), 1, 'example 3'); + +sub xmatrix($a) { + my $order = scalar @{$a} - 1; + my $valid = 1; + foreach my $y (0 .. $#{$a}) { + foreach my $x (0 .. $#{$a->[$y]}) { + if ($x == $y || $x == $order - $y) { + if ($a->[$y][$x] == 0) { + $valid = 0; + } + } else { + if ($a->[$y][$x] != 0) { + $valid = 0; + } + } + unless ($valid) { + last; + } + } + unless ($valid) { + last; + } + } + return $valid; +} diff --git a/challenge-266/roger-bell-west/postscript/ch-1.ps b/challenge-266/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..64eee1fb16 --- /dev/null +++ b/challenge-266/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,187 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/strsplit % (ajbjc) (j) -> [ (a) (b) (c) ] +{ + 1 dict begin + /sep exch def + [ exch + { + dup length 0 eq { + pop + exit + } { + sep search { + exch pop + dup length 0 eq { + pop + } { + exch + } ifelse + } { + () + } ifelse + } ifelse + } loop + ] + 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 + +/dget { + 3 1 roll + 2 copy + known { + get exch pop + } { + pop pop + } ifelse +} bind def + +/filter { % array proc(bool) -> array + 1 dict begin + /p exch def + [ exch + { + dup p not + { + pop + } if + } forall + ] + 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 + +/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 + +/getlistset { + 0 dict begin + ( ) strsplit /la exch def + /ca 0 dict def + la { + /k exch def + ca k 2 copy 0 dget 1 add put + } forall + /la la { ca exch get 1 eq } filter def + la ca + end +} bind def + +/uncommonwords { + 0 dict begin + getlistset + /sb exch def + /lb exch def + getlistset + /sa exch def + /la exch def + [ + [ [ la sb ] [ lb sa ] ] { + aload pop + /t exch def + { + /w exch def + t w known not { + w + } if + } forall + } forall + ] + end +} bind def + +(uncommonwords) test.start +(Mango is sweet) (Mango is sour) uncommonwords [(sweet) (sour)] deepeq test +(Mango Mango) (Orange) uncommonwords [(Orange)] deepeq test +(Mango is Mango) (Orange is Orange) uncommonwords [] deepeq test +test.end diff --git a/challenge-266/roger-bell-west/postscript/ch-2.ps b/challenge-266/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..d42e632710 --- /dev/null +++ b/challenge-266/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,87 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + + +% end included library code + +/xmatrix { + 0 dict begin + dup length 1 sub /order exch def + /valid true def + enumerate.array { + aload pop + exch + /y exch def + enumerate.array { + aload pop + /value exch def + /x exch def + x y eq x order y sub eq or { + value 0 eq { + /valid false def + } if + } { + value 0 ne { + /valid false def + } if + } ifelse + valid not { + exit + } if + } forall + valid not { + exit + } if + } forall + valid + end +} bind def + +(xmatrix) test.start +[[1 0 0 2] [0 3 4 0] [0 5 6 0] [7 0 0 1]] xmatrix test +[[1 2 3] [4 5 6] [7 8 9]] xmatrix not test +[[1 0 2] [0 3 0] [4 0 5]] xmatrix test +test.end diff --git a/challenge-266/roger-bell-west/python/ch-1.py b/challenge-266/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..3981ad0283 --- /dev/null +++ b/challenge-266/roger-bell-west/python/ch-1.py @@ -0,0 +1,36 @@ +#! /usr/bin/python3 + +from collections import defaultdict + +def getlistset(a): + la = a.split(" ") + ca = defaultdict(lambda: 0) + for w in la: + ca[w] += 1 + lb = [w for w in la if ca[w] == 1] + return lb, set(ca.keys()) + +def uncommonwords(a, b): + la, sa = getlistset(a) + lb, sb = getlistset(b) + out = [] + for wl, t in ((la, sb), (lb, sa)): + for w in wl: + if w not in t: + out.append(w) + return out + +import unittest + +class TestUncommonwords(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(uncommonwords("Mango is sweet", "Mango is sour"), ["sweet", "sour"], 'example 1') + + def test_ex2(self): + self.assertEqual(uncommonwords("Mango Mango", "Orange"), ["Orange"], 'example 2') + + def test_ex3(self): + self.assertEqual(uncommonwords("Mango is Mango", "Orange is Orange"), [], 'example 3') + +unittest.main() diff --git a/challenge-266/roger-bell-west/python/ch-2.py b/challenge-266/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..2122921de4 --- /dev/null +++ b/challenge-266/roger-bell-west/python/ch-2.py @@ -0,0 +1,33 @@ +#! /usr/bin/python3 + +def xmatrix(a): + order = len(a) - 1 + valid = True + for y, row in enumerate(a): + for x, value in enumerate(row): + if x == y or x == order - y: + if value == 0: + valid = False + else: + if value != 0: + valid = False + if not valid: + break + if not valid: + break + return valid + +import unittest + +class TestXmatrix(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(xmatrix([[1, 0, 0, 2], [0, 3, 4, 0], [0, 5, 6, 0], [7, 0, 0, 1]]), True, 'example 1') + + def test_ex2(self): + self.assertEqual(xmatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), False, 'example 2') + + def test_ex3(self): + self.assertEqual(xmatrix([[1, 0, 2], [0, 3, 0], [4, 0, 5]]), True, 'example 3') + +unittest.main() diff --git a/challenge-266/roger-bell-west/raku/ch-1.p6 b/challenge-266/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..cedbad2e60 --- /dev/null +++ b/challenge-266/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,31 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is-deeply(uncommonwords('Mango is sweet', 'Mango is sour'), ['sweet', 'sour'], 'example 1'); +is-deeply(uncommonwords('Mango Mango', 'Orange'), ['Orange'], 'example 2'); +is-deeply(uncommonwords('Mango is Mango', 'Orange is Orange'), [], 'example 3'); + +sub getlistset($a) { + my @la = $a.split(' '); + my %ca; + @la.map({%ca{$_}++}); + @la = @la.grep({%ca{$_} == 1}); + return (@la, Set(%ca.keys)); +} + +sub uncommonwords($a, $b) { + my ($la, $sa) = getlistset($a); + my ($lb, $sb) = getlistset($b); + my @out; + for ([$la, $sb], [$lb, $sa]) -> (@wl, %t) { + for @wl -> $w { + unless (%t{$w}:exists) { + @out.push($w); + } + } + } + return @out; +} diff --git a/challenge-266/roger-bell-west/raku/ch-2.p6 b/challenge-266/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..e0910b9906 --- /dev/null +++ b/challenge-266/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,34 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(xmatrix([[1, 0, 0, 2], [0, 3, 4, 0], [0, 5, 6, 0], [7, 0, 0, 1]]), True, 'example 1'); +is(xmatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), False, 'example 2'); +is(xmatrix([[1, 0, 2], [0, 3, 0], [4, 0, 5]]), True, 'example 3'); + +sub xmatrix(@a) { + my $order = @a.elems - 1; + my $valid = True; + for (0 .. @a.end) -> $y { + for (0 .. @a[$y].end) -> $x { + if ($x == $y || $x == $order - $y) { + if (@a[$y][$x] == 0) { + $valid = False; + } + } else { + if (@a[$y][$x] != 0) { + $valid = False; + } + } + unless ($valid) { + last; + } + } + unless ($valid) { + last; + } + } + return $valid; +} diff --git a/challenge-266/roger-bell-west/ruby/ch-1.rb b/challenge-266/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..4322c66911 --- /dev/null +++ b/challenge-266/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,45 @@ +#! /usr/bin/ruby + +require 'set' + +def getlistset(a) + la = a.split(' ') + ca = Hash.new(0) + la.each do |c| + ca[c] += 1 + end + la = la.find_all{|k| ca[k] == 1} + return [la, Set.new(ca.keys)] +end + +def uncommonwords(a, b) + (la, sa) = getlistset(a) + (lb, sb) = getlistset(b) + out = [] + [[la, sb], [lb, sa]].each do |wl, t| + wl.each do |w| + if !t.include?(w) then + out.push(w) + end + end + end + return out +end + +require 'test/unit' + +class TestUncommonwords < Test::Unit::TestCase + + def test_ex1 + assert_equal(['sweet', 'sour'], uncommonwords('Mango is sweet', 'Mango is sour')) + end + + def test_ex2 + assert_equal(['Orange'], uncommonwords('Mango Mango', 'Orange')) + end + + def test_ex3 + assert_equal([], uncommonwords('Mango is Mango', 'Orange is Orange')) + end + +end diff --git a/challenge-266/roger-bell-west/ruby/ch-2.rb b/challenge-266/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..db6a9b6275 --- /dev/null +++ b/challenge-266/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,44 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def xmatrix(a) + order = a.length - 1 + valid = true + a.each_with_index do |row, y| + row.each_with_index do |value, x| + if x == y || x == order - y then + if value == 0 then + valid = false + end + else + if value != 0 then + valid = false + end + end + if !valid then + break + end + end + if !valid then + break + end + end + return valid +end + +class TestXmatrix < Test::Unit::TestCase + + def test_ex1 + assert_equal(true, xmatrix([[1, 0, 0, 2], [0, 3, 4, 0], [0, 5, 6, 0], [7, 0, 0, 1]])) + end + + def test_ex2 + assert_equal(false, xmatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])) + end + + def test_ex3 + assert_equal(true, xmatrix([[1, 0, 2], [0, 3, 0], [4, 0, 5]])) + end + +end diff --git a/challenge-266/roger-bell-west/rust/ch-1.rs b/challenge-266/roger-bell-west/rust/ch-1.rs new file mode 100644 index 0000000000..d82b076dd0 --- /dev/null +++ b/challenge-266/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,44 @@ +use counter::Counter; +use std::collections::HashSet; + +#[test] +fn test_ex1() { + assert_eq!( + uncommonwords("Mango is sweet", "Mango is sour"), + vec!["sweet", "sour"] + ); +} + +#[test] +fn test_ex2() { + assert_eq!(uncommonwords("Mango Mango", "Orange"), vec!["Orange"]); +} + +#[test] +fn test_ex3() { + assert_eq!( + uncommonwords("Mango is Mango", "Orange is Orange"), + Vec::<String>::new() + ); +} + +fn getlistset(a: &str) -> (Vec<&str>, HashSet<&str>) { + let mut la = a.split(' ').collect::<Vec<&str>>(); + let ca = la.iter().copied().collect::<Counter<&str>>(); + la.retain(|k| *ca.get(k).unwrap() == 1); + (la, ca.keys().copied().collect::<HashSet<&str>>()) +} + +fn uncommonwords(a: &str, b: &str) -> Vec<String> { |
