diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-02 13:54:38 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-02 13:54:38 +0100 |
| commit | 597786308ff8f7d4fbf493789f7504e56db3e180 (patch) | |
| tree | 3e8c4a01bd2d49663de9fefa62042ce811266054 | |
| parent | 859adfc77544a11a83a43b8f5f367e12bf6029b9 (diff) | |
| parent | ef74a9c78b93907ac537ad721eebb6f7ed53f701 (diff) | |
| download | perlweeklychallenge-club-597786308ff8f7d4fbf493789f7504e56db3e180.tar.gz perlweeklychallenge-club-597786308ff8f7d4fbf493789f7504e56db3e180.tar.bz2 perlweeklychallenge-club-597786308ff8f7d4fbf493789f7504e56db3e180.zip | |
Merge pull request #9858 from Firedrake/rogerbw-challenge-263
RogerBW solutions for challenge no. 263
21 files changed, 1209 insertions, 0 deletions
diff --git a/challenge-263/roger-bell-west/javascript/ch-1.js b/challenge-263/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..82eef2d394 --- /dev/null +++ b/challenge-263/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,63 @@ +#! /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 targetindex(a0, k) { + let a = a0; + a.sort(function(a,b) { + return a-b; + }); + let r = []; + a.forEach((v, i) => { + if (v == k) { + r.push(i); + } + }); + return r; +} + +if (deepEqual(targetindex([1, 5, 3, 2, 4, 2], 2), [1, 2])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(targetindex([1, 2, 4, 3, 5], 6), [])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(targetindex([5, 3, 2, 4, 2, 1], 4), [4])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-263/roger-bell-west/javascript/ch-2.js b/challenge-263/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..c559458859 --- /dev/null +++ b/challenge-263/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,64 @@ +#! /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 mergeitems(a, b) { + let c = new Map; + for (let v of [a, b]) { + for (let w of v) { + if (!c.has(w[0])) { + c.set(w[0], 0); + } + c.set(w[0], c.get(w[0]) + w[1]); + } + } + let k = [...c.keys()]; + k.sort(); + return k.map(x => [x, c.get(x) ]); +} + +if (deepEqual(mergeitems([[1, 1], [2, 1], [3, 2]], [[2, 2], [1, 3]]), [[1, 4], [2, 3], [3, 2]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(mergeitems([[1, 2], [2, 3], [1, 3], [3, 2]], [[3, 1], [1, 3]]), [[1, 8], [2, 3], [3, 3]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(mergeitems([[1, 1], [2, 2], [3, 3]], [[2, 3], [2, 4]]), [[1, 1], [2, 9], [3, 3]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-263/roger-bell-west/kotlin/ch-1.kt b/challenge-263/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..3816c85104 --- /dev/null +++ b/challenge-263/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,33 @@ +fun targetindex(a0: List<Int>, k: Int): List<Int> { + var a = ArrayList(a0).sorted() + var r = ArrayList<Int>() + a.forEachIndexed{i, v -> + if (v == k) { + r += i + } + } + return r.toList() +} + +fun main() { + + if (targetindex(listOf(1, 5, 3, 2, 4, 2), 2) == listOf(1, 2)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (targetindex(listOf(1, 2, 4, 3, 5), 6) == emptyList<Int>()) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (targetindex(listOf(5, 3, 2, 4, 2, 1), 4) == listOf(4)) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-263/roger-bell-west/kotlin/ch-2.kt b/challenge-263/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..bddfcbd82b --- /dev/null +++ b/challenge-263/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,33 @@ +fun mergeitems(a: List<List<Int>>, b: List<List<Int>>): List<List<Int>> { + var c = mutableMapOf<Int, Int>().withDefault({0}) + for (v in listOf(a, b)) { + for (w in v) { + c.set(w[0], c.getValue(w[0]) + w[1]) + } + } + var k = c.keys.toList().sorted() + return k.map { listOf(it, c.getValue(it)) }.toList() +} + +fun main() { + + if (mergeitems(listOf(listOf(1, 1), listOf(2, 1), listOf(3, 2)), listOf(listOf(2, 2), listOf(1, 3))) == listOf(listOf(1, 4), listOf(2, 3), listOf(3, 2))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (mergeitems(listOf(listOf(1, 2), listOf(2, 3), listOf(1, 3), listOf(3, 2)), listOf(listOf(3, 1), listOf(1, 3))) == listOf(listOf(1, 8), listOf(2, 3), listOf(3, 3))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (mergeitems(listOf(listOf(1, 1), listOf(2, 2), listOf(3, 3)), listOf(listOf(2, 3), listOf(2, 4))) == listOf(listOf(1, 1), listOf(2, 9), listOf(3, 3))) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-263/roger-bell-west/lua/ch-1.lua b/challenge-263/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..4ac81e1420 --- /dev/null +++ b/challenge-263/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,64 @@ +#! /usr/bin/lua + +function targetindex(a0, k) + local a = a0 + table.sort(a0) + local r = {} + for i, v in ipairs(a) do + if v == k then + table.insert(r, i) + end + end + return r +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(targetindex({1, 5, 3, 2, 4, 2}, 2), {1, 2}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(targetindex({1, 2, 4, 3, 5}, 6), {}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(targetindex({5, 3, 2, 4, 2, 1}, 4), {4}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-263/roger-bell-west/lua/ch-2.lua b/challenge-263/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..3c0188c1b1 --- /dev/null +++ b/challenge-263/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,74 @@ +#! /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 mergeitems(a, b) + local c = {} + for i, v in ipairs({a, b}) do + for j, w in ipairs(v) do + if c[w[1]] == nil then + c[w[1]] = 0 + end + c[w[1]] = c[w[1]] + w[2] + end + end + local k = {} + for t, _ in pairs(c) do + table.insert(k, t) + end + table.sort(k) + local out = {} + for _, i in ipairs(k) do + table.insert(out, {i, c[i]}) + end + return out +end + +if recursive_compare(mergeitems({{1, 1}, {2, 1}, {3, 2}}, {{2, 2}, {1, 3}}), {{1, 4}, {2, 3}, {3, 2}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(mergeitems({{1, 2}, {2, 3}, {1, 3}, {3, 2}}, {{3, 1}, {1, 3}}), {{1, 8}, {2, 3}, {3, 3}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(mergeitems({{1, 1}, {2, 2}, {3, 3}}, {{2, 3}, {2, 4}}), {{1, 1}, {2, 9}, {3, 3}}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-263/roger-bell-west/perl/ch-1.pl b/challenge-263/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..7d8f6e9a13 --- /dev/null +++ b/challenge-263/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,16 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is_deeply(targetindex([1, 5, 3, 2, 4, 2], 2), [1, 2], 'example 1'); +is_deeply(targetindex([1, 2, 4, 3, 5], 6), [], 'example 2'); +is_deeply(targetindex([5, 3, 2, 4, 2, 1], 4), [4], 'example 3'); + +sub targetindex($a0, $k) { + my @a = sort {$a <=> $b} @{$a0}; + return [grep { $a[$_] == $k } (0 .. $#a)]; +} diff --git a/challenge-263/roger-bell-west/perl/ch-2.pl b/challenge-263/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..00c2c7daa4 --- /dev/null +++ b/challenge-263/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,21 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is_deeply(mergeitems([[1, 1], [2, 1], [3, 2]], [[2, 2], [1, 3]]), [[1, 4], [2, 3], [3, 2]], 'example 1'); +is_deeply(mergeitems([[1, 2], [2, 3], [1, 3], [3, 2]], [[3, 1], [1, 3]]), [[1, 8], [2, 3], [3, 3]], 'example 2'); +is_deeply(mergeitems([[1, 1], [2, 2], [3, 3]], [[2, 3], [2, 4]]), [[1, 1], [2, 9], [3, 3]], 'example 3'); + +sub mergeitems($aa, $bb) { + my %c; + foreach my $v ($aa, $bb) { + foreach my $w (@{$v}) { + $c{$w->[0]} += $w->[1]; + } + } + return [map {[$_, $c{$_}]} sort {$a <=> $b} keys %c]; +} diff --git a/challenge-263/roger-bell-west/postscript/ch-1.ps b/challenge-263/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..c59835b7f0 --- /dev/null +++ b/challenge-263/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,235 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 + +/quicksort { + { quicksort.cmp } quicksort.with_comparator +} 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 + +/filter { % array proc(bool) -> array + 1 dict begin + /p exch def + [ exch + { + dup p not + { + pop + } if + } forall + ] + 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 + +/quicksort.partition { + 3 dict begin + /pivot arr hi lo add 2 idiv get def + /i lo 1 sub def + /j hi 1 add def + { + { + /i i 1 add def + arr i get pivot cmp 0 ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot cmp 0 le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + end +} bind def + +/quicksort.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.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.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.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} 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 + +/targetindex { + 0 dict begin + /k exch def + quicksort + enumerate.array + { 1 get k eq } filter + { 0 get } map + end +} bind def + +(targetindex) test.start +[1 5 3 2 4 2] 2 targetindex [1 2] deepeq test +[1 2 4 3 5] 6 targetindex [] deepeq test +[5 3 2 4 2 1] 4 targetindex [4] deepeq test +test.end diff --git a/challenge-263/roger-bell-west/postscript/ch-2.ps b/challenge-263/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..fd05316836 --- /dev/null +++ b/challenge-263/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,225 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/quicksort.partition { + 3 dict begin + /pivot arr hi lo add 2 idiv get def + /i lo 1 sub def + /j hi 1 add def + { + { + /i i 1 add def + arr i get pivot cmp 0 ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot cmp 0 le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + end +} bind def + +/quicksort.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 + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} 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 + +/dget { + 3 1 roll + 2 copy + known { + get exch pop + } { + pop pop + } 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 + +/deepeq { + 2 dict begin + /a exch def + /b exch def + a type b type eq { + a type /dicttype eq { + a length b length eq { + << + a { + pop + true + } forall + b { + pop + true + } forall + >> + true exch + { + pop + dup a exch known { + dup b exch known { + dup a exch get exch b exch get deepeq not { + pop false + } if + } { + false + } ifelse + } { + false + } ifelse + } forall + } { + false + } ifelse + } { + a type dup /arraytype eq exch /stringtype eq or { + a length b length eq { + true + 0 1 a length 1 sub { + dup a exch get exch b exch get deepeq not { + pop false + exit + } if + } for + } { + false + } ifelse + } { + a b eq + } ifelse + } ifelse + } { + false + } ifelse + end +} bind def + +/quicksort { + { quicksort.cmp } quicksort.with_comparator +} 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 + +/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 + +/quicksort.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} bind def + + +% end included library code + +/mergeitems { + 0 dict begin + /c 0 dict def + 2 { + { + aload pop 1 index + c exch 0 dget add + c 3 1 roll put + } forall + } repeat + [ + c keys quicksort { + [ exch dup c exch get ] + } forall + ] + end +} bind def + +(mergeitems) test.start +[[1 1] [2 1] [3 2]] [[2 2] [1 3]] mergeitems [[1 4] [2 3] [3 2]] deepeq test +[[1 2] [2 3] [1 3] [3 2]] [[3 1] [1 3]] mergeitems [[1 8] [2 3] [3 3]] deepeq test +[[1 1] [2 2] [3 3]] [[2 3] [2 4]] mergeitems [[1 1] [2 9] [3 3]] deepeq test +test.end diff --git a/challenge-263/roger-bell-west/python/ch-1.py b/challenge-263/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..1cdedcf720 --- /dev/null +++ b/challenge-263/roger-bell-west/python/ch-1.py @@ -0,0 +1,21 @@ +#! /usr/bin/python3 + +def targetindex(a0, k): + a = a0 + a.sort() + return [i for i in range(len(a)) if a[i] == k] + +import unittest + +class TestTargetindex(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(targetindex([1, 5, 3, 2, 4, 2], 2), [1, 2], 'example 1') + + def test_ex2(self): + self.assertEqual(targetindex([1, 2, 4, 3, 5], 6), [], 'example 2') + + def test_ex3(self): + self.assertEqual(targetindex([5, 3, 2, 4, 2, 1], 4), [4], 'example 3') + +unittest.main() diff --git a/challenge-263/roger-bell-west/python/ch-2.py b/challenge-263/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..751c979f07 --- /dev/null +++ b/challenge-263/roger-bell-west/python/ch-2.py @@ -0,0 +1,28 @@ +#! /usr/bin/python3 + +from collections import defaultdict + +def mergeitems(a, b): + c = defaultdict(lambda: 0) + for v in [a, b]: + for w in v: + c[w[0]] += w[1] + k = list(c.keys()) + k.sort() + return [[i, c[i]] for i in k] + + +import unittest + +class TestMergeitems(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(mergeitems([[1, 1], [2, 1], [3, 2]], [[2, 2], [1, 3]]), [[1, 4], [2, 3], [3, 2]], 'example 1') + + def test_ex2(self): + self.assertEqual(mergeitems([[1, 2], [2, 3], [1, 3], [3, 2]], [[3, 1], [1, 3]]), [[1, 8], [2, 3], [3, 3]], 'example 2') + + def test_ex3(self): + self.assertEqual(mergeitems([[1, 1], [2, 2], [3, 3]], [[2, 3], [2, 4]]), [[1, 1], [2, 9], [3, 3]], 'example 3') + +unittest.main() diff --git a/challenge-263/roger-bell-west/raku/ch-1.p6 b/challenge-263/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..17316df461 --- /dev/null +++ b/challenge-263/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,14 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is-deeply(targetindex([1, 5, 3, 2, 4, 2], 2), [1, 2], 'example 1'); +is-deeply(targetindex([1, 2, 4, 3, 5], 6), [], 'example 2'); +is-deeply(targetindex([5, 3, 2, 4, 2, 1], 4), [4], 'example 3'); + +sub targetindex(@a0, $k) { + my @a = @a0.sort({$^a <=> $^b}); + return [(0 .. @a.end).grep({@a[$_] == $k})]; +} diff --git a/challenge-263/roger-bell-west/raku/ch-2.p6 b/challenge-263/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..bee532bb60 --- /dev/null +++ b/challenge-263/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,19 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is-deeply(mergeitems([[1, 1], [2, 1], [3, 2]], [[2, 2], [1, 3]]), [[1, 4], [2, 3], [3, 2]], 'example 1'); +is-deeply(mergeitems([[1, 2], [2, 3], [1, 3], [3, 2]], [[3, 1], [1, 3]]), [[1, 8], [2, 3], [3, 3]], 'example 2'); +is-deeply(mergeitems([[1, 1], [2, 2], [3, 3]], [[2, 3], [2, 4]]), [[1, 1], [2, 9], [3, 3]], 'example 3'); + +sub mergeitems(@a, @b) { + my %c; + for (@a, @b) -> @v { + for @v -> @w { + %c{@w[0]} += @w[1]; + } + } + return Array(%c.keys.sort({$^a <=> $^b}).map({[0 + $_, %c{$_}]})); +} diff --git a/challenge-263/roger-bell-west/ruby/ch-1.rb b/challenge-263/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..fbc9ac0a25 --- /dev/null +++ b/challenge-263/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,30 @@ +#! /usr/bin/ruby + +def targetindex(a0, k) + a = a0.sort + out = [] + a.each_with_index do |v, i| + if v == k then + out.push(i) + end + end + return out +end + +require 'test/unit' + +class TestTargeti |
