diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-07 14:49:34 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-07 14:49:34 +0000 |
| commit | 8acd05841cdfa05a5b8d0bc29b9f3395bf3c9778 (patch) | |
| tree | eafe54e2495d6ff40adbf60d0b0e1cbe2e37fbe2 | |
| parent | efcdd2f9c060717cbc64427dff3dc259f34c01b9 (diff) | |
| parent | 5b79a36456694c1c77104ddec7c315cffa74b994 (diff) | |
| download | perlweeklychallenge-club-8acd05841cdfa05a5b8d0bc29b9f3395bf3c9778.tar.gz perlweeklychallenge-club-8acd05841cdfa05a5b8d0bc29b9f3395bf3c9778.tar.bz2 perlweeklychallenge-club-8acd05841cdfa05a5b8d0bc29b9f3395bf3c9778.zip | |
Merge pull request #9024 from Firedrake/rogerbw-challenge-242
RogerBW solutions for challenge no. 242
21 files changed, 981 insertions, 0 deletions
diff --git a/challenge-242/roger-bell-west/javascript/ch-1.js b/challenge-242/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..7a4375078c --- /dev/null +++ b/challenge-242/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,65 @@ +#! /usr/bin/node + +"use strict" + +function unique(a) { + let ss = new Set; + let b = []; + for (let i of a) { + if (!ss.has(i)) { + b.push(i); + ss.add(i); + } + } + return b; +} + +function halfmissing(a, bh) { + return unique(a.filter(n => !bh.has(n))) +} + +function missingmembers(a, b) { + const ah = new Set(a); + const bh = new Set(b); + return [halfmissing(a, bh), halfmissing(b, ah)]; +} + +// 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(missingmembers([1, 2, 3], [2, 4, 6]), [[1, 3], [4, 6]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(missingmembers([1, 2, 3, 3], [1, 1, 2, 2]), [[3], []])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-242/roger-bell-west/javascript/ch-2.js b/challenge-242/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..f882ea2f06 --- /dev/null +++ b/challenge-242/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,53 @@ +#! /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 flipmatrix(a) { + let b = []; + for (let row of a) { + let r2 = row.map(i => 1 - i); + r2.reverse(); + b.push(r2); + } + return b; +} + +if (deepEqual(flipmatrix([[1, 1, 0], [1, 0, 1], [0, 0, 0]]), [[1, 0, 0], [0, 1, 0], [1, 1, 1]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(flipmatrix([[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]]), [[1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-242/roger-bell-west/kotlin/ch-1.kt b/challenge-242/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..e4f2edf224 --- /dev/null +++ b/challenge-242/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,26 @@ +fun halfmissing(a: List<Int>, bh: Set<Int>): List<Int> { + return a.filter {n -> !bh.contains(n)}.distinct().toList() +} + +fun missingmembers(a: List<Int>, b: List<Int>): List<List<Int>> { + val ah = a.toSet() + val bh = b.toSet() + return listOf(halfmissing(a, bh), halfmissing(b, ah)) +} + +fun main() { + + if (missingmembers(listOf(1, 2, 3), listOf(2, 4, 6)) == listOf(listOf(1, 3), listOf(4, 6))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (missingmembers(listOf(1, 2, 3, 3), listOf(1, 1, 2, 2)) == listOf(listOf(3), emptyList<Int>())) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-242/roger-bell-west/kotlin/ch-2.kt b/challenge-242/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..49c946cef0 --- /dev/null +++ b/challenge-242/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,20 @@ +fun flipmatrix(a: List<List<Int>>): List<List<Int>> { + return a.map{row -> row.map{1 - it}.reversed()} +} + +fun main() { + + if (flipmatrix(listOf(listOf(1, 1, 0), listOf(1, 0, 1), listOf(0, 0, 0))) == listOf(listOf(1, 0, 0), listOf(0, 1, 0), listOf(1, 1, 1))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (flipmatrix(listOf(listOf(1, 1, 0, 0), listOf(1, 0, 0, 1), listOf(0, 1, 1, 1), listOf(1, 0, 1, 0))) == listOf(listOf(1, 1, 0, 0), listOf(0, 1, 1, 0), listOf(0, 0, 0, 1), listOf(1, 0, 1, 0))) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-242/roger-bell-west/lua/ch-1.lua b/challenge-242/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..e720e01534 --- /dev/null +++ b/challenge-242/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,85 @@ +#! /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 unique(a) + local ss = {} + local b = {} + for _, i in ipairs(a) do + if ss[i] == nil then + table.insert(b, i) + ss[i] = true + end + end + return b +end + +function halfmissing(a, bh) + local c = {} + for _, i in ipairs(a) do + if bh[i] == nil then + table.insert(c, i) + end + end + return unique(c) +end + +function toset(a) + local ss = {} + for _, i in ipairs(a) do + ss[i] = true + end + return ss +end + + +function missingmembers(a, b) + local ah = toset(a) + local bh = toset(b) + return { + halfmissing(a, bh), + halfmissing(b, ah) + } +end + +if recursive_compare(missingmembers({1, 2, 3}, {2, 4, 6}), {{1, 3}, {4, 6}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(missingmembers({1, 2, 3, 3}, {1, 1, 2, 2}), {{3}, {}}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-242/roger-bell-west/lua/ch-2.lua b/challenge-242/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..a384c82dd9 --- /dev/null +++ b/challenge-242/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,57 @@ +#! /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 flipmatrix(a) + local b = {} + for _, row in ipairs(a) do + local r2 = {} + for _b, v in ipairs(row) do + table.insert(r2, 1, 1 - v) + end + table.insert(b, r2) + end + return b +end + +if recursive_compare(flipmatrix({{1, 1, 0}, {1, 0, 1}, {0, 0, 0}}), {{1, 0, 0}, {0, 1, 0}, {1, 1, 1}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(flipmatrix({{1, 1, 0, 0}, {1, 0, 0, 1}, {0, 1, 1, 1}, {1, 0, 1, 0}}), {{1, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 0, 1}, {1, 0, 1, 0}}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-242/roger-bell-west/perl/ch-1.pl b/challenge-242/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..7216e1687c --- /dev/null +++ b/challenge-242/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,25 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is_deeply(missingmembers([1, 2, 3], [2, 4, 6]), [[1, 3], [4, 6]], 'example 1'); +is_deeply(missingmembers([1, 2, 3, 3], [1, 1, 2, 2]), [[3], []], 'example 2'); + +use List::Util qw(uniq); + +sub halfmissing($a, $bh) { + return [uniq grep {!exists $bh->{$_}} @{$a}]; +} + +sub missingmembers($a, $b) { + my $ah = {map {$_ => 1} @{$a}}; + my $bh = {map {$_ => 1} @{$b}}; + return [ + halfmissing($a, $bh), + halfmissing($b, $ah), + ]; +} diff --git a/challenge-242/roger-bell-west/perl/ch-2.pl b/challenge-242/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..89da4f6938 --- /dev/null +++ b/challenge-242/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,14 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is_deeply(flipmatrix([[1, 1, 0], [1, 0, 1], [0, 0, 0]]), [[1, 0, 0], [0, 1, 0], [1, 1, 1]], 'example 1'); +is_deeply(flipmatrix([[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]]), [[1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]], 'example 2'); + +sub flipmatrix($a) { + return [map {[reverse map {1 - $_} @{$_}]} @{$a}]; +} diff --git a/challenge-242/roger-bell-west/postscript/ch-1.ps b/challenge-242/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..8522cacd28 --- /dev/null +++ b/challenge-242/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,171 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/filter { % array proc(bool) -> array + 1 dict begin + /p exch def + [ exch + { + dup p not + { + pop + } if + } forall + ] + end +} bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} bind def + +/unique { + toset keys +} 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 + +/toset { % array -> dict of (value, true) + << exch + { + true + } forall + >> +} 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 { + /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 + +/unique { + 0 dict begin + /ss 0 dict def + [ exch + { + /i exch def + ss i known not { + i + ss i true put + } if + } forall + ] + end +} bind def + +/halfmissing { + 0 dict begin + /bh exch def + { + bh exch known not + } filter + unique + end +} bind def + +/missingmembers { + 0 dict begin + /b exch def + /a exch def + [ + a b toset halfmissing + b a toset halfmissing + ] + end +} bind def + +(missingmembers) test.start +[1 2 3] [2 4 6] missingmembers [[1 3] [4 6]] deepeq test +[1 2 3 3] [1 1 2 2] missingmembers [[3] []] deepeq test +test.end diff --git a/challenge-242/roger-bell-west/postscript/ch-2.ps b/challenge-242/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..6c70e8f06b --- /dev/null +++ b/challenge-242/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,132 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/deepeq { + 2 dict begin + /a exch def + /b exch def + a type b type eq { + a type /dicttype eq { + a length b length eq { + << + a { + pop + true + } forall + b { + pop + true + } forall + >> + true exch + { + pop + dup a exch known { + dup b exch known { + dup a exch get exch b exch get deepeq not { + pop false + } if + } { + false + } ifelse + } { + false + } ifelse + } forall + } { + false + } ifelse + } { + a type dup /arraytype eq exch /stringtype eq or { + a length b length eq { + true + 0 1 a length 1 sub { + dup a exch get exch b exch get deepeq not { + pop false + exit + } if + } for + } { + false + } ifelse + } { + a b eq + } ifelse + } ifelse + } { + false + } ifelse + end +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/reverse { + 1 dict begin + dup length /l exch def + [ exch + aload pop + 2 1 l { + -1 roll + } for + ] + 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 + +/map { % array proc -> array + 2 dict begin + /p exch def + [ exch + { + p + } forall + ] + 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 + + +% end included library code + +/flipmatrix { + { + { + 1 exch sub + } map + reverse + } map +} bind def + +(flipmatrix) test.start +[[1 1 0] [1 0 1] [0 0 0]] flipmatrix [[1 0 0] [0 1 0] [1 1 1]] deepeq test +[[1 1 0 0] [1 0 0 1] [0 1 1 1] [1 0 1 0]] flipmatrix [[1 1 0 0] [0 1 1 0] [0 0 0 1] [1 0 1 0]] deepeq test +test.end diff --git a/challenge-242/roger-bell-west/python/ch-1.py b/challenge-242/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..a4b970b293 --- /dev/null +++ b/challenge-242/roger-bell-west/python/ch-1.py @@ -0,0 +1,30 @@ +#! /usr/bin/python3 + +def unique(a): + ss = set() + b = [] + for i in a: + if i not in ss: + b.append(i) + ss.add(i) + return b + +def halfmissing(a, bh): + return unique(i for i in a if i not in bh) + +def missingmembers(a, b): + ah = set(a) + bh = set(b) + return [halfmissing(a, bh), halfmissing(b, ah)] + +import unittest + +class TestMissingmembers(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(missingmembers([1, 2, 3], [2, 4, 6]), [[1, 3], [4, 6]], 'example 1') + + def test_ex2(self): + self.assertEqual(missingmembers([1, 2, 3, 3], [1, 1, 2, 2]), [[3], []], 'example 2') + +unittest.main() diff --git a/challenge-242/roger-bell-west/python/ch-2.py b/challenge-242/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..c01ccf6c74 --- /dev/null +++ b/challenge-242/roger-bell-west/python/ch-2.py @@ -0,0 +1,19 @@ +#! /usr/bin/python3 + +def flipmatrix(a): + b = [] + for row in a: + b.append(list(reversed([1 - i for i in row]))) + return b + +import unittest + +class TestFlipmatrix(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(flipmatrix([[1, 1, 0], [1, 0, 1], [0, 0, 0]]), [[1, 0, 0], [0, 1, 0], [1, 1, 1]], 'example 1') + + def test_ex2(self): + self.assertEqual(flipmatrix([[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]]), [[1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]], 'example 2') + +unittest.main() diff --git a/challenge-242/roger-bell-west/raku/ch-1.p6 b/challenge-242/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..4b4bf48abc --- /dev/null +++ b/challenge-242/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,21 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is-deeply(missingmembers([1, 2, 3], [2, 4, 6]), [[1, 3], [4, 6]], 'example 1'); +is-deeply(missingmembers([1, 2, 3, 3], [1, 1, 2, 2]), [[3], []], 'example 2'); + +sub halfmissing(@a, $bh) { + return @a.grep({$bh{$_}:!exists}).unique.Array; +} + +sub missingmembers(@a, @b) { + my $ah = @a.Set; + my $bh = @b.Set; + return [ + halfmissing(@a, $bh), + halfmissing(@b, $ah), + ]; +} diff --git a/challenge-242/roger-bell-west/raku/ch-2.p6 b/challenge-242/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..f2a19bae8b --- /dev/null +++ b/challenge-242/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,12 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is-deeply(flipmatrix([[1, 1, 0], [1, 0, 1], [0, 0, 0]]), [[1, 0, 0], [0, 1, 0], [1, 1, 1]], 'example 1'); +is-deeply(flipmatrix([[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]]), [[1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]], 'example 2'); + +sub flipmatrix(@a) { + return @a.map({$_.map({1 - $_}).reverse.Array}).Array; +} diff --git a/challenge-242/roger-bell-west/ruby/ch-1.rb b/challenge-242/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..c34f2d7d75 --- /dev/null +++ b/challenge-242/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,30 @@ +#! /usr/bin/ruby + +require 'set' + +def halfmissing(a, bh) + return a.find_all {|n| !bh.include?(n)}.uniq +end + +def missingmembers(a, b) + ah = Set.new(a) + bh = Set.new(b) + return [ + halfmissing(a, bh), + halfmissing(b, ah) + ] +end + +require 'test/unit' + +class TestMissingmembers < Test::Unit::TestCase + + def test_ex1 + assert_equal([[1, 3], [4, 6]], missingmembers([1, 2, 3], [2, 4, 6])) + end + + def test_ex2 + assert_equal([[3], []], missingmembers([1, 2, 3, 3], [1, 1, 2, 2])) + end + +end diff --git a/challenge-242/roger-bell-west/ruby/ch-2.rb b/challenge-242/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..47bf67b7ee --- /dev/null +++ b/challenge-242/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,19 @@ +#! /usr/bin/ruby + +def flipmatrix(a) + return a.collect{|r| r.collect{|m| 1-m}.reverse} +end + +require 'test/unit' + +class TestFlipmatrix < Test::Unit::TestCase + + def test_ex1 + assert_equal([[1, 0, 0], [0, 1, 0], [1, 1, 1]], flipmatrix([[1, 1, 0], [1, 0, 1], [0, 0, 0]])) + end + + def test_ex2 + assert_equal([[1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]], flipmatrix([[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]])) + end + +end diff --git a/challenge-242/roger-bell-west/rust/ch-1.rs b/challenge-242/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..ecc248438b --- /dev/null +++ b/challenge-242/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,32 @@ +use itertools::Itertools; +use std::collections::HashSet; + +#[test] +fn test_ex1() { + assert_eq!( + missingmembers(vec![1, 2, 3], vec![2, 4, 6]), + vec![vec![1, 3], vec![4, 6]] + ); +} + +#[test] +fn test_ex2() { + assert_eq!( + missingmembers(vec![1, 2, 3, 3], vec![1, 1, 2, 2]), + vec![vec![3], Vec::<u32>::new()] + ); +} + +fn halfmissing(a: &Vec<u32>, bh: HashSet<&u32>) -> Vec<u32> { + a.iter() + .filter(|n| !bh.contains(n)) + .unique() + .map(|i| *i) + .collect::<Vec<_>>() +} + +fn missingmembers(a: Vec<u32>, b: Vec<u32>) -> Vec<Vec<u32>> { + let ah = a.iter().collect::<HashSet<_>>(); + let bh = b.iter().collect::<HashSet<_>>(); + vec![halfmissing(&a, bh), halfmissing(&b, ah)] +} diff --git a/challenge-242/roger-bell-west/rust/ch-2.rs b/challenge-242/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..65c9c55082 --- /dev/null +++ b/challenge-242/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,38 @@ +#! /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!( + flipmatrix(vec![vec![1, 1, 0], vec![1, 0, 1], vec![0, 0, 0]]), + vec![vec![1, 0, 0], vec![0, 1, 0], vec![1, 1, 1]] + ); +} + +#[test] +fn test_ex2() { + assert_eq!( + flipmatrix(vec![ + vec![1, 1, 0, 0], + vec![1, 0, 0, 1], + vec![0, 1, 1, 1], + vec![1, 0, 1, 0] + ]), + vec![ + vec![1, 1, 0, 0], + vec![0, 1, 1, 0], + vec![0, 0, 0, 1], + vec![1, 0, 1, 0] + ] + ); +} + +fn flipmatrix(a: Vec<Vec<u8>>) -> Vec<Vec<u8>> { + let mut b = Vec::new(); + for row in a { + let mut r2 = row.into_iter().map(|i| 1 - i).collect::<Vec<_>>(); + r2.reverse(); + b.push(r2); + } + b +} diff --git a/challenge-242/roger-bell-west/scala/ch-1.scala b/challenge-242/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..39fe097d39 --- /dev/null +++ b/challenge-242/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,26 @@ + +object Missingmembers { + def halfmissing(a: List[Int], bh: Set[Int]): List[Int] = { + return a.filter(n => !bh.contains(n)).distinct + } + def missingmembers(a: List[Int], b: List[Int]): List[List[Int]] = { + val ah = a.toSet + val bh = b.toSet + return List(halfmissing(a, bh), halfmissing(b, ah)) + } + def main(args: Array[String]) { + if (missingmembers(List(1, 2, 3), List(2, 4, 6)) == List(List(1, 3), List(4, 6))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (missingmembers(List(1, 2, 3, 3), List(1, 1, 2, 2)) == List(List(3), List())) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-242/roger-bell-west/scala/ch-2.scala b/challenge-242/roger-bell-west/scala/ch-2.scala new file mode 100644 |
