diff options
| author | Roger Bell_West <roger@firedrake.org> | 2025-09-09 15:43:02 +0100 |
|---|---|---|
| committer | Roger Bell_West <roger@firedrake.org> | 2025-09-09 15:43:02 +0100 |
| commit | 3703aba5d959d03ca27ae15602564fa0842fcdff (patch) | |
| tree | ae095e7d405530c8c1e86226332230192b688112 | |
| parent | d3a276ea3ec81d3aaf94a8e0494b9fd469027cc4 (diff) | |
| download | perlweeklychallenge-club-3703aba5d959d03ca27ae15602564fa0842fcdff.tar.gz perlweeklychallenge-club-3703aba5d959d03ca27ae15602564fa0842fcdff.tar.bz2 perlweeklychallenge-club-3703aba5d959d03ca27ae15602564fa0842fcdff.zip | |
RogerBW solutions for challenge no. 338
25 files changed, 954 insertions, 0 deletions
diff --git a/challenge-338/roger-bell-west/crystal/ch-1.cr b/challenge-338/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..515956a813 --- /dev/null +++ b/challenge-338/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,24 @@ +#! /usr/bin/crystal + +def highestrow(a) + a.map{|x| x.sum}.max +end + +require "spec" +describe "highestrow" do + it "test_ex1" do + highestrow([[4, 4, 4, 4], [10, 0, 0, 0], [2, 2, 2, 9]]).should eq 16 + end + it "test_ex2" do + highestrow([[1, 5], [7, 3], [3, 5]]).should eq 10 + end + it "test_ex3" do + highestrow([[1, 2, 3], [3, 2, 1]]).should eq 6 + end + it "test_ex4" do + highestrow([[2, 8, 7], [7, 1, 3], [1, 9, 5]]).should eq 17 + end + it "test_ex5" do + highestrow([[10, 20, 30], [5, 5, 5], [0, 100, 0], [25, 25, 25]]).should eq 100 + end +end diff --git a/challenge-338/roger-bell-west/crystal/ch-2.cr b/challenge-338/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..90be96e26a --- /dev/null +++ b/challenge-338/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,26 @@ +#! /usr/bin/crystal + +def maxdistance(a, b) + l1, h1 = a.minmax + l2, h2 = b.minmax + [h1 - l2, h2 - l1].max +end + +require "spec" +describe "maxdistance" do + it "test_ex1" do + maxdistance([4, 5, 7], [9, 1, 3, 4]).should eq 6 + end + it "test_ex2" do + maxdistance([2, 3, 5, 4], [3, 2, 5, 5, 8, 7]).should eq 6 + end + it "test_ex3" do + maxdistance([2, 1, 11, 3], [2, 5, 10, 2]).should eq 9 + end + it "test_ex4" do + maxdistance([1, 2, 3], [3, 2, 1]).should eq 2 + end + it "test_ex5" do + maxdistance([1, 0, 2, 3], [5, 0]).should eq 5 + end +end diff --git a/challenge-338/roger-bell-west/javascript/ch-1.js b/challenge-338/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..30f68b0d45 --- /dev/null +++ b/challenge-338/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,38 @@ +#! /usr/bin/node + +"use strict" + +function highestrow(a) { + return Math.max(...a.map(r => r.reduce((x, y) => x + y))); +} + +if (highestrow([[4, 4, 4, 4], [10, 0, 0, 0], [2, 2, 2, 9]]) == 16) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (highestrow([[1, 5], [7, 3], [3, 5]]) == 10) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (highestrow([[1, 2, 3], [3, 2, 1]]) == 6) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (highestrow([[2, 8, 7], [7, 1, 3], [1, 9, 5]]) == 17) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (highestrow([[10, 20, 30], [5, 5, 5], [0, 100, 0], [25, 25, 25]]) == 100) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-338/roger-bell-west/javascript/ch-2.js b/challenge-338/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..fcc3f51254 --- /dev/null +++ b/challenge-338/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,42 @@ +#! /usr/bin/node + +"use strict" + +function maxdistance(a, b) { + const l1 = Math.min(...a); + const h1 = Math.max(...a); + const l2 = Math.min(...b); + const h2 = Math.max(...b); + return Math.max(h1 - l2, h2 - l1); +} + +if (maxdistance([4, 5, 7], [9, 1, 3, 4]) == 6) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maxdistance([2, 3, 5, 4], [3, 2, 5, 5, 8, 7]) == 6) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maxdistance([2, 1, 11, 3], [2, 5, 10, 2]) == 9) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maxdistance([1, 2, 3], [3, 2, 1]) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maxdistance([1, 0, 2, 3], [5, 0]) == 5) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-338/roger-bell-west/kotlin/ch-1.kt b/challenge-338/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..9167101288 --- /dev/null +++ b/challenge-338/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,38 @@ +fun highestrow(a: List<List<Int>>): Int { + return a.map{ it.sum() }.maxOrNull()!! +} + +fun main() { + + if (highestrow(listOf(listOf(4, 4, 4, 4), listOf(10, 0, 0, 0), listOf(2, 2, 2, 9))) == 16) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (highestrow(listOf(listOf(1, 5), listOf(7, 3), listOf(3, 5))) == 10) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (highestrow(listOf(listOf(1, 2, 3), listOf(3, 2, 1))) == 6) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (highestrow(listOf(listOf(2, 8, 7), listOf(7, 1, 3), listOf(1, 9, 5))) == 17) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (highestrow(listOf(listOf(10, 20, 30), listOf(5, 5, 5), listOf(0, 100, 0), listOf(25, 25, 25))) == 100) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-338/roger-bell-west/kotlin/ch-2.kt b/challenge-338/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..b3f52566aa --- /dev/null +++ b/challenge-338/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,42 @@ +fun maxdistance(a: List<Int>, b: List<Int>): Int { + val l1 = a.minOrNull()!! + val h1 = a.maxOrNull()!! + val l2 = b.minOrNull()!! + val h2 = b.maxOrNull()!! + return listOf(h1 - l2, h2 - l1).maxOrNull()!! +} + +fun main() { + + if (maxdistance(listOf(4, 5, 7), listOf(9, 1, 3, 4)) == 6) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxdistance(listOf(2, 3, 5, 4), listOf(3, 2, 5, 5, 8, 7)) == 6) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxdistance(listOf(2, 1, 11, 3), listOf(2, 5, 10, 2)) == 9) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxdistance(listOf(1, 2, 3), listOf(3, 2, 1)) == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxdistance(listOf(1, 0, 2, 3), listOf(5, 0)) == 5) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-338/roger-bell-west/lua/ch-1.lua b/challenge-338/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..9865c68e0f --- /dev/null +++ b/challenge-338/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,49 @@ +#! /usr/bin/lua + +function highestrow(a) + local ms = {} + for _, x in ipairs(a) do + local s = 0 + for __, y in ipairs(x) do + s = s + y + end + table.insert(ms, s) + end + return math.max(table.unpack(ms)) +end + +if highestrow({{4, 4, 4, 4}, {10, 0, 0, 0}, {2, 2, 2, 9}}) == 16 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if highestrow({{1, 5}, {7, 3}, {3, 5}}) == 10 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if highestrow({{1, 2, 3}, {3, 2, 1}}) == 6 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if highestrow({{2, 8, 7}, {7, 1, 3}, {1, 9, 5}}) == 17 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if highestrow({{10, 20, 30}, {5, 5, 5}, {0, 100, 0}, {25, 25, 25}}) == 100 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-338/roger-bell-west/lua/ch-2.lua b/challenge-338/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..2fe55691c8 --- /dev/null +++ b/challenge-338/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,45 @@ +#! /usr/bin/lua + +function maxdistance(a, b) + local l1 = math.min(table.unpack(a)) + local h1 = math.max(table.unpack(a)) + local l2 = math.min(table.unpack(b)) + local h2 = math.max(table.unpack(b)) + return math.max(h1 - l2, h2 - l1) +end + +if maxdistance({4, 5, 7}, {9, 1, 3, 4}) == 6 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maxdistance({2, 3, 5, 4}, {3, 2, 5, 5, 8, 7}) == 6 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maxdistance({2, 1, 11, 3}, {2, 5, 10, 2}) == 9 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maxdistance({1, 2, 3}, {3, 2, 1}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maxdistance({1, 0, 2, 3}, {5, 0}) == 5 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-338/roger-bell-west/perl/ch-1.pl b/challenge-338/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..18c21b3dd1 --- /dev/null +++ b/challenge-338/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(highestrow([[4, 4, 4, 4], [10, 0, 0, 0], [2, 2, 2, 9]]), 16, 'example 1'); +is(highestrow([[1, 5], [7, 3], [3, 5]]), 10, 'example 2'); +is(highestrow([[1, 2, 3], [3, 2, 1]]), 6, 'example 3'); +is(highestrow([[2, 8, 7], [7, 1, 3], [1, 9, 5]]), 17, 'example 4'); +is(highestrow([[10, 20, 30], [5, 5, 5], [0, 100, 0], [25, 25, 25]]), 100, 'example 5'); + +use List::Util qw(sum max); + +sub highestrow($a) { + max map {sum @{$_}} @{$a}; +} diff --git a/challenge-338/roger-bell-west/perl/ch-2.pl b/challenge-338/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..dd00829af9 --- /dev/null +++ b/challenge-338/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,22 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is(maxdistance([4, 5, 7], [9, 1, 3, 4]), 6, 'example 1'); +is(maxdistance([2, 3, 5, 4], [3, 2, 5, 5, 8, 7]), 6, 'example 2'); +is(maxdistance([2, 1, 11, 3], [2, 5, 10, 2]), 9, 'example 3'); +is(maxdistance([1, 2, 3], [3, 2, 1]), 2, 'example 4'); +is(maxdistance([1, 0, 2, 3], [5, 0]), 5, 'example 5'); + +use List::Util qw(max); +use List::MoreUtils qw(minmax); + +sub maxdistance($a, $b) { + my ($l1, $h1) = minmax(@{$a}); + my ($l2, $h2) = minmax(@{$b}); + return max($h1 - $l2, $h2 - $l1); +} diff --git a/challenge-338/roger-bell-west/postscript/ch-1.ps b/challenge-338/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..6b68da2897 --- /dev/null +++ b/challenge-338/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,71 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 + + +/listmax { + { max } reduce +} 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.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 + +/highestrow { + [ exch + { + { add } reduce + } forall + ] + listmax +} bind def + +(highestrow) test.start +[[4 4 4 4] [10 0 0 0] [2 2 2 9]] highestrow 16 eq test +[[1 5] [7 3] [3 5]] highestrow 10 eq test +[[1 2 3] [3 2 1]] highestrow 6 eq test +[[2 8 7] [7 1 3] [1 9 5]] highestrow 17 eq test +[[10 20 30] [5 5 5] [0 100 0] [25 25 25]] highestrow 100 eq test +test.end diff --git a/challenge-338/roger-bell-west/postscript/ch-2.ps b/challenge-338/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..bf81c84b6d --- /dev/null +++ b/challenge-338/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,76 @@ +%!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 + +/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 + +/minmax { + 0 dict begin + /a exch def + /l a dup length 1 sub get def + /h l def + 0 2 a length 2 sub { + /i exch def + a i get a i 1 add get + 2 copy gt { + exch + } if + h max /h exch def + l min /l exch def + } for + [ l h ] + 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 + +/maxdistance { + minmax exch + minmax + aload pop + 3 -1 roll + aload pop + 4 1 roll + sub + 3 1 roll + sub + max +} bind def + +(maxdistance) test.start +[4 5 7] [9 1 3 4] maxdistance 6 eq test +[2 3 5 4] [3 2 5 5 8 7] maxdistance 6 eq test +[2 1 11 3] [2 5 10 2] maxdistance 9 eq test +[1 2 3] [3 2 1] maxdistance 2 eq test +[1 0 2 3] [5 0] maxdistance 5 eq test +test.end diff --git a/challenge-338/roger-bell-west/python/ch-1.py b/challenge-338/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..71a831c440 --- /dev/null +++ b/challenge-338/roger-bell-west/python/ch-1.py @@ -0,0 +1,25 @@ +#! /usr/bin/python3 + +def highestrow(a): + return max(sum(x) for x in a) + +import unittest + +class TestHighestrow(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(highestrow([[4, 4, 4, 4], [10, 0, 0, 0], [2, 2, 2, 9]]), 16, 'example 1') + + def test_ex2(self): + self.assertEqual(highestrow([[1, 5], [7, 3], [3, 5]]), 10, 'example 2') + + def test_ex3(self): + self.assertEqual(highestrow([[1, 2, 3], [3, 2, 1]]), 6, 'example 3') + + def test_ex4(self): + self.assertEqual(highestrow([[2, 8, 7], [7, 1, 3], [1, 9, 5]]), 17, 'example 4') + + def test_ex5(self): + self.assertEqual(highestrow([[10, 20, 30], [5, 5, 5], [0, 100, 0], [25, 25, 25]]), 100, 'example 5') + +unittest.main() diff --git a/challenge-338/roger-bell-west/python/ch-2.py b/challenge-338/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..76b3c2be92 --- /dev/null +++ b/challenge-338/roger-bell-west/python/ch-2.py @@ -0,0 +1,30 @@ +#! /usr/bin/python3 + +def maxdistance(a, b): + l1 = min(a) + h1 = max(a) + l2 = min(b) + h2 = max(b) + return max(h1 - l2, h2 - l1) + + +import unittest + +class TestMaxdistance(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(maxdistance([4, 5, 7], [9, 1, 3, 4]), 6, 'example 1') + + def test_ex2(self): + self.assertEqual(maxdistance([2, 3, 5, 4], [3, 2, 5, 5, 8, 7]), 6, 'example 2') + + def test_ex3(self): + self.assertEqual(maxdistance([2, 1, 11, 3], [2, 5, 10, 2]), 9, 'example 3') + + def test_ex4(self): + self.assertEqual(maxdistance([1, 2, 3], [3, 2, 1]), 2, 'example 4') + + def test_ex5(self): + self.assertEqual(maxdistance([1, 0, 2, 3], [5, 0]), 5, 'example 5') + +unittest.main() diff --git a/challenge-338/roger-bell-west/raku/ch-1.p6 b/challenge-338/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..f4621c0370 --- /dev/null +++ b/challenge-338/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,15 @@ +#! /usr/bin/raku + +use Test; + +plan 5; + +is(highestrow([[4, 4, 4, 4], [10, 0, 0, 0], [2, 2, 2, 9]]), 16, 'example 1'); +is(highestrow([[1, 5], [7, 3], [3, 5]]), 10, 'example 2'); +is(highestrow([[1, 2, 3], [3, 2, 1]]), 6, 'example 3'); +is(highestrow([[2, 8, 7], [7, 1, 3], [1, 9, 5]]), 17, 'example 4'); +is(highestrow([[10, 20, 30], [5, 5, 5], [0, 100, 0], [25, 25, 25]]), 100, 'example 5'); + +sub highestrow(@a) { + @a.map({$_.sum}).max; +} diff --git a/challenge-338/roger-bell-west/raku/ch-2.p6 b/challenge-338/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..6567dafa41 --- /dev/null +++ b/challenge-338/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,19 @@ +#! /usr/bin/raku + +use Test; + +plan 5; + +is(maxdistance([4, 5, 7], [9, 1, 3, 4]), 6, 'example 1'); +is(maxdistance([2, 3, 5, 4], [3, 2, 5, 5, 8, 7]), 6, 'example 2'); +is(maxdistance([2, 1, 11, 3], [2, 5, 10, 2]), 9, 'example 3'); +is(maxdistance([1, 2, 3], [3, 2, 1]), 2, 'example 4'); +is(maxdistance([1, 0, 2, 3], [5, 0]), 5, 'example 5'); + +sub maxdistance(@a, @b) { + my $l1 = min(@a); + my $h1 = max(@a); + my $l2 = min(@b); + my $h2 = max(@b); + return max($h1 - $l2, $h2 - $l1); +} diff --git a/challenge-338/roger-bell-west/ruby/ch-1.rb b/challenge-338/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..1396f6ece9 --- /dev/null +++ b/challenge-338/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,31 @@ +#! /usr/bin/ruby + +def highestrow(a) + a.map{|x| x.sum}.max +end + +require 'test/unit' + +class TestHighestrow < Test::Unit::TestCase + + def test_ex1 + assert_equal(16, highestrow([[4, 4, 4, 4], [10, 0, 0, 0], [2, 2, 2, 9]])) + end + + def test_ex2 + assert_equal(10, highestrow([[1, 5], [7, 3], [3, 5]])) + end + + def test_ex3 + assert_equal(6, highestrow([[1, 2, 3], [3, 2, 1]])) + end + + def test_ex4 + assert_equal(17, highestrow([[2, 8, 7], [7, 1, 3], [1, 9, 5]])) + end + + def test_ex5 + assert_equal(100, highestrow([[10, 20, 30], [5, 5, 5], [0, 100, 0], [25, 25, 25]])) + end + +end diff --git a/challenge-338/roger-bell-west/ruby/ch-2.rb b/challenge-338/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..d1cf36bd92 --- /dev/null +++ b/challenge-338/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,33 @@ +#! /usr/bin/ruby + +def maxdistance(a, b) + l1, h1 = a.minmax + l2, h2 = b.minmax + [h1 - l2, h2 - l1].max +end + +require 'test/unit' + +class TestMaxdistance < Test::Unit::TestCase + + def test_ex1 + assert_equal(6, maxdistance([4, 5, 7], [9, 1, 3, 4])) + end + + def test_ex2 + assert_equal(6, maxdistance([2, 3, 5, 4], [3, 2, 5, 5, 8, 7])) + end + + def test_ex3 + assert_equal(9, maxdistance([2, 1, 11, 3], [2, 5, 10, 2])) + end + + def test_ex4 + assert_equal(2, maxdistance([1, 2, 3], [3, 2, 1])) + end + + def test_ex5 + assert_equal(5, maxdistance([1, 0, 2, 3], [5, 0])) + end + +end diff --git a/challenge-338/roger-bell-west/rust/ch-1.rs b/challenge-338/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..2e2989752f --- /dev/null +++ b/challenge-338/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,45 @@ +#! /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!( + highestrow(vec![vec![4, 4, 4, 4], vec![10, 0, 0, 0], vec![2, 2, 2, 9]]), + 16 + ); +} + +#[test] +fn test_ex2() { + assert_eq!(highestrow(vec![vec![1, 5], vec![7, 3], vec![3, 5]]), 10); +} + +#[test] +fn test_ex3() { + assert_eq!(highestrow(vec![vec![1, 2, 3], vec![3, 2, 1]]), 6); +} + +#[test] +fn test_ex4() { + assert_eq!( + highestrow(vec![vec![2, 8, 7], vec![7, 1, 3], vec![1, 9, 5]]), + 17 + ); +} + +#[test] +fn test_ex5() { + assert_eq!( + highestrow(vec![ + vec![10, 20, 30], + vec![5, 5, 5], + vec![0, 100, 0], + vec![25, 25, 25] + ]), + 100 + ); +} + +fn highestrow(a: Vec<Vec<u32>>) -> u32 { + a.iter().map(|x| x.into_iter().sum()).max().unwrap() +} diff --git a/challenge-338/roger-bell-west/rust/ch-2.rs b/challenge-338/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..89a5d54c8c --- /dev/null +++ b/challenge-338/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,35 @@ +#! /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!(maxdistance(vec![4, 5, 7], vec![9, 1, 3, 4]), 6); +} + +#[test] +fn test_ex2() { + assert_eq!(maxdistance(vec![2, 3, 5, 4], vec![3, 2, 5, 5, 8, 7]), 6); +} + +#[test] +fn test_ex3() { + assert_eq!(maxdistance(vec![2, 1, 11, 3], vec![2, 5, 10, 2]), 9); +} + +#[test] +fn test_ex4() { + assert_eq!(maxdistance(vec![1, 2, 3], vec![3, 2, 1]), 2); +} + +#[test] +fn test_ex5() { + assert_eq!(maxdistance(vec![1, 0, 2, 3], vec![5, 0]), 5); +} + +fn maxdistance(a: Vec<i32>, b: Vec<i32>) -> i32 { + let l1 = a.iter().min().unwrap(); + let h1 = a.iter().max().unwrap(); + let l2 = b.iter().min().unwrap(); + let h2 = b.iter().max().unwrap(); + *[h1 - l2, h2 - l1].iter().max().unwrap() +} diff --git a/challenge-338/roger-bell-west/scala/ch-1.scala b/challenge-338/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..0114c3fb75 --- /dev/null +++ b/challenge-338/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,40 @@ + +object Highestrow { +def highestrow(a: List[List[Int]]): Int = { + a.map( x => x.sum ).max +} + + def main(args: Array[String]) { + if (highestrow(List(List(4, 4, 4, 4), List(10, 0, 0, 0), List(2, 2, 2, 9))) == 16) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (highestrow(List(List(1, 5), List(7, 3), List(3, 5))) == 10) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (highestrow(List(List(1, 2, 3), List(3, 2, 1))) == 6) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (highestrow(List(List(2, 8, 7), List(7, 1, 3), List(1, 9, 5))) == 17) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (highestrow(List(List(10, 20, 30), List(5, 5, 5), List(0, 100, 0), List(25, 25, 25))) == 100) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-338/roger-bell-west/scala/ch-2.scala b/challenge-338/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..eba955aad9 --- /dev/null +++ b/challenge-338/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,43 @@ + +object Maxdistance { + def maxdistance(a: List[Int], b: List[Int]): Int = { + val l1 = a.min + val h1 = a.max + val l2 = b.min + val h2 = b.max + List(h1 - l2, h2 - l1).max + } + def main(args: Array[String]) { + if (maxdistance(List(4, 5, 7), List(9, 1, 3, 4)) == 6) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxdistance(List(2, 3, 5, 4), List(3, 2, 5, 5, 8, 7)) == 6) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxdistance(List(2, 1, 11, 3), List(2, 5, 10, 2)) == 9) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxdistance(List(1, 2, 3), List(3, 2, 1)) == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxdistance(List(1, 0, 2, 3), List(5, 0)) == 5) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-338/roger-bell-west/tests.json b/challenge-338/roger-bell-west/tests.json new file mode 100644 index 0000000000..9ec5e2c637 --- /dev/null +++ b/challenge-338/roger-bell-west/tests.json @@ -0,0 +1,88 @@ +{ + "ch-1" : [ + { + "function" : "highestrow", + "arguments" : [ + [4, 4, 4, 4], + [10, 0, 0, 0], + [2, 2, 2, 9] + ], + "result" : 16 + }, + { + "arguments" : [ + [1, 5], + [7, 3], + [3, 5] + |
