diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-12-19 18:59:55 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-19 18:59:55 +0000 |
| commit | a63659ade07f1b118958c7896ebf220b0e766eea (patch) | |
| tree | 6205317c25080b7e5f14d32c42be31d7514639d1 /challenge-248 | |
| parent | 60c8fe6ec139ec65766bc30e41b50c2582ddf244 (diff) | |
| parent | e191292658268e684b8dad27ad70a2b0d8233f5b (diff) | |
| download | perlweeklychallenge-club-a63659ade07f1b118958c7896ebf220b0e766eea.tar.gz perlweeklychallenge-club-a63659ade07f1b118958c7896ebf220b0e766eea.tar.bz2 perlweeklychallenge-club-a63659ade07f1b118958c7896ebf220b0e766eea.zip | |
Merge pull request #9267 from Firedrake/rogerbw-challenge-248
RogerBW solutions for challenge no. 248
Diffstat (limited to 'challenge-248')
21 files changed, 1155 insertions, 0 deletions
diff --git a/challenge-248/roger-bell-west/javascript/ch-1.js b/challenge-248/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..5a6743bd17 --- /dev/null +++ b/challenge-248/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,72 @@ +#! /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 shortestdistance(a, c) { + let q = []; + let i = 0; + while (i >= 0) { + const p = a.indexOf(c, i); + if (p >= 0) { + q.push([p, 0]); + i = p + 1; + } else { + i = -1; + } + } + const invalid = a.length + 1; + let out = Array(a.length).fill().map((element, index) => invalid); + while (q.length > 0) { + let [i, v] = q.shift(); + if (out[i] == invalid) { + out[i] = v; + if (i > 0) { + q.push([i - 1, v + 1]); + } + if (i < a.length - 1) { + q.push([i + 1, v + 1]); + } + } + } + return out; +} + +if (deepEqual(shortestdistance('loveleetcode', 'e'), [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(shortestdistance('aaab', 'b'), [3, 2, 1, 0])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-248/roger-bell-west/javascript/ch-2.js b/challenge-248/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..268838ce20 --- /dev/null +++ b/challenge-248/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,61 @@ +#! /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 submatrixsum(a) { + let out = []; + for (let y = 0; y < a.length - 1; y++) { + let row = []; + for (let x = 0; x < a[y].length - 1; x++) { + let s = 0; + for (let ya = y; ya <= y + 1; ya++) { + for (let xa = x; xa <= x + 1; xa++) { + s += a[ya][xa]; + } + } + row.push(s); + } + out.push(row); + } + return out; +} + +if (deepEqual(submatrixsum([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]), [[14, 18, 22], [30, 34, 38]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(submatrixsum([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]), [[2, 1, 0], [1, 2, 1], [0, 1, 2]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-248/roger-bell-west/kotlin/ch-1.kt b/challenge-248/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..f1d671b922 --- /dev/null +++ b/challenge-248/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,47 @@ +fun shortestdistance(a: String, c: Char): List<Int> { + var q = ArrayDeque<Pair<Int, Int>>() + var j = 0 + while (j >= 0) { + val p = a.indexOf(c, j) + if (p >= 0) { + q.add(Pair(p, 0)) + j = p + 1 + } else { + j = -1 + } + } + val invalid = a.length + 1 + var out = ArrayList(generateSequence(invalid) { invalid }.take(a.length).toList()) + while (q.size > 0) { + val pp = q.removeFirst() + val i = pp.first + val v = pp.second + if (out[i] == invalid) { + out[i] = v + if (i > 0) { + q.add(Pair(i - 1, v + 1)) + } + if (i < a.length - 1) { + q.add(Pair(i + 1, v + 1)) + } + } + } + return out.toList() +} + + fun main() { + + if (shortestdistance("loveleetcode", 'e') == listOf(3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (shortestdistance("aaab", 'b') == listOf(3, 2, 1, 0)) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-248/roger-bell-west/kotlin/ch-2.kt b/challenge-248/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..21d8e42bbb --- /dev/null +++ b/challenge-248/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,34 @@ +fun submatrixsum(a: List<List<Int>>): List<List<Int>> { + var out = ArrayList<List<Int>>() + for (y in 0 .. a.size - 2) { + var row = ArrayList<Int>() + for (x in 0 .. a[0].size - 2) { + var s = 0 + for (ya in y .. y + 1) { + for (xa in x .. x + 1) { + s += a[ya][xa] + } + } + row.add(s) + } + out.add(row.toList()) + } + return out.toList() +} + +fun main() { + + if (submatrixsum(listOf(listOf(1, 2, 3, 4), listOf(5, 6, 7, 8), listOf(9, 10, 11, 12))) == listOf(listOf(14, 18, 22), listOf(30, 34, 38))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (submatrixsum(listOf(listOf(1, 0, 0, 0), listOf(0, 1, 0, 0), listOf(0, 0, 1, 0), listOf(0, 0, 0, 1))) == listOf(listOf(2, 1, 0), listOf(1, 2, 1), listOf(0, 1, 2))) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-248/roger-bell-west/lua/ch-1.lua b/challenge-248/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..fac8c639ae --- /dev/null +++ b/challenge-248/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,77 @@ +#! /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 shortestdistance(a, c) + local q = {} + local i = 1 + while i >= 1 do + local p = string.find(a, c, i, true) + if p then + table.insert(q, {p, 0}) + i = p + 1 + else + i = 0 + end + end + local invalid = #a + 1 + local out = {} + for i = 1, #a do + out[i] = invalid + end + while (#q > 0) do + local i, v = table.remove(q, 1) + if out[i] == invalid then + out[i] = v + if i > 1 then + table.insert(q, {i - 1, v + 1}) + end + if i < #a then + table.insert(q, {i + 1, v + 1}) + end + end + end + return out +end + +if recursive_compare(shortestdistance("loveleetcode", "e"), {3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(shortestdistance("aaab", "b"), {3, 2, 1, 0}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-248/roger-bell-west/lua/ch-2.lua b/challenge-248/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..8cafa55155 --- /dev/null +++ b/challenge-248/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,63 @@ +#! /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 submatrixsum(a) + local out = {} + for x = 1, #a - 1 do + local row = {} + for y = 1, #(a[x])-1 do + local s = 0 + for ya = y, y + 1 do + for xa = x, x + 1 do + s = s + a[y][x] + end + end + table.insert(row, s) + end + table.insert(out, row) + end + return out +end + +if recursive_compare(submatrixsum({{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}), {{14, 18, 22}, {30, 34, 38}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(submatrixsum({{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}), {{2, 1, 0}, {1, 2, 1}, {0, 1, 2}}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-248/roger-bell-west/perl/ch-1.pl b/challenge-248/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..57e51877bf --- /dev/null +++ b/challenge-248/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,39 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is_deeply(shortestdistance('loveleetcode', 'e'), [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0], 'example 1'); +is_deeply(shortestdistance('aaab', 'b'), [3, 2, 1, 0], 'example 2'); + +sub shortestdistance($a, $c) { + my @q; + my $i = 0; + while ($i >= 0) { + my $p = index($a, $c, $i); + if ($p > -1) { + push @q, [$p, 0]; + $i = $p + 1; + } else { + $i = -1; + } + } + my $invalid = length($a) + 1; + my @out = ($invalid) x length($a); + while (scalar @q) { + my ($i, $v) = @{shift @q}; + if ($out[$i] == $invalid) { + $out[$i] = $v; + if ($i > 0) { + push(@q, [$i - 1, $v + 1]); + } + if ($i < length($a) - 1) { + push(@q, [$i + 1, $v + 1]); + } + } + } + return \@out; +} diff --git a/challenge-248/roger-bell-west/perl/ch-2.pl b/challenge-248/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..0c958525c8 --- /dev/null +++ b/challenge-248/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,28 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is_deeply(submatrixsum([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]), [[14, 18, 22], [30, 34, 38]], 'example 1'); +is_deeply(submatrixsum([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]), [[2, 1, 0], [1, 2, 1], [0, 1, 2]], 'example 2'); + +sub submatrixsum($a) { + my @out; + foreach my $y (0 .. $#{$a} - 1) { + my @row; + foreach my $x (0 .. $#{$a->[$y]} - 1) { + my $s = 0; + foreach my $ya ($y, $y + 1) { + foreach my $xa ($x, $x + 1) { + $s += $a->[$ya][$xa]; + } + } + push @row, $s; + } + push @out, \@row; + } + return \@out; +} diff --git a/challenge-248/roger-bell-west/postscript/ch-1.ps b/challenge-248/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..0876cff3be --- /dev/null +++ b/challenge-248/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,179 @@ +%!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 + +/apop.left { % [a b c] -> [b c] a + dup 0 get exch + [ exch aload length -1 roll pop ] exch +} 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 + +/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 + +/map { % array proc -> array + 2 dict begin + /p exch def + [ exch + { + p + } forall + ] + end +} bind def + +/apush.right { % [a b] c -> [a b c] + exch + [ exch aload length 2 add -1 roll ] +} bind def + +/s2a { + [ exch { } forall ] +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + + +% end included library code + +/shortestdistance { + 0 dict begin + /c exch s2a 0 get def + /a exch s2a def + /i 0 def + /q a enumerate.array { 1 get c eq } filter { 0 get [ exch 0 ] } map def + /invalid a length 1 add def + /out [ a length { invalid } repeat ] def + { + q length 0 eq { + exit + } if + q apop.left + aload pop /v exch def /i exch def + /q exch def + out i get invalid eq { + out i v put + i 0 gt { + /q q [ i 1 sub v 1 add ] apush.right def + } if + i a length 1 sub lt { + /q q [ i 1 add v 1 add ] apush.right def + } if + } if + } loop + out + end +} bind def + +(shortestdistance) test.start +(loveleetcode) (e) shortestdistance [3 2 1 0 1 0 0 1 2 2 1 0] deepeq test +(aaab) (b) shortestdistance [3 2 1 0] deepeq test +test.end diff --git a/challenge-248/roger-bell-west/postscript/ch-2.ps b/challenge-248/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..a96973fb7c --- /dev/null +++ b/challenge-248/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,125 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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.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 { + /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 + +/submatrixsum { + 0 dict begin + /a exch def + [ + 0 1 a length 2 sub { + /y exch def + [ + 0 1 a y get length 2 sub { + /x exch def + /s 0 def + [ y dup 1 add ] { + /ya exch def + [ x dup 1 add ] { + /xa exch def + /s s a ya get xa get add def + } forall + } forall + s + } for + ] + } for + ] + end +} bind def + +(submatrixsum) test.start +[[1 2 3 4] [5 6 7 8] [9 10 11 12]] submatrixsum [[14 18 22] [30 34 38]] deepeq test +[[1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]] submatrixsum [[2 1 0] [1 2 1] [0 1 2]] deepeq test +test.end diff --git a/challenge-248/roger-bell-west/python/ch-1.py b/challenge-248/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..b1a4c7491f --- /dev/null +++ b/challenge-248/roger-bell-west/python/ch-1.py @@ -0,0 +1,37 @@ +#! /usr/bin/python3 + +from collections import deque + +def shortestdistance(a, c): + q = deque() + i = 0 + while i >= 0: + p = a.find(c, i) + if p >= 0: + q.append([p, 0]) + i = p + 1 + else: + i = -1 + invalid = len(a) + 1 + out = len(a) * [invalid] + while len(q) > 0: + i, v = q.popleft() + if out[i] == invalid: + out[i] = v + if i > 0: + q.append([i - 1, v + 1]) + if i < len(a) - 1: + q.append([i + 1, v + 1]) + return out + +import unittest + +class TestShortestdistance(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(shortestdistance("loveleetcode", "e"), [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0], 'example 1') + + def test_ex2(self): + self.assertEqual(shortestdistance("aaab", "b"), [3, 2, 1, 0], 'example 2') + +unittest.main() diff --git a/challenge-248/roger-bell-west/python/ch-2.py b/challenge-248/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..5d21ce17dd --- /dev/null +++ b/challenge-248/roger-bell-west/python/ch-2.py @@ -0,0 +1,26 @@ +#! /usr/bin/python3 + +def submatrixsum(a): + out = [] + for y in range(len(a) - 1): + row = [] + for x in range(len(a[y]) - 1): + s = 0 + for ya in [y, y+1]: + for xa in [x, x+1]: + s += a[ya][xa] + row.append(s) + out.append(row) + return out + +import unittest + +class TestSubmatrixsum(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(submatrixsum([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]), [[14, 18, 22], [30, 34, 38]], 'example 1') + + def test_ex2(self): + self.assertEqual(submatrixsum([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]), [[2, 1, 0], [1, 2, 1], [0, 1, 2]], 'example 2') + +unittest.main() diff --git a/challenge-248/roger-bell-west/raku/ch-1.p6 b/challenge-248/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..dc7b130510 --- /dev/null +++ b/challenge-248/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,36 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is-deeply(shortestdistance('loveleetcode', 'e'), [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0], 'example 1'); +is-deeply(shortestdistance('aaab', 'b'), [3, 2, 1, 0], 'example 2'); + +sub shortestdistance($a, $c) { + my @q; + my $i = 0; + while (True) { + with ($a.index($c, $i)) { + @q.push(($_, 0)); + $i = $_ + 1; + } else { + last; + } + } + my $invalid = $a.chars + 1; + my @out = $invalid xx $a.chars(); + while (@q.elems > 0) { + my ($i, $v) = @q.shift; + if (@out[$i] == $invalid) { + @out[$i] = $v; + if ($i > 0) { + @q.push(($i - 1, $v + 1)); + } + if ($i < $a.chars - 1) { + @q.push(($i + 1, $v + 1)); + } + } + } + return @out; +} diff --git a/challenge-248/roger-bell-west/raku/ch-2.p6 b/challenge-248/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..0dade3df2e --- /dev/null +++ b/challenge-248/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,26 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is-deeply(submatrixsum([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]), [[14, 18, 22], [30, 34, 38]], 'example 1'); +is-deeply(submatrixsum([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]), [[2, 1, 0], [1, 2, 1], [0, 1, 2]], 'example 2'); + +sub submatrixsum(@a) { + my @out; + for (0 .. @a.end - 1) -> $y { + my @row; + for (0 .. @a[$y].end - 1) -> $x { + my $s = 0; + for ($y .. $y + 1) -> $ya { + for ($x .. $x + 1) -> $xa { + $s += @a[$ya][$xa]; + } + } + @row.push($s); + } + @out.push(@row) + } + return @out; +} diff --git a/challenge-248/roger-bell-west/ruby/ch-1.rb b/challenge-248/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..9d54ab7123 --- /dev/null +++ b/challenge-248/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,44 @@ +#! /usr/bin/ruby + +def shortestdistance(a, c) + q = [] + i = 0 + while i >= 0 do + p = a.index(c, i) + if p then + q.push([p, 0]) + i = p + 1 + else + i = -1 + end + end + invalid = a.length + 1 + out = Array.new(a.length) {|i| invalid} + while q.length > 0 do + i, v = q.shift + if out[i] == invalid then + out[i] = v + if i > 0 then + q.push([i - 1, v + 1]) + end + if i < a.length - 1 then + q.push([i + 1, v + 1]) + end + end + end + return out +end + +require 'test/unit' + +class TestShortestdistance < Test::Unit::TestCase + + def test_ex1 + assert_equal([3, 2, 1, 0, 1, 0, |
