diff options
| author | Roger Bell_West <roger@firedrake.org> | 2025-10-14 16:01:48 +0100 |
|---|---|---|
| committer | Roger Bell_West <roger@firedrake.org> | 2025-10-14 16:01:48 +0100 |
| commit | 28f2c8f0d7201d6e7cf724ea4ceb92c8ff7eaf9f (patch) | |
| tree | 787c336e74fef240959a22a7ed1327db333e571f | |
| parent | 7f402e9d0ada4506d06824aeb010ef78cef2e7c2 (diff) | |
| download | perlweeklychallenge-club-28f2c8f0d7201d6e7cf724ea4ceb92c8ff7eaf9f.tar.gz perlweeklychallenge-club-28f2c8f0d7201d6e7cf724ea4ceb92c8ff7eaf9f.tar.bz2 perlweeklychallenge-club-28f2c8f0d7201d6e7cf724ea4ceb92c8ff7eaf9f.zip | |
RogerBW solutions for challenge no. 343
23 files changed, 1089 insertions, 0 deletions
diff --git a/challenge-343/roger-bell-west/crystal/ch-1.cr b/challenge-343/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..3d0ae37f66 --- /dev/null +++ b/challenge-343/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,28 @@ +#! /usr/bin/crystal + +def zerofriend(a) + b = a.map { |x| x.abs }.min + if a.includes?(b) + return b + end + -b +end + +require "spec" +describe "zerofriend" do + it "test_ex1" do + zerofriend([4, 2, -1, 3, -2]).should eq -1 + end + it "test_ex2" do + zerofriend([-5, 5, -3, 3, -1, 1]).should eq 1 + end + it "test_ex3" do + zerofriend([7, -3, 0, 2, -8]).should eq 0 + end + it "test_ex4" do + zerofriend([-2, -5, -1, -8]).should eq -1 + end + it "test_ex5" do + zerofriend([-2, 2, -4, 4, -1, 1]).should eq 1 + end +end diff --git a/challenge-343/roger-bell-west/crystal/ch-2.cr b/challenge-343/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..77cce8c9be --- /dev/null +++ b/challenge-343/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,69 @@ +#! /usr/bin/crystal + +def championteam(a) + maxw = 0 + teamw = Array(Int32).new + a.each_with_index do |w, i| + wins = w.sum + if wins > maxw + teamw = Array(Int32).new + maxw = wins + end + if wins == maxw + teamw.push(i) + end + end + if teamw.size == 1 + return teamw[0] + end + bestt = teamw[0] + teamw.each do |rt| + if a[rt][bestt] == 1 + bestt = rt + end + end + bestt +end + +def championteam(a) + maxw = 0 + teamw = Array(Int32).new + a.each_with_index do |w, i| + wins = w.sum + if wins > maxw + teamw = Array(Int32).new + maxw = wins + end + if wins == maxw + teamw.push(i) + end + end + if teamw.size == 1 + return teamw[0] + end + bestt = teamw[0] + teamw.each do |rt| + if a[rt][bestt] == 1 + bestt = rt + end + end + bestt +end +require "spec" +describe "championteam" do + it "test_ex1" do + championteam([[0, 1, 1], [0, 0, 1], [0, 0, 0]]).should eq 0 + end + it "test_ex2" do + championteam([[0, 1, 0, 0], [0, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0]]).should eq 3 + end + it "test_ex3" do + championteam([[0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 0], [0, 0, 1, 0]]).should eq 0 + end + it "test_ex4" do + championteam([[0, 1, 1], [0, 0, 0], [0, 1, 0]]).should eq 0 + end + it "test_ex5" do + championteam([[0, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 0, 1, 1], [1, 1, 0, 0, 0], [1, 1, 0, 1, 0]]).should eq 2 + end +end diff --git a/challenge-343/roger-bell-west/javascript/ch-1.js b/challenge-343/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..bacd6b9ef5 --- /dev/null +++ b/challenge-343/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,42 @@ +#! /usr/bin/node + +"use strict" + +function zerofriend(a) { + const b = Math.min(...a.map(x => Math.abs(x))); + if (a.find(x => x == b) >= 0) { + return b; + } + return -b; +} + +if (zerofriend([4, 2, -1, 3, -2]) == -1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (zerofriend([-5, 5, -3, 3, -1, 1]) == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (zerofriend([7, -3, 0, 2, -8]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (zerofriend([-2, -5, -1, -8]) == -1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (zerofriend([-2, 2, -4, 4, -1, 1]) == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-343/roger-bell-west/javascript/ch-2.js b/challenge-343/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..d64ad71cec --- /dev/null +++ b/challenge-343/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,59 @@ +#! /usr/bin/node + +"use strict" + +function championteam(a) { + let maxw = 0; + let teamw = [] + a.forEach((w, i) => { + const wins = w.reduce((x, y) => x + y); + if (wins > maxw) { + teamw = []; + maxw = wins; + } + if (wins == maxw) { + teamw.push(i); + } + }); + if (teamw.length == 1) { + return teamw[0]; + } + let bestt = teamw[0]; + for (let rt of teamw) { + if (a[rt][bestt] == 1) { + bestt = rt; + } + } + return bestt; +} + +if (championteam([[0, 1, 1], [0, 0, 1], [0, 0, 0]]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (championteam([[0, 1, 0, 0], [0, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0]]) == 3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (championteam([[0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 0], [0, 0, 1, 0]]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (championteam([[0, 1, 1], [0, 0, 0], [0, 1, 0]]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (championteam([[0, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 0, 1, 1], [1, 1, 0, 0, 0], [1, 1, 0, 1, 0]]) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-343/roger-bell-west/kotlin/ch-1.kt b/challenge-343/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..ba52eb5079 --- /dev/null +++ b/challenge-343/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,42 @@ +fun zerofriend(a: List<Int>): Int { + val b = a.map {Math.abs(it)}.minOrNull()!! + if (a.contains(b)) { + return b + } + return -b +} + +fun main() { + + if (zerofriend(listOf(4, 2, -1, 3, -2)) == -1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (zerofriend(listOf(-5, 5, -3, 3, -1, 1)) == 1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (zerofriend(listOf(7, -3, 0, 2, -8)) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (zerofriend(listOf(-2, -5, -1, -8)) == -1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (zerofriend(listOf(-2, 2, -4, 4, -1, 1)) == 1) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-343/roger-bell-west/kotlin/ch-2.kt b/challenge-343/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..5dde1f51be --- /dev/null +++ b/challenge-343/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,59 @@ +fun championteam(a: List<List<Int>>): Int { + var maxw = 0 + var teamw = ArrayList<Int>() + a.forEachIndexed {i, w -> + val wins = w.sum() + if (wins > maxw) { + teamw = ArrayList<Int>() + maxw = wins + } + if (wins == maxw) { + teamw.add(i) + } + } + if (teamw.size == 1) { + return teamw[0] + } + var bestt = teamw[0] + for (rt in teamw) { + if (a[rt][bestt] == 1) { + bestt = rt + } + } + return bestt +} + +fun main() { + + if (championteam(listOf(listOf(0, 1, 1), listOf(0, 0, 1), listOf(0, 0, 0))) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (championteam(listOf(listOf(0, 1, 0, 0), listOf(0, 0, 0, 0), listOf(1, 1, 0, 0), listOf(1, 1, 1, 0))) == 3) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (championteam(listOf(listOf(0, 1, 0, 1), listOf(0, 0, 1, 1), listOf(1, 0, 0, 0), listOf(0, 0, 1, 0))) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (championteam(listOf(listOf(0, 1, 1), listOf(0, 0, 0), listOf(0, 1, 0))) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (championteam(listOf(listOf(0, 0, 0, 0, 0), listOf(1, 0, 0, 0, 0), listOf(1, 1, 0, 1, 1), listOf(1, 1, 0, 0, 0), listOf(1, 1, 0, 1, 0))) == 2) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-343/roger-bell-west/lua/ch-1.lua b/challenge-343/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..5175b645a9 --- /dev/null +++ b/challenge-343/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,63 @@ +#! /usr/bin/lua + +function map(seq, func) + local out = {} + for _, x in ipairs(seq) do + table.insert(out, func(x)) + end + return out +end + +function filter(seq, func) + local out = {} + for _, x in ipairs(seq) do + if func(x) then + table.insert(out, x) + end + end + return out +end + +function zerofriend(a) + local b = math.min(table.unpack(map(a, function(x) return math.abs(x) end) )) + if #(filter(a, function(x) return x == b end)) > 0 then + return b + end + return -b +end + +if zerofriend({4, 2, -1, 3, -2}) == -1 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if zerofriend({-5, 5, -3, 3, -1, 1}) == 1 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if zerofriend({7, -3, 0, 2, -8}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if zerofriend({-2, -5, -1, -8}) == -1 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if zerofriend({-2, 2, -4, 4, -1, 1}) == 1 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-343/roger-bell-west/lua/ch-2.lua b/challenge-343/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..cf91282acf --- /dev/null +++ b/challenge-343/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,71 @@ +#! /usr/bin/lua + +function reduce(seq, func) + local out = seq[1] + for i = 2, #seq do + out = func(out, seq[i]) + end + return out +end + +function championteam(a) + local maxw = 0 + local teamw = {} + for i, w in ipairs(a) do + local wins = reduce(w, function(a, b) return a + b end) + if wins > maxw then + teamw = {} + maxw = wins + end + if wins == maxw then + table.insert(teamw, i) + end + end + if #teamw == 1 then + return teamw[1] - 1 + end + local bestt = teamw[1] + for _, rt in ipairs(teamw) do + if a[rt][bestt] == 1 then + bestt = rt + end + end + return bestt - 1 +end + + +if championteam({{0, 1, 1}, {0, 0, 1}, {0, 0, 0}}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if championteam({{0, 1, 0, 0}, {0, 0, 0, 0}, {1, 1, 0, 0}, {1, 1, 1, 0}}) == 3 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if championteam({{0, 1, 0, 1}, {0, 0, 1, 1}, {1, 0, 0, 0}, {0, 0, 1, 0}}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if championteam({{0, 1, 1}, {0, 0, 0}, {0, 1, 0}}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if championteam({{0, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, {1, 1, 0, 1, 1}, {1, 1, 0, 0, 0}, {1, 1, 0, 1, 0}}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-343/roger-bell-west/perl/ch-1.pl b/challenge-343/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..5ec1c95c06 --- /dev/null +++ b/challenge-343/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,24 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is(zerofriend([4, 2, -1, 3, -2]), -1, 'example 1'); +is(zerofriend([-5, 5, -3, 3, -1, 1]), 1, 'example 2'); +is(zerofriend([7, -3, 0, 2, -8]), 0, 'example 3'); +is(zerofriend([-2, -5, -1, -8]), -1, 'example 4'); +is(zerofriend([-2, 2, -4, 4, -1, 1]), 1, 'example 5'); + +use List::Util qw(min); + +sub zerofriend($a) { + my $b = min(map {abs($_)} @{$a}); + my %ah = map {$_ => 1} @{$a}; + if (exists $ah{$b}) { + return $b; + } + -$b; +} diff --git a/challenge-343/roger-bell-west/perl/ch-2.pl b/challenge-343/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..0052447590 --- /dev/null +++ b/challenge-343/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,40 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is(championteam([[0, 1, 1], [0, 0, 1], [0, 0, 0]]), 0, 'example 1'); +is(championteam([[0, 1, 0, 0], [0, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0]]), 3, 'example 2'); +is(championteam([[0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 0], [0, 0, 1, 0]]), 0, 'example 3'); +is(championteam([[0, 1, 1], [0, 0, 0], [0, 1, 0]]), 0, 'example 4'); +is(championteam([[0, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 0, 1, 1], [1, 1, 0, 0, 0], [1, 1, 0, 1, 0]]), 2, 'example 5'); + +use List::Util qw(sum); + +sub championteam($a) { + my $maxw = 0; + my @teamw; + while (my ($i, $w) = each @{$a}) { + my $wins = sum(@{$w}); + if ($wins > $maxw) { + @teamw = (); + $maxw = $wins; + } + if ($wins == $maxw) { + push @teamw, $i; + } + } + if (scalar @teamw == 1) { + return $teamw[0]; + } + my $bestt = $teamw[0]; + foreach my $rt (@teamw) { + if ($a->[$rt][$bestt] == 1) { + $bestt = $rt; + } + } + $bestt; +} diff --git a/challenge-343/roger-bell-west/python/ch-1.py b/challenge-343/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..8bff93350b --- /dev/null +++ b/challenge-343/roger-bell-west/python/ch-1.py @@ -0,0 +1,28 @@ +#! /usr/bin/python3 + +def zerofriend(a): + b = min(abs(x) for x in a) + if b in a: + return b + return -b + +import unittest + +class TestZerofriend(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(zerofriend([4, 2, -1, 3, -2]), -1, 'example 1') + + def test_ex2(self): + self.assertEqual(zerofriend([-5, 5, -3, 3, -1, 1]), 1, 'example 2') + + def test_ex3(self): + self.assertEqual(zerofriend([7, -3, 0, 2, -8]), 0, 'example 3') + + def test_ex4(self): + self.assertEqual(zerofriend([-2, -5, -1, -8]), -1, 'example 4') + + def test_ex5(self): + self.assertEqual(zerofriend([-2, 2, -4, 4, -1, 1]), 1, 'example 5') + +unittest.main() diff --git a/challenge-343/roger-bell-west/python/ch-2.py b/challenge-343/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..cf233f9efc --- /dev/null +++ b/challenge-343/roger-bell-west/python/ch-2.py @@ -0,0 +1,43 @@ +#! /usr/bin/python3 + +from functools import reduce + +def championteam(a): + maxw = 0 + teamw = [] + for i, w in enumerate(a): + wins = reduce(lambda x, y: x + y, w) + if wins > maxw: + teamw = [] + maxw = wins + if wins == maxw: + teamw.append(i) + if len(teamw) == 1: + return teamw[0] + bestt = teamw[0] + for rt in teamw: + if a[rt][bestt] == 1: + beastt = rt + return bestt + + +import unittest + +class TestChampionteam(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(championteam([[0, 1, 1], [0, 0, 1], [0, 0, 0]]), 0, 'example 1') + + def test_ex2(self): + self.assertEqual(championteam([[0, 1, 0, 0], [0, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0]]), 3, 'example 2') + + def test_ex3(self): + self.assertEqual(championteam([[0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 0], [0, 0, 1, 0]]), 0, 'example 3') + + def test_ex4(self): + self.assertEqual(championteam([[0, 1, 1], [0, 0, 0], [0, 1, 0]]), 0, 'example 4') + + def test_ex5(self): + self.assertEqual(championteam([[0, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 0, 1, 1], [1, 1, 0, 0, 0], [1, 1, 0, 1, 0]]), 2, 'example 5') + +unittest.main() diff --git a/challenge-343/roger-bell-west/raku/ch-1.p6 b/challenge-343/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..962f5dd6f7 --- /dev/null +++ b/challenge-343/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,20 @@ +#! /usr/bin/raku + +use Test; + +plan 5; + +is(zerofriend([4, 2, -1, 3, -2]), -1, 'example 1'); +is(zerofriend([-5, 5, -3, 3, -1, 1]), 1, 'example 2'); +is(zerofriend([7, -3, 0, 2, -8]), 0, 'example 3'); +is(zerofriend([-2, -5, -1, -8]), -1, 'example 4'); +is(zerofriend([-2, 2, -4, 4, -1, 1]), 1, 'example 5'); + +sub zerofriend(@a) { + my $b = @a.map({$_.abs}).min; + my %ah = @a.map({$_ => 1}); + if (%ah{$b}:exists) { + return $b; + } + -$b; +} diff --git a/challenge-343/roger-bell-west/raku/ch-2.p6 b/challenge-343/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..365d67f71d --- /dev/null +++ b/challenge-343/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,36 @@ +#! /usr/bin/raku + +use Test; + +plan 5; + +is(championteam([[0, 1, 1], [0, 0, 1], [0, 0, 0]]), 0, 'example 1'); +is(championteam([[0, 1, 0, 0], [0, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0]]), 3, 'example 2'); +is(championteam([[0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 0], [0, 0, 1, 0]]), 0, 'example 3'); +is(championteam([[0, 1, 1], [0, 0, 0], [0, 1, 0]]), 0, 'example 4'); +is(championteam([[0, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 0, 1, 1], [1, 1, 0, 0, 0], [1, 1, 0, 1, 0]]), 2, 'example 5'); + +sub championteam(@a) { + my $maxw = 0; + my @teamw; + for @a.kv -> $i, @w { + my $wins = sum(@w); + if ($wins > $maxw) { + @teamw = []; + $maxw = $wins; + } + if ($wins == $maxw) { + @teamw.push($i); + } + } + if (@teamw.elems == 1) { + return @teamw[0]; + } + my $bestt = @teamw[0]; + for @teamw -> $rt { + if (@a[$rt][$bestt] == 1) { + $bestt = $rt; + } + } + $bestt; +} diff --git a/challenge-343/roger-bell-west/ruby/ch-1.rb b/challenge-343/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..58d71cf512 --- /dev/null +++ b/challenge-343/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,35 @@ +#! /usr/bin/ruby + +def zerofriend(a) + b = a.map { |x| x.abs }.min + if a.include?(b) + return b + end + -b +end + +require 'test/unit' + +class TestZerofriend < Test::Unit::TestCase + + def test_ex1 + assert_equal(-1, zerofriend([4, 2, -1, 3, -2])) + end + + def test_ex2 + assert_equal(1, zerofriend([-5, 5, -3, 3, -1, 1])) + end + + def test_ex3 + assert_equal(0, zerofriend([7, -3, 0, 2, -8])) + end + + def test_ex4 + assert_equal(-1, zerofriend([-2, -5, -1, -8])) + end + + def test_ex5 + assert_equal(1, zerofriend([-2, 2, -4, 4, -1, 1])) + end + +end diff --git a/challenge-343/roger-bell-west/ruby/ch-2.rb b/challenge-343/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..69e63fd5f6 --- /dev/null +++ b/challenge-343/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,52 @@ +#! /usr/bin/ruby + +def championteam(a) + maxw = 0 + teamw = [] + a.each_with_index do |w, i| + wins = w.sum + if wins > maxw + teamw = [] + maxw = wins + end + if wins == maxw + teamw.push(i) + end + end + if teamw.length == 1 + return teamw[0] + end + bestt = teamw[0] + teamw.each do |rt| + if a[rt][bestt] == 1 + bestt = rt + end + end + bestt +end + +require 'test/unit' + +class TestChampionteam < Test::Unit::TestCase + + def test_ex1 + assert_equal(0, championteam([[0, 1, 1], [0, 0, 1], [0, 0, 0]])) + end + + def test_ex2 + assert_equal(3, championteam([[0, 1, 0, 0], [0, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0]])) + end + + def test_ex3 + assert_equal(0, championteam([[0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 0], [0, 0, 1, 0]])) + end + + def test_ex4 + assert_equal(0, championteam([[0, 1, 1], [0, 0, 0], [0, 1, 0]])) + end + + def test_ex5 + assert_equal(2, championteam([[0, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 0, 1, 1], [1, 1, 0, 0, 0], [1, 1, 0, 1, 0]])) + end + +end diff --git a/challenge-343/roger-bell-west/rust/ch-1.rs b/challenge-343/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..f1bc131dfc --- /dev/null +++ b/challenge-343/roger-bell-west/rust/ch-1.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!(zerofriend(vec![4, 2, -1, 3, -2]), -1); +} + +#[test] +fn test_ex2() { + assert_eq!(zerofriend(vec![-5, 5, -3, 3, -1, 1]), 1); +} + +#[test] +fn test_ex3() { + assert_eq!(zerofriend(vec![7, -3, 0, 2, -8]), 0); +} + +#[test] +fn test_ex4() { + assert_eq!(zerofriend(vec![-2, -5, -1, -8]), -1); +} + +#[test] +fn test_ex5() { + assert_eq!(zerofriend(vec![-2, 2, -4, 4, -1, 1]), 1); +} + +fn zerofriend(a: Vec<i32>) -> i32 { + let b = a.iter().map(|x| x.abs()).min().unwrap(); + if a.iter().filter(|x| **x == b).count() > 0 { + return b; + } + -b +} diff --git a/challenge-343/roger-bell-west/rust/ch-2.rs b/challenge-343/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..23341dc750 --- /dev/null +++ b/challenge-343/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,83 @@ +#! /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!( + championteam(vec![vec![0, 1, 1], vec![0, 0, 1], vec![0, 0, 0]]), + 0 + ); +} + +#[test] +fn test_ex2() { + assert_eq!( + championteam(vec![ + vec![0, 1, 0, 0], + vec![0, 0, 0, 0], + vec![1, 1, 0, 0], + vec![1, 1, 1, 0] + ]), + 3 + ); +} + +#[test] +fn test_ex3() { + assert_eq!( + championteam(vec![ + vec![0, 1, 0, 1], + vec![0, 0, 1, 1], + vec![1, 0, 0, 0], + vec![0, 0, 1, 0] + ]), + 0 + ); +} + +#[test] +fn test_ex4() { + assert_eq!( + championteam(vec![vec![0, 1, 1], vec![0, 0, 0], vec![0, 1, 0]]), + 0 + ); +} + +#[test] +fn test_ex5() { + assert_eq!( + championteam(vec![ + vec![0, 0, 0, 0, 0], + vec![1, 0, 0, 0, 0], + vec![1, 1, 0, 1, 1], + vec![1, 1, 0, 0, 0], + vec![1, 1, 0, 1, 0] + ]), + 2 + ); +} + +fn championteam(a: Vec<Vec<u8>>) -> usize { + let mut maxw = 0; + let mut teamw = Vec::new(); + for (i, w) in a.iter().enumerate() { + let wins = w.iter().sum(); + if wins > maxw { + teamw = Vec::new(); + maxw = wins + } + if wins == maxw { + teamw.push(i); + } + } + if teamw.len() == 1 { + return teamw[0]; + } + let mut bestt = teamw[0]; + for rt in teamw.iter().skip(1) { + if a[*rt][bestt] == 1 { + bestt = *rt; + } + } + bestt +} diff --git a/challenge-343/roger-bell-west/scala/ch-1.scala b/challenge-343/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..0c51091daf --- /dev/null +++ b/challenge-343/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,45 @@ + +object Zerofriend { + def zerofriend(a: List[Int]): Int = { + val b = a.map(x => x.abs).min + if (a.contains(b)) { + b + } else { + -b + } + } + + def main(args: Array[String]) { + if (zerofriend(List(4, 2, -1, 3, -2)) == -1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (zerofriend(List(-5, 5, -3, 3, -1, 1)) == 1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (zerofriend(List(7, -3, 0, 2, -8)) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (zerofriend(List(-2, -5, -1, -8)) == -1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (zerofriend(List(-2, 2, -4, 4, -1, 1)) == 1) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-343/roger-bell-west/scala/ch-2.scala b/challenge-343/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..6166b5cca3 --- /dev/null +++ b/challenge-343/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,62 @@ +import scala.collection.mutable.ListBuffer + +object Championteam { + def championteam(a: List[List[Int]]): Int = { + var maxw = 0 + var teamw = new ListBuffer[Int] + for ((w, i) <- a.zipWithIndex) { + val wins = w.sum + if (wins > maxw) { + teamw = new ListBuffer[Int] + maxw = wins + } + if (wins == maxw) { |
