From 8b44b0beb72b0c89f7bc0a3c77a70244ec4d79a9 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Wed, 24 May 2023 23:01:11 +0100 Subject: RogerBW solutions for challenge no. 218 --- challenge-218/roger-bell-west/javascript/ch-1.js | 52 +++++++ challenge-218/roger-bell-west/javascript/ch-2.js | 53 +++++++ challenge-218/roger-bell-west/kotlin/ch-1.kt | 50 +++++++ challenge-218/roger-bell-west/kotlin/ch-2.kt | 56 ++++++++ challenge-218/roger-bell-west/lua/ch-1.lua | 52 +++++++ challenge-218/roger-bell-west/lua/ch-2.lua | 53 +++++++ challenge-218/roger-bell-west/perl/ch-1.pl | 35 +++++ challenge-218/roger-bell-west/perl/ch-2.pl | 46 ++++++ challenge-218/roger-bell-west/postscript/ch-1.ps | 172 +++++++++++++++++++++++ challenge-218/roger-bell-west/postscript/ch-2.ps | 84 +++++++++++ challenge-218/roger-bell-west/python/ch-1.py | 35 +++++ challenge-218/roger-bell-west/python/ch-2.py | 38 +++++ challenge-218/roger-bell-west/raku/ch-1.p6 | 31 ++++ challenge-218/roger-bell-west/raku/ch-2.p6 | 45 ++++++ challenge-218/roger-bell-west/ruby/ch-1.rb | 44 ++++++ challenge-218/roger-bell-west/ruby/ch-2.rb | 52 +++++++ challenge-218/roger-bell-west/rust/ch-1.rs | 44 ++++++ challenge-218/roger-bell-west/rust/ch-2.rs | 52 +++++++ challenge-218/roger-bell-west/tests.yaml | 49 +++++++ 19 files changed, 1043 insertions(+) create mode 100755 challenge-218/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-218/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-218/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-218/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-218/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-218/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-218/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-218/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-218/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-218/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-218/roger-bell-west/python/ch-1.py create mode 100755 challenge-218/roger-bell-west/python/ch-2.py create mode 100755 challenge-218/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-218/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-218/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-218/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-218/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-218/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-218/roger-bell-west/tests.yaml diff --git a/challenge-218/roger-bell-west/javascript/ch-1.js b/challenge-218/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..1208ea6708 --- /dev/null +++ b/challenge-218/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,52 @@ +#! /usr/bin/node + +function maximumproduct(lst) { + let l = lst; + l.sort(function(a,b) { + return a-b; + }); + const b = l.length; + let t = []; + for (let i = 0; i <= 3; i++) { + let p = 1; + if (i > 0) { + for (let j = 0; j < i; j++) { + p *= l[j]; + } + } + if (i < 3) { + for (let j = b - 3 + i; j < b; j++) { + p *= l[j]; + } + } + t.push(p); + } + return Math.max(...t); +} + +"use strict" + +if (maximumproduct([3, 1, 2]) == 6) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maximumproduct([4, 1, 3, 2]) == 24) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maximumproduct([-1, 0, 1, 3, 1]) == 3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maximumproduct([-8, 2, -9, 0, -4, 3]) == 216) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-218/roger-bell-west/javascript/ch-2.js b/challenge-218/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..76dd4f42ea --- /dev/null +++ b/challenge-218/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,53 @@ +#! /usr/bin/node + +"use strict" + +function matrixscore(matrix0) { + let matrix = matrix0; + for (let i = 0; i < matrix.length; i++) { + if (matrix[i][0] == 0) { + for (let j = 0; j < matrix[i].length; j++) { + matrix[i][j] = 1 - matrix[i][j]; + } + } + } + const t = Math.floor(matrix.length / 2); + for (let i = 1; i < matrix[0].length; i++) { + let c = 0; + for (let j = 0; j < matrix.length; j++) { + if (matrix[j][i] == 0) { + c += 1; + } + } + if (c > t) { + for (let j= 0; j < matrix.length; j++) { + matrix[j][i] = 1 - matrix[j][i]; + } + } + } + let tot = 0; + for (let m of matrix) { + let p = 0; + for (let n of m) { + p *= 2; + if (n == 1) { + p += 1; + } + } + tot += p; + } + return tot; +} + +if (matrixscore([[0, 0, 1, 1], [1, 0, 1, 0], [1, 1, 1, 0]]) == 39) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (matrixscore([[0]]) == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-218/roger-bell-west/kotlin/ch-1.kt b/challenge-218/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..7f791f1802 --- /dev/null +++ b/challenge-218/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,50 @@ +fun maximumproduct(lst: List): Int { + var l = ArrayList(lst) + l.sort() + val b = l.size + var t = ArrayList() + for (i in 0..3) { + var p = 1 + if (i > 0) { + for (j in 0 .. i-1) { + p *= l[j] + } + } + if (i < 3) { + for (j in b - 3 + i .. b-1) { + p *= l[j] + } + } + t.add(p) + } + return t.maxOrNull()!! +} + +fun main() { + + if (maximumproduct(listOf(3, 1, 2)) == 6) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maximumproduct(listOf(4, 1, 3, 2)) == 24) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maximumproduct(listOf(-1, 0, 1, 3, 1)) == 3) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maximumproduct(listOf(-8, 2, -9, 0, -4, 3)) == 216) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-218/roger-bell-west/kotlin/ch-2.kt b/challenge-218/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..0cfcd6d337 --- /dev/null +++ b/challenge-218/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,56 @@ +fun matrixscore(matrix0: List>): Int { + var matrix = ArrayList>() + for (r in matrix0) { + matrix.add(ArrayList(r)) + } + for (i in 0 .. matrix.size-1) { + if (matrix[i][0] == 0) { + for (j in 0 .. matrix[i].size-1) { + matrix[i][j] = 1 - matrix[i][j] + } + } + } + val t = matrix.size / 2 + for (i in 1 .. matrix[0].size-1) { + var c = 0 + for (j in 0 .. matrix.size-1) { + if (matrix[j][i] == 0) { + c += 1 + } + } + if (c > t) { + for (j in 0 .. matrix.size-1) { + matrix[j][i] = 1 - matrix[j][i] + } + } + } + var tot = 0 + for (m in matrix) { + var p = 0 + for (n in m) { + p *= 2 + if (n == 1) { + p += 1 + } + } + tot += p + } + return tot +} + +fun main() { + + if (matrixscore(listOf(listOf(0, 0, 1, 1), listOf(1, 0, 1, 0), listOf(1, 1, 1, 0))) == 39) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (matrixscore(listOf(listOf(0))) == 1) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-218/roger-bell-west/lua/ch-1.lua b/challenge-218/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..fd4065b56f --- /dev/null +++ b/challenge-218/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,52 @@ +#! /usr/bin/lua + +function maximumproduct(lst) + local l = lst + table.sort(l) + local b = #l + local t = {} + for i = 0, 3 do + local p = 1 + if i > 0 then + for j = 1, i do + p = p * l[j] + end + end + if i < 3 then + for j = b - 2 + i, b do + p = p * l[j] + end + end + table.insert(t, p) + end + return math.max(table.unpack(t)) +end + +if maximumproduct({3, 1, 2}) == 6 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maximumproduct({4, 1, 3, 2}) == 24 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maximumproduct({-1, 0, 1, 3, 1}) == 3 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maximumproduct({-8, 2, -9, 0, -4, 3}) == 216 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-218/roger-bell-west/lua/ch-2.lua b/challenge-218/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..2981524741 --- /dev/null +++ b/challenge-218/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,53 @@ +#! /usr/bin/lua + +function matrixscore(matrix0) + local matrix = matrix0 + for i = 1, #matrix do + if matrix[i][1] == 0 then + for j = 1, #(matrix[i]) do + matrix[i][j] = 1 - matrix[i][j] + end + end + end + local t = #matrix // 2 + for i = 2, #(matrix[1]) do + local c = 0 + for j = 1, #matrix do + if matrix[j][i] == 0 then + c = c + 1 + end + end + if c > t then + for j = 1, #matrix do + matrix[j][i] = 1 - matrix[j][i] + end + end + end + local tot = 0 + for _a, m in ipairs(matrix) do + local p = 0 + for _b, n in ipairs(m) do + p = p * 2 + if n == 1 then + p = p + 1 + end + end + tot = tot + p + end + return tot +end + +if matrixscore({{0, 0, 1, 1}, {1, 0, 1, 0}, {1, 1, 1, 0}}) == 39 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if matrixscore({{0}}) == 1 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-218/roger-bell-west/perl/ch-1.pl b/challenge-218/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..4219de5b6a --- /dev/null +++ b/challenge-218/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,35 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 4; + +use List::Util qw(max); + +is(maximumproduct([3, 1, 2]), 6, 'example 1'); +is(maximumproduct([4, 1, 3, 2]), 24, 'example 2'); +is(maximumproduct([-1, 0, 1, 3, 1]), 3, 'example 3'); +is(maximumproduct([-8, 2, -9, 0, -4, 3]), 216, 'example 4'); + +sub maximumproduct($lst) { + my @l = sort {$a <=> $b} @{$lst}; + my $b = scalar @l; + my @t; + foreach my $i (0..3) { + my $p = 1; + if ($i > 0) { + foreach my $j (0..$i-1) { + $p *= $l[$j]; + } + } + if ($i < 3) { + foreach my $j ($b-3+$i..$b-1) { + $p *= $l[$j]; + } + } + push @t, $p; + } + return max(@t); +} diff --git a/challenge-218/roger-bell-west/perl/ch-2.pl b/challenge-218/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..cc7474f36a --- /dev/null +++ b/challenge-218/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,46 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(matrixscore([[0, 0, 1, 1], [1, 0, 1, 0], [1, 1, 1, 0]]), 39, 'example 1'); +is(matrixscore([[0]]), 1, 'example 2'); + +sub matrixscore($matrix) { + foreach my $i (0..$#{$matrix}) { + if ($matrix->[$i][0] == 0) { + foreach my $j (0..$#{$matrix->[0]}) { + $matrix->[$i][$j] = 1 - $matrix->[$i][$j]; + } + } + } + my $t = int(scalar @{$matrix} / 2); + foreach my $i (1..$#{$matrix->[0]}) { + my $c = 0; + foreach my $j (0..$#{$matrix}) { + if ($matrix->[$j][$i] == 0) { + $c++; + } + } + if ($c > $t) { + foreach my $j (0..$#{$matrix}) { + $matrix->[$j][$i] = 1 - $matrix->[$j][$i]; + } + } + } + my $tot = 0; + foreach my $m (@{$matrix}) { + my $p = 0; + foreach my $n (@{$m}) { + $p *= 2; + if ($n == 1) { + $p++; + } + } + $tot += $p; + } + return $tot; +} diff --git a/challenge-218/roger-bell-west/postscript/ch-1.ps b/challenge-218/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..6730a05b8b --- /dev/null +++ b/challenge-218/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,172 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/quicksort.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} 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 + +/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 { + { quicksort.cmp } quicksort.with_comparator +} 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 + +/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.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 { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/listmax { + { max } reduce +} bind def + + +% end included library code + +/maximumproduct { + 4 dict begin + /l exch quicksort def + /b l length def + [ + 0 1 3 { + /i exch def + /p 1 def + i 0 gt { + 0 1 i 1 sub { + l exch get p mul /p exch def + } for + } if + i 3 lt { + b 3 sub i add 1 b 1 sub { + l exch get p mul /p exch def + } for + } if + p + } for + ] listmax + end +} bind def + +(maximumproduct) test.start +[3 1 2] maximumproduct 6 eq test +[4 1 3 2] maximumproduct 24 eq test +[-1 0 1 3 1] maximumproduct 3 eq test +[-8 2 -9 0 -4 3] maximumproduct 216 eq test +test.end diff --git a/challenge-218/roger-bell-west/postscript/ch-2.ps b/challenge-218/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..89fede440d --- /dev/null +++ b/challenge-218/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,84 @@ +%!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 + +/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 + +/matrixscore { + 4 dict begin + /mx exch def + 0 1 mx length 1 sub { + /i exch def + mx i get 0 get 0 eq { + 0 1 mx i get length 1 sub { + /j exch def + mx i get dup j get 1 exch sub j exch put + } for + } if + } for + /t mx length 2 idiv def + 1 1 mx 0 get length 1 sub { + /i exch def + 0 + 0 1 mx length 1 sub { + mx exch get i get 0 eq { + 1 add + } if + } for + t gt { + 0 1 mx length 1 sub { + /j exch def + mx j get dup i get 1 exch sub i exch put + } for + } if + } for + 0 + mx { + 0 exch + { + exch 2 mul exch + 1 eq { + 1 add + } if + } forall + add + } forall + end +} bind def + +(matrixscore) test.start +[[0 0 1 1] [1 0 1 0] [1 1 1 0]] matrixscore 39 eq test +[[0]] matrixscore 1 eq test +test.end diff --git a/challenge-218/roger-bell-west/python/ch-1.py b/challenge-218/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..e148c40816 --- /dev/null +++ b/challenge-218/roger-bell-west/python/ch-1.py @@ -0,0 +1,35 @@ +#! /usr/bin/python3 + +def maximumproduct(lst): + l = lst + l.sort() + b = len(l) + t = [] + for i in range(4): + p = 1 + if i > 0: + for j in range(i): + p *= l[j] + if i < 3: + for j in range(b - 3 + i, b): + p *= l[j] + t.append(p) + return max(t) + +import unittest + +class TestMaximumproduct(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(maximumproduct([3, 1, 2]), 6, 'example 1') + + def test_ex2(self): + self.assertEqual(maximumproduct([4, 1, 3, 2]), 24, 'example 2') + + def test_ex3(self): + self.assertEqual(maximumproduct([-1, 0, 1, 3, 1]), 3, 'example 3') + + def test_ex4(self): + self.assertEqual(maximumproduct([-8, 2, -9, 0, -4, 3]), 216, 'example 4') + +unittest.main() diff --git a/challenge-218/roger-bell-west/python/ch-2.py b/challenge-218/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..7d16a57c01 --- /dev/null +++ b/challenge-218/roger-bell-west/python/ch-2.py @@ -0,0 +1,38 @@ +#! /usr/bin/python3 + +def matrixscore(matrix0): + matrix = matrix0; + for i in range(len(matrix)): + if matrix[i][0] == 0: + for j in range(len(matrix[i])): + matrix[i][j] = 1 - matrix[i][j] + t = len(matrix) // 2 + for i in range(1, len(matrix[0])): + c = 0 + for j in range(len(matrix)): + if matrix[j][i] == 0: + c += 1 + if c > t: + for j in range(len(matrix)): + matrix[j][i] = 1 - matrix[j][i] + tot = 0 + for m in matrix: + p = 0 + for n in m: + p *= 2 + if n == 1: + p += 1 + tot += p + return tot + +import unittest + +class TestMatrixscore(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(matrixscore([[0, 0, 1, 1], [1, 0, 1, 0], [1, 1, 1, 0]]), 39, 'example 1') + + def test_ex2(self): + self.assertEqual(matrixscore([[0]]), 1, 'example 2') + +unittest.main() diff --git a/challenge-218/roger-bell-west/raku/ch-1.p6 b/challenge-218/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..b1350b04bb --- /dev/null +++ b/challenge-218/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,31 @@ +#! /usr/bin/raku + +use Test; + +plan 4; + +is(maximumproduct([3, 1, 2]), 6, 'example 1'); +is(maximumproduct([4, 1, 3, 2]), 24, 'example 2'); +is(maximumproduct([-1, 0, 1, 3, 1]), 3, 'example 3'); +is(maximumproduct([-8, 2, -9, 0, -4, 3]), 216, 'example 4'); + +sub maximumproduct(@lst) { + my @l = sort {$^a <=> $^b}, @lst; + my $b = @l.elems; + my @t; + for (0..3) -> $i { + my $p = 1; + if ($i > 0) { + for (0..$i-1) -> $j { + $p *= @l[$j]; + } + } + if ($i < 3) { + for ($b-3+$i..$b-1) -> $j { + $p *= @l[$j]; + } + } + @t.push($p); + } + return max(@t); +} diff --git a/challenge-218/roger-bell-west/raku/ch-2.p6 b/challenge-218/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..85cf63575d --- /dev/null +++ b/challenge-218/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,45 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(matrixscore([[0, 0, 1, 1], [1, 0, 1, 0], [1, 1, 1, 0]]), 39, 'example 1'); +is(matrixscore([[0]]), 1, 'example 2'); + +sub matrixscore(@matrix0) { + my @matrix = map {$_.Array}, @matrix0; + for (0..@matrix.end) -> $i { + if (@matrix[$i][0] == 0) { + for (0..@matrix[0].end) -> $j { + @matrix[$i][$j] = 1 - @matrix[$i][$j]; + } + } + } + my $t = (@matrix.elems / 2).Int; + for (1..@matrix[0].end) -> $i { + my $c = 0; + for (0..@matrix.end) -> $j { + if (@matrix[$j][$i] == 0) { + $c++; + } + } + if ($c > $t) { + for (0..@matrix.end) -> $j { + @matrix[$j][$i] = 1 - @matrix[$j][$i]; + } + } + } + my $tot = 0; + for @matrix -> @m { + my $p = 0; + for @m -> $n { + $p *= 2; + if ($n == 1) { + $p++; + } + } + $tot += $p; + } + return $tot; +} diff --git a/challenge-218/roger-bell-west/ruby/ch-1.rb b/challenge-218/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..2a52f10cf7 --- /dev/null +++ b/challenge-218/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,44 @@ +#! /usr/bin/ruby + +def maximumproduct(lst) + l = lst.sort + b = l.length + t = [] + 0.upto(3) do |i| + p = 1 + if i > 0 then + 0.upto(i-1) do |j| + p *= l[j] + end + end + if i < 3 then + (b - 3 + i).upto(b-1) do |j| + p *= l[j] + end + end + t.push(p) + end + return t.max +end + +require 'test/unit' + +class TestMaximumproduct < Test::Unit::TestCase + + def test_ex1 + assert_equal(6, maximumproduct([3, 1, 2])) + end + + def test_ex2 + assert_equal(24, maximumproduct([4, 1, 3, 2])) + end + + def test_ex3 + assert_equal(3, maximumproduct([-1, 0, 1, 3, 1])) + end + + def test_ex4 + assert_equal(216, maximumproduct([-8, 2, -9, 0, -4, 3])) + end + +end diff --git a/challenge-218/roger-bell-west/ruby/ch-2.rb b/challenge-218/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..5e032504ce --- /dev/null +++ b/challenge-218/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,52 @@ +#! /usr/bin/ruby + +def matrixscore(matrix0) + matrix = matrix0 + 0.upto(matrix.length-1) do |i| + if matrix[i][0] == 0 then + 0.upto(matrix[i].length-1) do |j| + matrix[i][j] = 1 - matrix[i][j] + end + end + end + t = matrix.length.div(2) + 1.upto(matrix[0].length-1) do |i| + c = 0 + 0.upto(matrix.length-1) do |j| + if matrix[j][i] == 0 then + c += 1 + end + end + if c > t then + 0.upto(matrix.length-1) do |j| + matrix[j][i] = 1 - matrix[j][i] + end + end + end + tot = 0 + matrix.each do |m| + p = 0 + m.each do |n| + p *= 2 + if n == 1 then + p += 1 + end + end + tot += p + end + return tot +end + +require 'test/unit' + +class TestMatrixscore < Test::Unit::TestCase + + def test_ex1 + assert_equal(39, matrixscore([[0, 0, 1, 1], [1, 0, 1, 0], [1, 1, 1, 0]])) + end + + def test_ex2 + assert_equal(1, matrixscore([[0]])) + end + +end diff --git a/challenge-218/roger-bell-west/rust/ch-1.rs b/challenge-218/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..65956f73be --- /dev/null +++ b/challenge-218/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,44 @@ +#! /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!(maximumproduct(vec![3, 1, 2]), 6); +} + +#[test] +fn test_ex2() { + assert_eq!(maximumproduct(vec![4, 1, 3, 2]), 24); +} + +#[test] +fn test_ex3() { + assert_eq!(maximumproduct(vec![-1, 0, 1, 3, 1]), 3); +} + +#[test] +fn test_ex4() { + assert_eq!(maximumproduct(vec![-8, 2, -9, 0, -4, 3]), 216); +} + +fn maximumproduct(lst: Vec) -> i32 { + let mut l = lst; + l.sort(); + let b = l.len(); + let mut t: Vec = Vec::new(); + for i in 0..=3 { + let mut p = 1; + if i > 0 { + for j in 0..i { + p *= l[j]; + } + } + if i < 3 { + for j in b - 3 + i..b { + p *= l[j]; + } + } + t.push(p); + } + t.into_iter().max().unwrap() +} diff --git a/challenge-218/roger-bell-west/rust/ch-2.rs b/challenge-218/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..f17213ce27 --- /dev/null +++ b/challenge-218/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,52 @@ +#! /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!( + matrixscore(vec![vec![0, 0, 1, 1], vec![1, 0, 1, 0], vec![1, 1, 1, 0]]), + 39 + ); +} + +#[test] +fn test_ex2() { + assert_eq!(matrixscore(vec![vec![0]]), 1); +} + +fn matrixscore(matrix0: Vec>) -> u32 { + let mut matrix = matrix0; + for i in 0..matrix.len() { + if matrix[i][0] == 0 { + for j in 0..matrix[i].len() { + matrix[i][j] = 1 - matrix[i][j]; + } + } + } + let t = matrix.len() / 2; + for i in 1..matrix[0].len() { + let mut c = 0; + for j in 0..matrix.len() { + if matrix[j][i] == 0 { + c += 1; + } + } + if c > t { + for j in 0..matrix.len() { + matrix[j][i] = 1 - matrix[j][i]; + } + } + } + let mut tot = 0; + for m in matrix { + let mut p = 0; + for n in m { + p *= 2; + if n == 1 { + p += 1; + } + } + tot += p; + } + tot +} diff --git a/challenge-218/roger-bell-west/tests.yaml b/challenge-218/roger-bell-west/tests.yaml new file mode 100644 index 0000000000..ed901a139a --- /dev/null +++ b/challenge-218/roger-bell-west/tests.yaml @@ -0,0 +1,49 @@ +--- +ch-1: + - function: maximumproduct + arguments: + - 3 + - 1 + - 2 + result: 6 + - arguments: + - 4 + - 1 + - 3 + - 2 + result: 24 + - arguments: + - -1 + - 0 + - 1 + - 3 + - 1 + result: 3 + - arguments: + - -8 + - 2 + - -9 + - 0 + - -4 + - 3 + result: 216 +ch-2: + - function: matrixscore + arguments: + - - 0 + - 0 + - 1 + - 1 + - - 1 + - 0 + - 1 + - 0 + - - 1 + - 1 + - 1 + - 0 + result: 39 + - arguments: + - - 0 + result: 1 + -- cgit