diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-10 16:57:57 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-10 16:57:57 +0000 |
| commit | b32c0ef48b233fc2b7dbe00b4ab67cdc5d5e23ce (patch) | |
| tree | 4db42d4d0d1e005779849f6a0d33ef824e21aad9 | |
| parent | 4e5a407712af3485999e3ad353019e7f6517c063 (diff) | |
| parent | b2433950e1225e4e0e1759e84fb2e626bbdec55a (diff) | |
| download | perlweeklychallenge-club-b32c0ef48b233fc2b7dbe00b4ab67cdc5d5e23ce.tar.gz perlweeklychallenge-club-b32c0ef48b233fc2b7dbe00b4ab67cdc5d5e23ce.tar.bz2 perlweeklychallenge-club-b32c0ef48b233fc2b7dbe00b4ab67cdc5d5e23ce.zip | |
Merge pull request #9382 from Firedrake/rogerbw-challenge-251
RogerBW solutions for challenge no. 251
21 files changed, 1100 insertions, 0 deletions
diff --git a/challenge-251/roger-bell-west/javascript/ch-1.js b/challenge-251/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..16d97f30e4 --- /dev/null +++ b/challenge-251/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,49 @@ +#! /usr/bin/node + +"use strict" + +function concat(a0, b0) { + if (b0 == 0) { + return 10 * a0; + } + let a = a0; + let b = b0; + while (b > 0) { + a *= 10; + b = Math.floor(b / 10); + } + return a + b0; +} + +function concatenationvalue(a) { + let t = 0; + let ms = Math.floor((a.length - 1) / 2); + for (let i = 0; i <= ms; i++) { + const j = a.length - 1 - i; + if (j == i) { + t += a[i]; + } else { + t += concat(a[i], a[j]); + } + } + return t; +} + +if (concatenationvalue([6, 12, 25, 1]) == 1286) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (concatenationvalue([10, 7, 31, 5, 2, 2]) == 489) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (concatenationvalue([1, 2, 10]) == 112) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-251/roger-bell-west/javascript/ch-2.js b/challenge-251/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..94f11a4824 --- /dev/null +++ b/challenge-251/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,53 @@ +#! /usr/bin/node + +"use strict" + +function luckynumbers (a) { + var maxs = new Set(); + for (let x = 0; x < a[0].length; x++) { + var max = [0, 0, 0]; + for (let y = 0; y < a.length; y++) { + if (a[y][x] > max[0]) { + max = [a[y][x], y, x]; + } + } + maxs.add(max); + } + var mins = new Set(); + for (let y = 0; y < a.length; y++) { + var min = [a[y][0], y, 0]; + for (let x = 0; x < a[0].length; x++) { + if (a[y][x] < min[0]) { + min = [a[y][x], y, x]; + } + } + mins.add(min); + } + for (let i of maxs) { + for (let j of mins) { + if (i[0] == j[0] && i[1] == j[1] && i[2] == j[2]) { + return i[0]; + } + } + } + return -1; +} + +if (luckynumbers([[3, 7, 9], [9, 11, 13], [15, 16, 17]]) == 15) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (luckynumbers([[1, 10, 4, 2], [9, 3, 8, 7], [15, 16, 17, 12]]) == 12) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (luckynumbers([[7, 8], [1, 2]]) == 7) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-251/roger-bell-west/kotlin/ch-1.kt b/challenge-251/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..7e0eb3c79a --- /dev/null +++ b/challenge-251/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,48 @@ +fun concat(a0: Int, b0: Int): Int { + if (b0 == 0) { + return 10 * a0 + } + var a = a0 + var b = b0 + while (b > 0) { + a *= 10 + b /= 10 + } + return a + b0 +} + +fun concatenationvalue(a: List<Int>): Int { + var t = 0 + for (i in 0..(a.size - 1) / 2) { + val j = a.size - 1 - i + if (j == i) { + t += a[i] + } else { + t += concat(a[i], a[j]) + } + } + return t +} + +fun main() { + + if (concatenationvalue(listOf(6, 12, 25, 1)) == 1286) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (concatenationvalue(listOf(10, 7, 31, 5, 2, 2)) == 489) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (concatenationvalue(listOf(1, 2, 10)) == 112) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-251/roger-bell-west/kotlin/ch-2.kt b/challenge-251/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..863bfebad4 --- /dev/null +++ b/challenge-251/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,50 @@ +fun luckynumbers (a: List<List<Int>>): Int { + var maxs = mutableSetOf<List<Int>>() + for (x in 0 .. a[0].size - 1) { + var max = listOf(0, 0, 0) + for (y in 0 .. a.size - 1) { + if (a[y][x] > max[0]) { + max = listOf(a[y][x], y, x) + } + } + maxs.add(max) + } + var mins = mutableSetOf<List<Int>>() + for (y in 0 .. a.size - 1) { + var min = listOf(a[y][0], y, 0) + for (x in 0 .. a[0].size - 1) { + if (a[y][x] < min[0]) { + min = listOf(a[y][x], y, x) + } + } + mins.add(min) + } + val i = ArrayList(maxs.intersect(mins)) + if (i.size > 0) { + return i[0][0] + } + return -1 +} + +fun main() { + + if (luckynumbers(listOf(listOf(3, 7, 9), listOf(9, 11, 13), listOf(15, 16, 17))) == 15) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (luckynumbers(listOf(listOf(1, 10, 4, 2), listOf(9, 3, 8, 7), listOf(15, 16, 17, 12))) == 12) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (luckynumbers(listOf(listOf(7, 8), listOf(1, 2))) == 7) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-251/roger-bell-west/lua/ch-1.lua b/challenge-251/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..e893bf6fbd --- /dev/null +++ b/challenge-251/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,49 @@ +#! /usr/bin/lua + +function concat(a0, b0) + if b0 == 0 then + return 10 * a0 + end + local a = a0 + local b = b0 + while b > 0 do + a = a * 10 + b = math.floor(b / 10) + end + return a + b0 +end + +function concatenationvalue(a) + local t = 0 + for i = 1, math.floor((#a + 1) / 2) do + local j = #a + 1 - i + if j == i then + t = t + a[i] + else + t = t + concat(a[i], a[j]) + end + end + return t +end + +if concatenationvalue({6, 12, 25, 1}) == 1286 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if concatenationvalue({10, 7, 31, 5, 2, 2}) == 489 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if concatenationvalue({1, 2, 10}) == 112 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-251/roger-bell-west/lua/ch-2.lua b/challenge-251/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..b6f31930c0 --- /dev/null +++ b/challenge-251/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,54 @@ +#! /usr/bin/lua + +function luckynumbers(a) + local maxs = {} + for x = 1, #(a[1]) do + local max = {0, 0, 0} + for y = 1, #a do + if a[y][x] > max[1] then + max = {a[y][x], y, x} + end + end + table.insert(maxs, max) + end + local mins = {} + for y = 1, #a do + local min = {a[y][1], y, 1} + for x = 1, #(a[1]) do + if a[y][x] < min[1] then + min = {a[y][x], y, x} + end + end + table.insert(mins, min) + end + for i, v in ipairs(mins) do + for j, w in ipairs(maxs) do + if v[1] == w[1] and v[2] == w[2] and v[3] == w[3] then + return v[1] + end + end + end + return -1 +end + +if luckynumbers({{3, 7, 9}, {9, 11, 13}, {15, 16, 17}}) == 15 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if luckynumbers({{1, 10, 4, 2}, {9, 3, 8, 7}, {15, 16, 17, 12}}) == 12 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if luckynumbers({{7, 8}, {1, 2}}) == 7 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-251/roger-bell-west/perl/ch-1.pl b/challenge-251/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..8dab36aa12 --- /dev/null +++ b/challenge-251/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,37 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(concatenationvalue([6, 12, 25, 1]), 1286, 'example 1'); +is(concatenationvalue([10, 7, 31, 5, 2, 2]), 489, 'example 2'); +is(concatenationvalue([1, 2, 10]), 112, 'example 3'); + +sub concat($a0, $b0) { + if ($b0 == 0) { + return 10 * $a0; + } + my $a = $a0; + my $b = $b0; + while ($b > 0) { + $a *= 10; + $b = int($b/10); + } + return $a + $b0; +} + +sub concatenationvalue($a) { + my $t = 0; + foreach my $i (0 .. int($#{$a} / 2)) { + my $j = $#{$a} - $i; + if ($j == $i) { + $t += $a->[$i]; + } else { + $t += concat($a->[$i], $a->[$j]); + } + } + return $t; +} diff --git a/challenge-251/roger-bell-west/perl/ch-2.pl b/challenge-251/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..157e4aa828 --- /dev/null +++ b/challenge-251/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,42 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(luckynumbers([[3, 7, 9], [9, 11, 13], [15, 16, 17]]), 15, 'example 1'); +is(luckynumbers([[1, 10, 4, 2], [9, 3, 8, 7], [15, 16, 17, 12]]), 12, 'example 2'); +is(luckynumbers([[7, 8], [1, 2]]), 7, 'example 3'); + +use Storable qw(freeze thaw); + +sub luckynumbers($a) { + my $maxs = {}; + foreach my $x (0 .. $#{$a->[0]}) { + my $max = [0, 0, 0]; + foreach my $y (0 .. $#{$a}) { + if ($a->[$y][$x] > $max->[0]) { + $max = [$a->[$y][$x], $y, $x]; + } + } + $maxs->{freeze($max)} = 1; + } + my $mins = {}; + foreach my $y (0 .. $#{$a}) { + my $min = [$a->[$y][0], $y, 0]; + foreach my $x (0 .. $#{$a->[0]}) { + if ($a->[$y][$x] < $min->[0]) { + $min = [$a->[$y][$x], $y, $x]; + } + } + $mins->{freeze($min)} = 1; + } + foreach my $i (keys %{$maxs}) { + if (exists $mins->{$i}) { + return thaw($i)->[0]; + } + } + return -1; +} diff --git a/challenge-251/roger-bell-west/postscript/ch-1.ps b/challenge-251/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..ee5715d948 --- /dev/null +++ b/challenge-251/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,79 @@ +%!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 + +/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 + +/concat { + 0 dict begin + /b0 exch def + /a0 exch def + b0 0 eq { + 10 a0 mul + } { + /a a0 def + /b b0 def + { + b 0 le { + exit + } if + /a a 10 mul def + /b b 10 idiv def + } loop + a b0 add + } ifelse + end +} bind def + +/concatenationvalue { + 0 dict begin + /a exch def + 0 + 0 1 a length 1 sub 2 idiv { + /i exch def + /j a length 1 sub i sub def + i j eq { + a i get add + } { + a i get a j get concat add + } ifelse + } for +} bind def + +(concatenationvalue) test.start +[6 12 25 1] concatenationvalue 1286 eq test +[10 7 31 5 2 2] concatenationvalue 489 eq test +[1 2 10] concatenationvalue 112 eq test +test.end diff --git a/challenge-251/roger-bell-west/postscript/ch-2.ps b/challenge-251/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..9020e475b0 --- /dev/null +++ b/challenge-251/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,149 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 + +/deepeq { + 2 dict begin + /a exch def + /b exch def + a type b type eq { + a type /dicttype eq { + a length b length eq { + << + a { + pop + true + } forall + b { + pop + true + } forall + >> + true exch + { + pop + dup a exch known { + dup b exch known { + dup a exch get exch b exch get deepeq not { + pop false + } if + } { + false + } ifelse + } { + false + } ifelse + } forall + } { + false + } ifelse + } { + a type dup /arraytype eq exch /stringtype eq or { + a length b length eq { + true + 0 1 a length 1 sub { + dup a exch get exch b exch get deepeq not { + pop false + exit + } if + } for + } { + false + } ifelse + } { + a b eq + } ifelse + } ifelse + } { + false + } ifelse + end +} bind def + +/test { + /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 + +/luckynumbers { + 0 dict begin + /a exch def + /maxs [ + 0 1 a 0 get length 1 sub { + /x exch def + /max [ 0 0 0 ] def + 0 1 a length 1 sub { + /y exch def + a y get x get max 0 get gt { + /max [ a y get x get y x ] def + } if + } for + max + } for + ] def + /mins [ + 0 1 a length 1 sub { + /y exch def + /min [ a y get 0 get y 0 ] def + 0 1 a 0 get length 1 sub { + /x exch def + a y get x get min 0 get lt { + /min [ a y get x get y x ] def + } if + } for + min + } for + ] def + -1 + /ex false def + maxs { + /i exch def + mins { + /j exch def + i j deepeq { + pop i 0 get + /ex true def + exit + } if + } forall + ex { + exit + } if + } forall + end +} bind def + +(luckynumbers) test.start +[[3 7 9] [9 11 13] [15 16 17]] luckynumbers 15 eq test +[[1 10 4 2] [9 3 8 7] [15 16 17 12]] luckynumbers 12 eq test +[[7 8] [1 2]] luckynumbers 7 eq test +test.end diff --git a/challenge-251/roger-bell-west/python/ch-1.py b/challenge-251/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..8f81c81b2e --- /dev/null +++ b/challenge-251/roger-bell-west/python/ch-1.py @@ -0,0 +1,36 @@ +#! /usr/bin/python3 + +def concat(a0, b0): + if b0 == 0: + return 10 * a0 + a = a0 + b = b0 + while b > 0: + a *= 10 + b = int(b / 10) + return a + b0 + +def concatenationvalue(a): + t = 0 + for i in range(int((len(a) - 1)/2) + 1): + j = len(a) - 1 - i + if j == i: + t += a[i] + else: + t += concat(a[i], a[j]) + return t + +import unittest + +class TestConcatenationvalue(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(concatenationvalue([6, 12, 25, 1]), 1286, 'example 1') + + def test_ex2(self): + self.assertEqual(concatenationvalue([10, 7, 31, 5, 2, 2]), 489, 'example 2') + + def test_ex3(self): + self.assertEqual(concatenationvalue([1, 2, 10]), 112, 'example 3') + +unittest.main() diff --git a/challenge-251/roger-bell-west/python/ch-2.py b/challenge-251/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..9de893dc56 --- /dev/null +++ b/challenge-251/roger-bell-west/python/ch-2.py @@ -0,0 +1,36 @@ +#! /usr/bin/python3 + +def luckynumbers(a): + maxs = set() + for x in range(len(a[0])): + max = (0, 0, 0) + for y in range(len(a)): + if a[y][x] > max[0]: + max = (a[y][x], y, x) + maxs.add(max) + mins = set() + for y in range(len(a)): + min = (a[y][0], y, 0) + for x in range(len(a[0])): + if a[y][x] < min[0]: + min = (a[y][x], y, x) + mins.add(min) + i = list(maxs.intersection(mins)) + if len(i) > 0: + return i[0][0] + return -1 + +import unittest + +class TestLuckynumbers(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(luckynumbers([[3, 7, 9], [9, 11, 13], [15, 16, 17]]), 15, 'example 1') + + def test_ex2(self): + self.assertEqual(luckynumbers([[1, 10, 4, 2], [9, 3, 8, 7], [15, 16, 17, 12]]), 12, 'example 2') + + def test_ex3(self): + self.assertEqual(luckynumbers([[7, 8], [1, 2]]), 7, 'example 3') + +unittest.main() diff --git a/challenge-251/roger-bell-west/raku/ch-1.p6 b/challenge-251/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..696eda5c84 --- /dev/null +++ b/challenge-251/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,35 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(concatenationvalue([6, 12, 25, 1]), 1286, 'example 1'); +is(concatenationvalue([10, 7, 31, 5, 2, 2]), 489, 'example 2'); +is(concatenationvalue([1, 2, 10]), 112, 'example 3'); + +sub concat($a0, $b0) { + if ($b0 == 0) { + return 10 * $a0; + } + my $a = $a0; + my $b = $b0; + while ($b > 0) { + $a *= 10; + $b = floor($b/10); + } + return $a + $b0; +} + +sub concatenationvalue(@a) { + my $t = 0; + for 0 .. floor((@a.elems-1) / 2) -> $i { + my $j = @a.elems - 1 - $i; + if ($j == $i) { + $t += @a[$i]; + } else { + $t += concat(@a[$i], @a[$j]); + } + } + return $t +} diff --git a/challenge-251/roger-bell-west/raku/ch-2.p6 b/challenge-251/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..93f748d9f1 --- /dev/null +++ b/challenge-251/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,42 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(luckynumbers([[3, 7, 9], [9, 11, 13], [15, 16, 17]]), 15, 'example 1'); +is(luckynumbers([[1, 10, 4, 2], [9, 3, 8, 7], [15, 16, 17, 12]]), 12, 'example 2'); +is(luckynumbers([[7, 8], [1, 2]]), 7, 'example 3'); + +sub luckynumbers(@a) { + my $maxs = SetHash.new; + for 0 .. @a[0].end() -> $x { + my $max = [0, 0, 0]; + for 0 .. @a.end -> $y { + if @a[$y][$x] > $max[0] { + $max = [@a[$y][$x], $y, $x]; + } + } + $maxs{$max}++; + } + my $mins = SetHash.new; + for 0 .. @a.end() -> $y { + my $min = [@a[$y][0], $y, 0]; + for 0 .. @a[0].end -> $x { + if @a[$y][$x] < $min[0] { + $min = [@a[$y][$x], $y, $x]; + } + } + $mins{$min}++; + } + for $maxs.keys -> $i { + for $mins.keys -> $j { + if ($i[0] == $j[0] && + $i[1] == $j[1] && + $i[2] == $j[2]) { + return $i[0]; + } + } + } + return -1; +} diff --git a/challenge-251/roger-bell-west/ruby/ch-1.rb b/challenge-251/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..2154ed2189 --- /dev/null +++ b/challenge-251/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,45 @@ +#! /usr/bin/ruby + +def concat(a0, b0) + if (b0 == 0) then + return 10 * a0 + end + a = a0 + b = b0 + while (b > 0) do + a *= 10; + b = (b / 10).floor + end + return a + b0 +end + +def concatenationvalue(a) + t = 0 + 0.upto(((a.length - 1) / 2).floor) do |i| + j = a.length - 1 - i + if (j == i) then + t += a[i] + else + t += concat(a[i], a[j]) + end + end + return t +end + +require 'test/unit' + +class TestConcatenationvalue < Test::Unit::TestCase + + def test_ex1 + assert_equal(1286, concatenationvalue([6, 12, 25, 1])) + end + + def test_ex2 + assert_equal(489, concatenationvalue([10, 7, 31, 5, 2, 2])) + end + + def test_ex3 + assert_equal(112, concatenationvalue([1, 2, 10])) + end + +end diff --git a/challenge-251/roger-bell-west/ruby/ch-2.rb b/challenge-251/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..4c7966b290 --- /dev/null +++ b/challenge-251/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,49 @@ +#! /usr/bin/ruby + +require 'set' + +def luckynumbers(a) + maxs = Set.new + 0.upto(a[0].length - 1) do |x| + max = [0, 0, 0] + 0.upto(a.length - 1) do |y| + if a[y][x] > max[0] then + max = [a[y][x], y, x] + end + end + maxs.add(Marshal.dump(max)) + end + mins = Set.new + 0.upto(a.length - 1) do |y| + min = [a[y][0], y, 0] + 0.upto(a[0].length - 1) do |x| + if a[y][x] < min[0] then + min = [a[y][x], y, x] + end + end + mins.add(Marshal.dump(min)) + end + i = (maxs & mins).to_a + if i.length > 0 then + return Marshal.load(i[0])[0] + end + return -1 +end + +require 'test/unit' + +class TestLuckynumbers < Test::Unit::TestCase + + def test_ex1 + assert_equal(15, luckynumbers([[3, 7, 9], [9, 11, 13], [15, 16, 17]])) + end + + def test_ex2 + assert_equal(12, luckynumbers([[1, 10, 4, 2], [9, 3, 8, 7], [15, 16, 17, 12]])) + end + + def test_ex3 + assert_equal(7, luckynumbers([[7, 8], [1, 2]])) + end + +end diff --git a/challenge-251/roger-bell-west/rust/ch-1.rs b/challenge-251/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..d6dc693d03 --- /dev/null +++ b/challenge-251/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,43 @@ +#! /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!(concatenationvalue(vec![6, 12, 25, 1]), 1286); +} + +#[test] +fn test_ex2() { + assert_eq!(concatenationvalue(vec![10, 7, 31, 5, 2, 2]), 489); +} + +#[test] +fn test_ex3() { + assert_eq!(concatenationvalue(vec![1, 2, 10]), 112); +} + +fn concat(a0: u32, b0: u32) -> u32 { + if b0 == 0 { + return 10 * a0; + } + let mut a = a0; + let mut b = b0; + while b > 0 { + a *= 10; + b /= 10; + } + a + b0 +} + +fn concatenationvalue(a: Vec<u32>) -> u32 { + let mut t = 0; + for i in 0 ..= (a.len()-1) / 2 { + let j = a.len() - 1 - i; + if j == i { + t += a[i]; + } else { + t += concat(a[i], a[j]); + } + } + t +} diff --git a/challenge-251/roger-bell-west/rust/ch-2.rs b/challenge-251/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..ffc50172c1 --- /dev/null +++ b/challenge-251/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,47 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +use std::collections::HashSet; + +#[test] +fn test_ex1() { + assert_eq!(luckynumbers(vec![vec![3, 7, 9], vec![9, 11, 13], vec![15, 16, 17]]), 15); +} + +#[test] +fn test_ex2() { + assert_eq!(luckynumbers(vec![vec![1, 10, 4, 2], vec![9, 3, 8, 7], vec![15, 16, 17, 12]]), 12); +} + +#[test] +fn test_ex3() { + assert_eq!(luckynumbers(vec![vec![7, 8], vec![1, 2]]), 7); +} + +fn luckynumbers(a: Vec<Vec<i32>>) -> i32 { + let mut maxs = HashSet::new(); + for x in 0 .. a[0].len() { + let mut max = (0, 0, 0); + f |
