diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-18 21:32:42 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-18 21:32:42 +0000 |
| commit | b3c3ecb1de5ee2622b63b089aba08f9457f3e818 (patch) | |
| tree | 00c1f848dbb91cb0d4a7a153325effec5120c70a | |
| parent | 03dddf36060a3f882ab7610894a21a41fa93359d (diff) | |
| parent | 0f4fe3e4f30d32cf0c88ce24d1a658c0f5439bdf (diff) | |
| download | perlweeklychallenge-club-b3c3ecb1de5ee2622b63b089aba08f9457f3e818.tar.gz perlweeklychallenge-club-b3c3ecb1de5ee2622b63b089aba08f9457f3e818.tar.bz2 perlweeklychallenge-club-b3c3ecb1de5ee2622b63b089aba08f9457f3e818.zip | |
Merge pull request #7735 from Firedrake/rogerbw-challenge-208
RogerBW solutions for challenge no. 208
19 files changed, 1157 insertions, 0 deletions
diff --git a/challenge-208/roger-bell-west/javascript/ch-1.js b/challenge-208/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..57186a97c3 --- /dev/null +++ b/challenge-208/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,79 @@ +#! /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 v2hm(a) { + let h = new Map(); + a.forEach((x, i) => { + if (!h.has(x)) { + h.set(x, i); + } + }); + return h; +} + +function minindexsum(a, b) { + let ah = v2hm(a); + let bh = v2hm(b); + let out = []; + let mi = a.length + b.length; + for (let w of a) { + if (bh.has(w)) { + let mv = ah.get(w) + bh.get(w); + if (mv < mi) { + out = []; + mi = mv; + } + if (mv == mi) { + out.push(w); + } + } + } + return out; +} + +if (deepEqual(minindexsum(['Perl', 'Raku', 'Love'], ['Raku', 'Perl', 'Hate']), ['Perl', 'Raku'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(minindexsum(['A', 'B', 'C'], ['D', 'E', 'F']), [])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(minindexsum(['A', 'B', 'C'], ['C', 'A', 'B']), ['A'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-208/roger-bell-west/javascript/ch-2.js b/challenge-208/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..49ebe84e79 --- /dev/null +++ b/challenge-208/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,73 @@ +#! /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 duplicateandmissing(a) { + let flags = 0; + let dup = 0; + let mis = 0; + let exp = a[0] + 1; + for (let n of a.slice(1)) { + if (n < exp) { + dup = n + flags |= 1; + } else if (n > exp) { + mis = exp; + flags |= 2; + } + if (flags == 3) { + return [dup, mis]; + } + exp = n + 1; + } + if (flags == 1) { + return [dup, exp]; + } + return [-1]; +} + +if (deepEqual(duplicateandmissing([1, 2, 2, 4]), [2, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(duplicateandmissing([1, 2, 3, 4]), [-1])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(duplicateandmissing([1, 2, 3, 3]), [3, 4])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-208/roger-bell-west/kotlin/ch-1.kt b/challenge-208/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..f571b42ac0 --- /dev/null +++ b/challenge-208/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,52 @@ +fun v2hm(a: List<String>): Map<String, Int> { + var h = mutableMapOf<String, Int>() + for ((i, x) in a.withIndex()) { + if (!h.containsKey(x)) { + h[x] = i + } + } + return h +} + +fun minindexsum(a: List<String>, b: List<String>): List<String> { + val ah = v2hm(a) + val bh = v2hm(b) + var out = ArrayList<String>() + var mi = a.size + b.size + for (w in a) { + if (bh.containsKey(w)) { + val mv = ah[w]!! + bh[w]!! + if (mv < mi) { + out.clear() + mi = mv + } + if (mv == mi) { + out.add(w) + } + } + } + return out.toList() +} + +fun main() { + + if (minindexsum(listOf("Perl", "Raku", "Love"), listOf("Raku", "Perl", "Hate")) == listOf("Perl", "Raku")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (minindexsum(listOf("A", "B", "C"), listOf("D", "E", "F")) == emptyList<String>()) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (minindexsum(listOf("A", "B", "C"), listOf("C", "A", "B")) == listOf("A")) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-208/roger-bell-west/kotlin/ch-2.kt b/challenge-208/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..c6360bee4e --- /dev/null +++ b/challenge-208/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,46 @@ +fun duplicateandmissing(a: List<Int>): List<Int> { + var flags = 0 + var dup = 0 + var mis = 0 + var exp = a[0] + 1 + for (n in a.drop(1)) { + if (n < exp) { + dup = n + flags = flags or 1 + } else if (n > exp) { + mis = exp + flags = flags or 2 + } + if (flags == 3) { + return listOf(dup, mis) + } + exp = n + 1 + } + if (flags == 1) { + return listOf(dup, exp) + } + return listOf(-1) +} + +fun main() { + + if (duplicateandmissing(listOf(1, 2, 2, 4)) == listOf(2, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (duplicateandmissing(listOf(1, 2, 3, 4)) == listOf(-1)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (duplicateandmissing(listOf(1, 2, 3, 3)) == listOf(3, 4)) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-208/roger-bell-west/lua/ch-1.lua b/challenge-208/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..93fe24ce6a --- /dev/null +++ b/challenge-208/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,73 @@ +#! /usr/bin/lua + +-- by Michael Anderson at +-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua +function recursive_compare(t1,t2) + if t1==t2 then return true end + if (type(t1)~="table") then return false end + local mt1 = getmetatable(t1) + local mt2 = getmetatable(t2) + if( not recursive_compare(mt1,mt2) ) then return false end + for k1,v1 in pairs(t1) do + local v2 = t2[k1] + if( not recursive_compare(v1,v2) ) then return false end + end + for k2,v2 in pairs(t2) do + local v1 = t1[k2] + if( not recursive_compare(v1,v2) ) then return false end + end + return true +end + +function v2hm(a) + local h = {} + for i, x in ipairs(a) do + if h[x] == nil then + h[x] = i + end + end + return h +end + +function minindexsum(a, b) + local ah = v2hm(a) + local bh = v2hm(b) + local out = {} + local mi = #a + #b + for w, av in pairs(ah) do + if bh[w] ~= nil then + mv = av + bh[w] + if mv < mi then + out = {} + mi = mv + end + if mv == mi then + table.insert(out, w) + end + end + end + table.sort(out) + return out +end + +if recursive_compare(minindexsum({"Perl", "Raku", "Love"}, {"Raku", "Perl", "Hate"}), {"Perl", "Raku"}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(minindexsum({"A", "B", "C"}, {"D", "E", "F"}), {}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(minindexsum({"A", "B", "C"}, {"C", "A", "B"}), {"A"}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-208/roger-bell-west/lua/ch-2.lua b/challenge-208/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..00240af312 --- /dev/null +++ b/challenge-208/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,66 @@ +#! /usr/bin/lua + +-- by Michael Anderson at +-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua +function recursive_compare(t1,t2) + if t1==t2 then return true end + if (type(t1)~="table") then return false end + local mt1 = getmetatable(t1) + local mt2 = getmetatable(t2) + if( not recursive_compare(mt1,mt2) ) then return false end + for k1,v1 in pairs(t1) do + local v2 = t2[k1] + if( not recursive_compare(v1,v2) ) then return false end + end + for k2,v2 in pairs(t2) do + local v1 = t1[k2] + if( not recursive_compare(v1,v2) ) then return false end + end + return true +end + +function duplicateandmissing(a) + local flags = 0 + local dup = 0 + local mis = 0 + local exp = a[1] + for i, n in ipairs(a) do + if n < exp then + dup = n + flags = flags | 1 + elseif n > exp then + mis = exp + flags = flags | 2 + end + if flags == 3 then + return {dup, mis} + end + exp = n + 1 + end + if flags == 1 then + return {dup, exp} + end + return {-1} +end + +if recursive_compare(duplicateandmissing({1, 2, 2, 4}), {2, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(duplicateandmissing({1, 2, 3, 4}), {-1}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(duplicateandmissing({1, 2, 3, 3}), {3, 4}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-208/roger-bell-west/perl/ch-1.pl b/challenge-208/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..5601d59ebc --- /dev/null +++ b/challenge-208/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,41 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is_deeply(minindexsum(['Perl', 'Raku', 'Love'], ['Raku', 'Perl', 'Hate']), ['Perl', 'Raku'], 'example 1'); +is_deeply(minindexsum(['A', 'B', 'C'], ['D', 'E', 'F']), [], 'example 2'); +is_deeply(minindexsum(['A', 'B', 'C'], ['C', 'A', 'B']), ['A'], 'example 3'); + +sub v2hm($a) { + my %h; + foreach my $i (0..$#{$a}) { + unless (exists $h{$a->[$i]}) { + $h{$a->[$i]} = $i; + } + } + return \%h; +} + +sub minindexsum($a, $b) { + my $ah = v2hm($a); + my $bh = v2hm($b); + my @out; + my $mi = scalar @{$a} + scalar @{$b}; + foreach my $w (@{$a}) { + if (exists $bh->{$w}) { + my $mv = $ah->{$w} + $bh->{$w}; + if ($mv < $mi) { + @out = (); + $mi = $mv; + } + if ($mv == $mi) { + push @out, $w; + } + } + } + return \@out; +} diff --git a/challenge-208/roger-bell-west/perl/ch-2.pl b/challenge-208/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..60b35ae539 --- /dev/null +++ b/challenge-208/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,35 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is_deeply(duplicateandmissing([1, 2, 2, 4]), [2, 3], 'example 1'); +is_deeply(duplicateandmissing([1, 2, 3, 4]), [-1], 'example 2'); +is_deeply(duplicateandmissing([1, 2, 3, 3]), [3, 4], 'example 3'); + +sub duplicateandmissing($a) { + my $flags = 0; + my $dup = 0; + my $mis = 0; + my $exp = $a->[0]; + foreach my $n (@{$a}) { + if ($n < $exp) { + $dup = $n; + $flags |= 1; + } elsif ($n > $exp) { + $mis = $exp; + $flags |= 2; + } + if ($flags == 3) { + return [$dup, $mis]; + } + $exp = $n + 1; + } + if ($flags == 1) { + return [$dup, $exp]; + } + return [-1]; +} diff --git a/challenge-208/roger-bell-west/postscript/ch-1.ps b/challenge-208/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..e90a57ced7 --- /dev/null +++ b/challenge-208/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,162 @@ +%!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 { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/deepeq { + 2 dict begin + /a exch def + /b exch def + a type b type eq { + a type /dicttype eq { + a length b length eq { + << + a { + pop + true + } forall + b { + pop + true + } forall + >> + true exch + { + pop + dup a exch known { + dup b exch known { + dup a exch get exch b exch get deepeq not { + pop false + } if + } { + false + } ifelse + } { + false + } ifelse + } forall + } { + false + } ifelse + } { + a type dup /arraytype eq exch /stringtype eq or { + a length b length eq { + true + 0 1 a length 1 sub { + dup a exch get exch b exch get deepeq not { + pop false + exit + } if + } for + } { + false + } ifelse + } { + a b eq + } ifelse + } ifelse + } { + false + } ifelse + end +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} bind def + + +% end included library code + +/v2hm { + 1 dict begin + /h 0 dict def + enumerate.array { + aload pop dup + h exch known { + pop pop + } { + exch + h 3 1 roll + put + } ifelse + } forall + h + end +} bind def + +/minindexsum { + 6 dict begin + v2hm /bh exch def + /a exch def + /ah a v2hm def + /mi a length bh keys length add def + [ mark + a { + /w exch def + bh w known { + /mv ah w get bh w get add def + mv mi lt { + cleartomark + mark + /mi mv def + } if + mv mi eq { + w + } if + } if + } forall + ] + end +} bind def + +(minindexsum) test.start +[(Perl) (Raku) (Love)] [(Raku) (Perl) (Hate)] minindexsum [(Perl) (Raku)] deepeq test +[(A) (B) (C)] [(D) (E) (F)] minindexsum [] deepeq test +[(A) (B) (C)] [(C) (A) (B)] minindexsum [(A)] deepeq test +test.end diff --git a/challenge-208/roger-bell-west/postscript/ch-2.ps b/challenge-208/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..a4c0bc2d58 --- /dev/null +++ b/challenge-208/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,137 @@ +%!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.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} 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 + + +% end included library code + +/duplicateandmissing { + 4 dict begin + dup + /flags 0 def + /ddup 0 def + /mis 0 def + /eexp exch 0 get def + { + /n exch def + n eexp lt { + /ddup n def + /flags flags 1 or def + } if + n eexp gt { + /mis eexp def + /flags flags 2 or def + } if + flags 3 eq { + exit + } if + /eexp n 1 add def + } forall + 1 { + flags 3 eq { + [ ddup mis ] + exit + } if + flags 1 eq { + [ ddup eexp ] + exit + } if + [ -1 ] + } repeat + end +} bind def + +(duplicateandmissing) test.start +[1 2 2 4] duplicateandmissing [2 3] deepeq test +[1 2 3 4] duplicateandmissing [-1] deepeq test +[1 2 3 3] duplicateandmissing [3 4] deepeq test +test.end diff --git a/challenge-208/roger-bell-west/python/ch-1.py b/challenge-208/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..42006d3ac2 --- /dev/null +++ b/challenge-208/roger-bell-west/python/ch-1.py @@ -0,0 +1,38 @@ +#! /usr/bin/python3 + +import unittest + +def v2hm(a): + h = dict() + for i, x in enumerate(a): + if x not in h: + h[x] = i + return h + +def minindexsum(a, b): + ah = v2hm(a) + bh = v2hm(b) + out = [] + mi = len(a) + len(b) + for w in a: + if w in bh: + mv = ah[w] + bh[w] + if mv < mi: + out = [] + mi = mv + if mv == mi: + out.append(w) + return out + +class TestMinindexsum(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(minindexsum(["Perl", "Raku", "Love"], ["Raku", "Perl", "Hate"]), ["Perl", "Raku"], 'example 1') + + def test_ex2(self): + self.assertEqual(minindexsum(["A", "B", "C"], ["D", "E", "F"]), [], 'example 2') + + def test_ex3(self): + self.assertEqual(minindexsum(["A", "B", "C"], ["C", "A", "B"]), ["A"], 'example 3') + +unittest.main() diff --git a/challenge-208/roger-bell-west/python/ch-2.py b/challenge-208/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..f492b4c6ab --- /dev/null +++ b/challenge-208/roger-bell-west/python/ch-2.py @@ -0,0 +1,35 @@ +#! /usr/bin/python3 + +import unittest + +def duplicateandmissing(a): + flags = 0 + dup = 0 + mis = 0 + exp = a[0] + for n in a: + if n < exp: + dup = n + flags |= 1 + elif n > exp: + mis = exp + flags |= 2 + if flags == 3: + return [dup, mis] + exp = n + 1 + if flags == 1: + return [dup, exp] + return [-1] + +class TestDuplicateandmissing(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(duplicateandmissing([1, 2, 2, 4]), [2, 3], 'example 1') + + def test_ex2(self): + self.assertEqual(duplicateandmissing([1, 2, 3, 4]), [-1], 'example 2') + + def test_ex3(self): + self.assertEqual(duplicateandmissing([1, 2, 3, 3]), [3, 4], 'example 3') + +unittest.main() diff --git a/challenge-208/roger-bell-west/raku/ch-1.p6 b/challenge-208/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..f350678181 --- /dev/null +++ b/challenge-208/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,39 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is-deeply(minindexsum(['Perl', 'Raku', 'Love'], ['Raku', 'Perl', 'Hate']), ['Perl', 'Raku'], 'example 1'); +is-deeply(minindexsum(['A', 'B', 'C'], ['D', 'E', 'F']), [], 'example 2'); +is-deeply(minindexsum(['A', 'B', 'C'], ['C', 'A', 'B']), ['A'], 'example 3'); + +sub v2hm(@a) { + my %h; + for (0..@a.end) -> $i { + unless (%h{@a[$i]}:exists) { + %h{@a[$i]} = $i; + } + } + return %h; +} + +sub minindexsum(@a, @b) { + my %ah = v2hm(@a); + my %bh = v2hm(@b); + my @out; + my $mi = @a.elems + @b.elems; + for @a -> $w { + if (%bh{$w}:exists) { + my $mv = %ah{$w} + %bh{$w}; + if ($mv < $mi) { + @out = (); + $mi = $mv; + } + if ($mv == $mi) { + @out.push($w); + } + } + } + return @out; +} diff --git a/challenge-208/roger-bell-west/raku/ch-2.p6 b/challenge-208/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..e358154b9a --- /dev/null +++ b/challenge-208/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,33 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is-deeply(duplicateandmissing([1, 2, 2, 4]), [2, 3], 'example 1'); +is-deeply(duplicateandmissing([1, 2, 3, 4]), [-1], 'example 2'); +is-deeply(duplicateandmissing([1, 2, 3, 3]), [3, 4], 'example 3'); + +sub duplicateandmissing(@a) { + my $flags = 0; + my $dup = 0; + my $mis = 0; + my $exp = @a[0]; + for @a -> $n { + if ($n < $exp) { + $dup = $n; + $flags +|= 1; + } elsif ($n > $exp) { + $mis = $exp; + $flags +|= 2; + } + if ($flags == 3) { + return [$dup, $mis]; + } + $exp = $n + 1; + } + if ($flags == 1) { + return [$dup, $exp]; + } + return [-1]; +} diff --git a/challenge-208/roger-bell-west/ruby/ch-1.rb b/challenge-208/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..ddf57f5b8d --- /dev/null +++ b/challenge-208/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,50 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def v2hm(a) + h = Hash.new + a.each_with_index do |x, i| + if !h.has_key?(x) then + h[x] = i + end + end + return h +end + +def minindexsum(a, b) + ah = v2hm(a) + bh = v2hm(b) + out = [] + mi = a.length + b.length + a.each do |w| + if bh.has_key?(w) then + mv = ah[w] + bh[w] + if mv < mi then + out = [] + mi = mv + end + if mv == mi then + out.push(w) + end + end + end + return out +end + + +class TestMinindexsum < Test::Unit::TestCase + + def test_ex1 + assert_equal(['Perl', 'Raku'], minindexsum(['Perl', 'Raku', 'Love'], ['Raku', 'Perl', 'Hate'])) + end + + def test_ex2 + assert_equal([], minindexsum(['A', 'B', 'C'], ['D', 'E', 'F'])) + end + + def test_ex3 + assert_equal(['A'], minindexsum(['A', 'B', 'C'], ['C', 'A', 'B'])) + end + |
