diff options
| author | Roger Bell_West <roger@firedrake.org> | 2025-08-12 13:04:56 +0100 |
|---|---|---|
| committer | Roger Bell_West <roger@firedrake.org> | 2025-08-12 13:04:56 +0100 |
| commit | a168874a1ccc29672937dd3ae40ba4e63eb34fb9 (patch) | |
| tree | 270f1babfc138f7a9b4cb018f9ff9ec58ca40795 | |
| parent | 730e172acb159a2fc320a78a6250083328ef1067 (diff) | |
| download | perlweeklychallenge-club-a168874a1ccc29672937dd3ae40ba4e63eb34fb9.tar.gz perlweeklychallenge-club-a168874a1ccc29672937dd3ae40ba4e63eb34fb9.tar.bz2 perlweeklychallenge-club-a168874a1ccc29672937dd3ae40ba4e63eb34fb9.zip | |
RogerBW solutions for challenge no. 334
25 files changed, 1050 insertions, 0 deletions
diff --git a/challenge-334/roger-bell-west/crystal/ch-1.cr b/challenge-334/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..7e7237e2b3 --- /dev/null +++ b/challenge-334/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,24 @@ +#! /usr/bin/crystal + +def rangesum(a, s, e) + a[s .. e].sum +end + +require "spec" +describe "rangesum" do + it "test_ex1" do + rangesum([-2, 0, 3, -5, 2, -1], 0, 2).should eq 1 + end + it "test_ex2" do + rangesum([1, -2, 3, -4, 5], 1, 3).should eq -3 + end + it "test_ex3" do + rangesum([1, 0, 2, -1, 3], 3, 4).should eq 2 + end + it "test_ex4" do + rangesum([-5, 4, -3, 2, -1, 0], 0, 3).should eq -2 + end + it "test_ex5" do + rangesum([-1, 0, 2, -3, -2, 1], 0, 2).should eq 1 + end +end diff --git a/challenge-334/roger-bell-west/crystal/ch-2.cr b/challenge-334/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..dcb25372fa --- /dev/null +++ b/challenge-334/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,35 @@ +#! /usr/bin/crystal + +def nearestvalidpoint(x, y, points) + ix = -1 + minmhd = -1 + points.each_with_index do |p, i| + if p[0] == x || p[1] == y + mhd = (p[0] - x).abs() + (p[1] - y).abs() + if minmhd == -1 || mhd < minmhd + minmhd = mhd + ix = i + end + end + end + ix +end + +require "spec" +describe "nearestvalidpoint" do + it "test_ex1" do + nearestvalidpoint(3, 4, [[1, 2], [3, 1], [2, 4], [2, 3]]).should eq 2 + end + it "test_ex2" do + nearestvalidpoint(2, 5, [[3, 4], [2, 3], [1, 5], [2, 5]]).should eq 3 + end + it "test_ex3" do + nearestvalidpoint(1, 1, [[2, 2], [3, 3], [4, 4]]).should eq -1 + end + it "test_ex4" do + nearestvalidpoint(0, 0, [[0, 1], [1, 0], [0, 2], [2, 0]]).should eq 0 + end + it "test_ex5" do + nearestvalidpoint(5, 5, [[5, 6], [6, 5], [5, 4], [4, 5]]).should eq 0 + end +end diff --git a/challenge-334/roger-bell-west/javascript/ch-1.js b/challenge-334/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..5c45371820 --- /dev/null +++ b/challenge-334/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,38 @@ +#! /usr/bin/node + +"use strict" + +function rangesum(a, s, e) { + return a.slice(s, e + 1).reduce((x, y) => x + y); +} + +if (rangesum([-2, 0, 3, -5, 2, -1], 0, 2) == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (rangesum([1, -2, 3, -4, 5], 1, 3) == -3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (rangesum([1, 0, 2, -1, 3], 3, 4) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (rangesum([-5, 4, -3, 2, -1, 0], 0, 3) == -2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (rangesum([-1, 0, 2, -3, -2, 1], 0, 2) == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-334/roger-bell-west/javascript/ch-2.js b/challenge-334/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..158e76469f --- /dev/null +++ b/challenge-334/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,49 @@ +#! /usr/bin/node + +"use strict" + +function nearestvalidpoint(x, y, points) { + let ix = -1; + let minmhd = -1; + points.forEach((p, i) => { + if (p[0] == x || p[1] == y) { + const mhd = Math.abs(p[0] - x) + Math.abs(p[1] - y); + if (minmhd == -1 || mhd < minmhd) { + minmhd = mhd; + ix = i; + } + } + }); + return ix; +} + +if (nearestvalidpoint(3, 4, [[1, 2], [3, 1], [2, 4], [2, 3]]) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (nearestvalidpoint(2, 5, [[3, 4], [2, 3], [1, 5], [2, 5]]) == 3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (nearestvalidpoint(1, 1, [[2, 2], [3, 3], [4, 4]]) == -1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (nearestvalidpoint(0, 0, [[0, 1], [1, 0], [0, 2], [2, 0]]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (nearestvalidpoint(5, 5, [[5, 6], [6, 5], [5, 4], [4, 5]]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-334/roger-bell-west/kotlin/ch-1.kt b/challenge-334/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..ed708f2fa8 --- /dev/null +++ b/challenge-334/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,38 @@ +fun rangesum(a: List<Int>, s: Int, e: Int): Int { + return a.slice(s .. e).sum() +} + +fun main() { + + if (rangesum(listOf(-2, 0, 3, -5, 2, -1), 0, 2) == 1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (rangesum(listOf(1, -2, 3, -4, 5), 1, 3) == -3) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (rangesum(listOf(1, 0, 2, -1, 3), 3, 4) == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (rangesum(listOf(-5, 4, -3, 2, -1, 0), 0, 3) == -2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (rangesum(listOf(-1, 0, 2, -3, -2, 1), 0, 2) == 1) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-334/roger-bell-west/kotlin/ch-2.kt b/challenge-334/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..c0014fc9cd --- /dev/null +++ b/challenge-334/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,49 @@ +fun nearestvalidpoint(x: Int, y: Int, points: List<List<Int>>): Int { + var ix = -1 + var minmhd = -1 + points.forEachIndexed {i, p -> + if (p[0] == x || p[1] == y) { + val mhd = Math.abs(p[0] - x) + Math.abs(p[1] - y) + if (minmhd == -1 || mhd < minmhd) { + minmhd = mhd + ix = i + } + } + } + return ix +} + +fun main() { + + if (nearestvalidpoint(3, 4, listOf(listOf(1, 2), listOf(3, 1), listOf(2, 4), listOf(2, 3))) == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (nearestvalidpoint(2, 5, listOf(listOf(3, 4), listOf(2, 3), listOf(1, 5), listOf(2, 5))) == 3) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (nearestvalidpoint(1, 1, listOf(listOf(2, 2), listOf(3, 3), listOf(4, 4))) == -1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (nearestvalidpoint(0, 0, listOf(listOf(0, 1), listOf(1, 0), listOf(0, 2), listOf(2, 0))) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (nearestvalidpoint(5, 5, listOf(listOf(5, 6), listOf(6, 5), listOf(5, 4), listOf(4, 5))) == 0) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-334/roger-bell-west/lua/ch-1.lua b/challenge-334/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..0d08107659 --- /dev/null +++ b/challenge-334/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,45 @@ +#! /usr/bin/lua + +function rangesum(a, s, e) + local t = 0 + for i = s + 1, e + 1 do + t = t + a[i] + end + return t +end + +if rangesum({-2, 0, 3, -5, 2, -1}, 0, 2) == 1 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if rangesum({1, -2, 3, -4, 5}, 1, 3) == -3 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if rangesum({1, 0, 2, -1, 3}, 3, 4) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if rangesum({-5, 4, -3, 2, -1, 0}, 0, 3) == -2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if rangesum({-1, 0, 2, -3, -2, 1}, 0, 2) == 1 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-334/roger-bell-west/lua/ch-2.lua b/challenge-334/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..bfcc219437 --- /dev/null +++ b/challenge-334/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,52 @@ +#! /usr/bin/lua + +function nearestvalidpoint(x, y, points) + local ix = -1 + local minmhd = -1 + for i, p in ipairs(points) do + if p[1] == x or p[2] == y then + local mhd = math.abs(p[1] - x) + math.abs(p[2] - y) + if minmhd == -1 or mhd < minmhd then + minmhd = mhd + ix = i - 1 + end + end + end + return ix +end + +if nearestvalidpoint(3, 4, {{1, 2}, {3, 1}, {2, 4}, {2, 3}}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if nearestvalidpoint(2, 5, {{3, 4}, {2, 3}, {1, 5}, {2, 5}}) == 3 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if nearestvalidpoint(1, 1, {{2, 2}, {3, 3}, {4, 4}}) == -1 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if nearestvalidpoint(0, 0, {{0, 1}, {1, 0}, {0, 2}, {2, 0}}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if nearestvalidpoint(5, 5, {{5, 6}, {6, 5}, {5, 4}, {4, 5}}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-334/roger-bell-west/perl/ch-1.pl b/challenge-334/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..dec433e5f8 --- /dev/null +++ b/challenge-334/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,19 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is(rangesum([-2, 0, 3, -5, 2, -1], 0, 2), 1, 'example 1'); +is(rangesum([1, -2, 3, -4, 5], 1, 3), -3, 'example 2'); +is(rangesum([1, 0, 2, -1, 3], 3, 4), 2, 'example 3'); +is(rangesum([-5, 4, -3, 2, -1, 0], 0, 3), -2, 'example 4'); +is(rangesum([-1, 0, 2, -3, -2, 1], 0, 2), 1, 'example 5'); + +use List::Util qw(sum); + +sub rangesum($a, $s, $e) { + sum(@$a[$s .. $e]); +} diff --git a/challenge-334/roger-bell-west/perl/ch-2.pl b/challenge-334/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..87f359a455 --- /dev/null +++ b/challenge-334/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 => 5; + +is(nearestvalidpoint(3, 4, [[1, 2], [3, 1], [2, 4], [2, 3]]), 2, 'example 1'); +is(nearestvalidpoint(2, 5, [[3, 4], [2, 3], [1, 5], [2, 5]]), 3, 'example 2'); +is(nearestvalidpoint(1, 1, [[2, 2], [3, 3], [4, 4]]), -1, 'example 3'); +is(nearestvalidpoint(0, 0, [[0, 1], [1, 0], [0, 2], [2, 0]]), 0, 'example 4'); +is(nearestvalidpoint(5, 5, [[5, 6], [6, 5], [5, 4], [4, 5]]), 0, 'example 5'); + +sub nearestvalidpoint($x, $y, $points) { + my $ix = -1; + my $minmhd = -1; + while (my ($i, $p) = each @{$points}) { + if ($p->[0] == $x || $p->[1] == $y) { + my $mhd = abs($p->[0] - $x) + abs($p->[1] - $y); + if ($minmhd == -1 || $mhd < $minmhd) { + $minmhd = $mhd; + $ix = $i; + } + } + } + $ix; +} diff --git a/challenge-334/roger-bell-west/postscript/ch-1.ps b/challenge-334/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..5bc6b9654b --- /dev/null +++ b/challenge-334/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,67 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 + +/reduce { % array proc -> value + 2 dict begin + /p exch def + /a exch def + a 0 get + 1 1 a length 1 sub { + a exch get + p + } 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 + + +% end included library code + +/rangesum { + exch + dup + 3 1 roll + sub + 1 add + getinterval + { add } reduce +} bind def + +(rangesum) test.start +[-2 0 3 -5 2 -1] 0 2 rangesum 1 eq test +[1 -2 3 -4 5] 1 3 rangesum -3 eq test +[1 0 2 -1 3] 3 4 rangesum 2 eq test +[-5 4 -3 2 -1 0] 0 3 rangesum -2 eq test +[-1 0 2 -3 -2 1] 0 2 rangesum 1 eq test +test.end diff --git a/challenge-334/roger-bell-west/postscript/ch-2.ps b/challenge-334/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..3430204852 --- /dev/null +++ b/challenge-334/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,78 @@ +%!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 + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} 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 + +/nearestvalidpoint { + 0 dict begin + 3 1 roll + /y exch def + /x exch def + /minmhd -1 def + -1 exch + enumerate.array { + aload pop + /p exch def + /i exch def + p 0 get x eq p 1 get y eq or { + /mhd p 0 get x sub abs p 1 get y sub abs add def + minmhd -1 eq mhd minmhd lt or { + /minmhd mhd def + pop i + } if + } if + } forall + end +} bind def + +(nearestvalidpoint) test.start +3 4 [[1 2] [3 1] [2 4] [2 3]] nearestvalidpoint 2 eq test +2 5 [[3 4] [2 3] [1 5] [2 5]] nearestvalidpoint 3 eq test +1 1 [[2 2] [3 3] [4 4]] nearestvalidpoint -1 eq test +0 0 [[0 1] [1 0] [0 2] [2 0]] nearestvalidpoint 0 eq test +5 5 [[5 6] [6 5] [5 4] [4 5]] nearestvalidpoint 0 eq test +test.end diff --git a/challenge-334/roger-bell-west/python/ch-1.py b/challenge-334/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..31a1fdf54e --- /dev/null +++ b/challenge-334/roger-bell-west/python/ch-1.py @@ -0,0 +1,25 @@ +#! /usr/bin/python3 + +def rangesum(a, s, e): + return sum(a[slice(s, e + 1)]) + +import unittest + +class TestRangesum(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(rangesum([-2, 0, 3, -5, 2, -1], 0, 2), 1, 'example 1') + + def test_ex2(self): + self.assertEqual(rangesum([1, -2, 3, -4, 5], 1, 3), -3, 'example 2') + + def test_ex3(self): + self.assertEqual(rangesum([1, 0, 2, -1, 3], 3, 4), 2, 'example 3') + + def test_ex4(self): + self.assertEqual(rangesum([-5, 4, -3, 2, -1, 0], 0, 3), -2, 'example 4') + + def test_ex5(self): + self.assertEqual(rangesum([-1, 0, 2, -3, -2, 1], 0, 2), 1, 'example 5') + +unittest.main() diff --git a/challenge-334/roger-bell-west/python/ch-2.py b/challenge-334/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..0d649ae314 --- /dev/null +++ b/challenge-334/roger-bell-west/python/ch-2.py @@ -0,0 +1,33 @@ +#! /usr/bin/python3 + +def nearestvalidpoint(x, y, points): + ix = -1 + minmhd = -1 + for i, p in enumerate(points): + if p[0] == x or p[1] == y: + mhd = abs(p[0] - x) + abs(p[1] - y) + if minmhd == -1 or mhd < minmhd: + minmhd = mhd; + ix = i + return ix + +import unittest + +class TestNearestvalidpoint(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(nearestvalidpoint(3, 4, [[1, 2], [3, 1], [2, 4], [2, 3]]), 2, 'example 1') + + def test_ex2(self): + self.assertEqual(nearestvalidpoint(2, 5, [[3, 4], [2, 3], [1, 5], [2, 5]]), 3, 'example 2') + + def test_ex3(self): + self.assertEqual(nearestvalidpoint(1, 1, [[2, 2], [3, 3], [4, 4]]), -1, 'example 3') + + def test_ex4(self): + self.assertEqual(nearestvalidpoint(0, 0, [[0, 1], [1, 0], [0, 2], [2, 0]]), 0, 'example 4') + + def test_ex5(self): + self.assertEqual(nearestvalidpoint(5, 5, [[5, 6], [6, 5], [5, 4], [4, 5]]), 0, 'example 5') + +unittest.main() diff --git a/challenge-334/roger-bell-west/raku/ch-1.p6 b/challenge-334/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..eae6495811 --- /dev/null +++ b/challenge-334/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,15 @@ +#! /usr/bin/raku + +use Test; + +plan 5; + +is(rangesum([-2, 0, 3, -5, 2, -1], 0, 2), 1, 'example 1'); +is(rangesum([1, -2, 3, -4, 5], 1, 3), -3, 'example 2'); +is(rangesum([1, 0, 2, -1, 3], 3, 4), 2, 'example 3'); +is(rangesum([-5, 4, -3, 2, -1, 0], 0, 3), -2, 'example 4'); +is(rangesum([-1, 0, 2, -3, -2, 1], 0, 2), 1, 'example 5'); + +sub rangesum(@a, $s, $e) { + @a[$s .. $e].sum; +} diff --git a/challenge-334/roger-bell-west/raku/ch-2.p6 b/challenge-334/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..39ad3f692f --- /dev/null +++ b/challenge-334/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,26 @@ +#! /usr/bin/raku + +use Test; + +plan 5; + +is(nearestvalidpoint(3, 4, [[1, 2], [3, 1], [2, 4], [2, 3]]), 2, 'example 1'); +is(nearestvalidpoint(2, 5, [[3, 4], [2, 3], [1, 5], [2, 5]]), 3, 'example 2'); +is(nearestvalidpoint(1, 1, [[2, 2], [3, 3], [4, 4]]), -1, 'example 3'); +is(nearestvalidpoint(0, 0, [[0, 1], [1, 0], [0, 2], [2, 0]]), 0, 'example 4'); +is(nearestvalidpoint(5, 5, [[5, 6], [6, 5], [5, 4], [4, 5]]), 0, 'example 5'); + +sub nearestvalidpoint($x, $y, @points) { + my $ix = -1; + my $minmhd = -1; + for @points.kv -> $i, @p { + if (@p[0] == $x || @p[1] == $y) { + my $mhd = abs(@p[0] - $x) + abs(@p[1] - $y); + if ($minmhd == -1 || $mhd < $minmhd) { + $minmhd = $mhd; + $ix = $i; + } + } + } + $ix; +} diff --git a/challenge-334/roger-bell-west/ruby/ch-1.rb b/challenge-334/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..60168bc15c --- /dev/null +++ b/challenge-334/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,31 @@ +#! /usr/bin/ruby + +def rangesum(a, s, e) + a.slice(s .. e).sum +end + +require 'test/unit' + +class TestRangesum < Test::Unit::TestCase + + def test_ex1 + assert_equal(1, rangesum([-2, 0, 3, -5, 2, -1], 0, 2)) + end + + def test_ex2 + assert_equal(-3, rangesum([1, -2, 3, -4, 5], 1, 3)) + end + + def test_ex3 + assert_equal(2, rangesum([1, 0, 2, -1, 3], 3, 4)) + end + + def test_ex4 + assert_equal(-2, rangesum([-5, 4, -3, 2, -1, 0], 0, 3)) + end + + def test_ex5 + assert_equal(1, rangesum([-1, 0, 2, -3, -2, 1], 0, 2)) + end + +end diff --git a/challenge-334/roger-bell-west/ruby/ch-2.rb b/challenge-334/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..0cc03b0f20 --- /dev/null +++ b/challenge-334/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,42 @@ +#! /usr/bin/ruby + +def nearestvalidpoint(x, y, points) + ix = -1 + minmhd = -1 + points.each_with_index do |p, i| + if p[0] == x || p[1] == y + mhd = (p[0] - x).abs() + (p[1] - y).abs() + if minmhd == -1 || mhd < minmhd + minmhd = mhd + ix = i + end + end + end + ix +end + +require 'test/unit' + +class TestNearestvalidpoint < Test::Unit::TestCase + + def test_ex1 + assert_equal(2, nearestvalidpoint(3, 4, [[1, 2], [3, 1], [2, 4], [2, 3]])) + end + + def test_ex2 + assert_equal(3, nearestvalidpoint(2, 5, [[3, 4], [2, 3], [1, 5], [2, 5]])) + end + + def test_ex3 + assert_equal(-1, nearestvalidpoint(1, 1, [[2, 2], [3, 3], [4, 4]])) + end + + def test_ex4 + assert_equal(0, nearestvalidpoint(0, 0, [[0, 1], [1, 0], [0, 2], [2, 0]])) + end + + def test_ex5 + assert_equal(0, nearestvalidpoint(5, 5, [[5, 6], [6, 5], [5, 4], [4, 5]])) + end + +end diff --git a/challenge-334/roger-bell-west/rust/ch-1.rs b/challenge-334/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..e3e5daa3ab --- /dev/null +++ b/challenge-334/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,31 @@ +#! /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!(rangesum(vec![-2, 0, 3, -5, 2, -1], 0, 2), 1); +} + +#[test] +fn test_ex2() { + assert_eq!(rangesum(vec![1, -2, 3, -4, 5], 1, 3), -3); +} + +#[test] +fn test_ex3() { + assert_eq!(rangesum(vec![1, 0, 2, -1, 3], 3, 4), 2); +} + +#[test] +fn test_ex4() { + assert_eq!(rangesum(vec![-5, 4, -3, 2, -1, 0], 0, 3), -2); +} + +#[test] +fn test_ex5() { + assert_eq!(rangesum(vec![-1, 0, 2, -3, -2, 1], 0, 2), 1); +} + +fn rangesum(a: Vec<i32>, s: usize, e: usize) -> i32 { + a[s..=e].iter().sum() +} diff --git a/challenge-334/roger-bell-west/rust/ch-2.rs b/challenge-334/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..02b0fd2c67 --- /dev/null +++ b/challenge-334/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,73 @@ +#! /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!( + nearestvalidpoint( + 3, + 4, + vec![vec![1, 2], vec![3, 1], vec![2, 4], vec![2, 3]] + ), + 2 + ); +} + +#[test] +fn test_ex2() { + assert_eq!( + nearestvalidpoint( + 2, + 5, + vec![vec![3, 4], vec![2, 3], vec![1, 5], vec![2, 5]] + ), + 3 + ); +} + +#[test] +fn test_ex3() { + assert_eq!( + nearestvalidpoint(1, 1, vec![vec![2, 2], vec![3, 3], vec![4, 4]]), + -1 + ); +} + +#[test] +fn test_ex4() { + assert_eq!( + nearestvalidpoint( + 0, + 0, + vec![vec![0, 1], vec![1, 0], vec![0, 2], vec![2, 0]] + ), + 0 + ); +} + +#[test] +fn test_ex5() { + assert_eq!( + nearestvalidpoint( + 5, + 5, + vec![vec![5, 6], vec![6, 5], vec![5, 4], vec![4, 5]] + ), + 0 + ); +} + +fn nearestvalidpoint(x: i32, y: i32, points: Vec<Vec<i32>>) -> isize { + let mut ix = -1isize; + let mut minmhd = -1; + for (i, p) in points.iter().enumerate() { + if p[0] == x || p[1] == y { + let mhd = (p[0] - x).abs() + (p[1] - y).abs(); + if minmhd == -1 || mhd < minmhd { + minmhd = mhd; + ix = i as isize; + } + } + } + ix +} diff --git a/challenge-334/roger-bell-west/scala/ch-1.scala b/challenge-334/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..f2f89bd913 --- /dev/null +++ b/challenge-334/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,39 @@ + +object Rangesum { + def rangesum(a: List[Int], s: Int, e: Int): Int = { + a.slice(s, e + 1).sum + } + def main(args: Array[String]) { + if (rangesum(List(-2, 0, 3, -5, 2, -1), 0, 2) == 1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (rangesum(List(1, -2, 3, -4, 5), 1, 3) == -3) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (rangesum(List(1, 0, 2, -1, 3), 3, 4) == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (rangesum(List(-5, 4, -3, 2, -1, 0), 0, 3) == -2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (rangesum(List(-1, 0, 2, -3, -2, 1), 0, 2) == 1) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-334/roger |
