diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-05-17 10:17:31 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-17 10:17:31 +0100 |
| commit | e21f81d885ef9aaf998d7aca0027c4bef9739a45 (patch) | |
| tree | eebf63f0060c68ee84db899083e0cf929846bec2 | |
| parent | ea6fd8fcc63818c74f931498c2aae111af37701b (diff) | |
| parent | 3a1fee4bd9bb3cec787172940e0a8a2e3cd8696b (diff) | |
| download | perlweeklychallenge-club-e21f81d885ef9aaf998d7aca0027c4bef9739a45.tar.gz perlweeklychallenge-club-e21f81d885ef9aaf998d7aca0027c4bef9739a45.tar.bz2 perlweeklychallenge-club-e21f81d885ef9aaf998d7aca0027c4bef9739a45.zip | |
Merge pull request #8092 from Firedrake/rogerbw-challenge-217
RogerBW solutions for challenge no. 217
19 files changed, 1061 insertions, 0 deletions
diff --git a/challenge-217/roger-bell-west/javascript/ch-1.js b/challenge-217/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..17583437bc --- /dev/null +++ b/challenge-217/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,28 @@ +#! /usr/bin/node + +"use strict" + +function sortedmatrix(matrix) { + var n = matrix.flat(); + n.sort(); + return n[2]; +} + +if (sortedmatrix([[3, 1, 2], [5, 2, 4], [0, 1, 3]]) == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (sortedmatrix([[2, 1], [4, 5]]) == 4) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (sortedmatrix([[1, 0, 3], [0, 0, 0], [1, 2, 1]]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-217/roger-bell-west/javascript/ch-2.js b/challenge-217/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..35490cc9bf --- /dev/null +++ b/challenge-217/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,69 @@ +#! /usr/bin/node + +"use strict" + +function range(from, to) { + let a = new Array(to - from + 1); + for (let i = from; i <= to; i++) { + a.push(i); + } + return a; +} + +function maxnumber(lst) { + const po = lst.map (x => x.toString()); + const pl = Math.max(...po.map (x => x.length)); + let pm = []; + for (let so of po) { + let sm = so; + if (so.length < pl) { + const c = so.slice(-1); + for (let _i = so.length - pl; _i < 0; _i++) { + sm += c; + } + } + pm.push(sm); + } + let pi = range(0, pm.length - 1); + pi.sort(function(a, b) { + if (pm[b] > pm[a]) return 1; + if (pm[b] < pm[a]) return -1; + return 0; + }); + let out = ""; + for (let st of pi) { + out += po[st]; + } + return parseInt(out); +} + +if (maxnumber([1, 23]) == 231) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maxnumber([10, 3, 2]) == 3210) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maxnumber([31, 2, 4, 10]) == 431210) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maxnumber([5, 11, 4, 1, 2]) == 542111) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maxnumber([1, 10]) == 110) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-217/roger-bell-west/kotlin/ch-1.kt b/challenge-217/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..3ecfdce2e8 --- /dev/null +++ b/challenge-217/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,28 @@ +fun sortedmatrix(matrix: List<List<Int>>): Int { + var n = ArrayList(matrix.flatten()) + n.sort() + return n[2] +} + +fun main() { + + if (sortedmatrix(listOf(listOf(3, 1, 2), listOf(5, 2, 4), listOf(0, 1, 3))) == 1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (sortedmatrix(listOf(listOf(2, 1), listOf(4, 5))) == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (sortedmatrix(listOf(listOf(1, 0, 3), listOf(0, 0, 0), listOf(1, 2, 1))) == 0) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-217/roger-bell-west/kotlin/ch-2.kt b/challenge-217/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..dd26b4a522 --- /dev/null +++ b/challenge-217/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,57 @@ +fun maxnumber(lst: List<Int>): Int { + val po = lst.map {it.toString()} + val pl = po.map {it.length}.maxOrNull()!! + var pm = ArrayList<String>() + for (so in po) { + var sm = so + if (so.length < pl) { + val c = so.lastOrNull()!! + for (_i in 1 .. pl - so.length) { + sm += c + } + } + pm.add(sm) + } + var pi = ArrayList((0 .. pm.size - 1).toList()) + pi.sortByDescending {pm[it]} + var out = "" + for (st in pi) { + out += po[st] + } + return out.toInt() +} + +fun main() { + + if (maxnumber(listOf(1, 23)) == 231) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxnumber(listOf(10, 3, 2)) == 3210) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxnumber(listOf(31, 2, 4, 10)) == 431210) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxnumber(listOf(5, 11, 4, 1, 2)) == 542111) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxnumber(listOf(1, 10)) == 110) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-217/roger-bell-west/lua/ch-1.lua b/challenge-217/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..7a74acc591 --- /dev/null +++ b/challenge-217/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,34 @@ +#! /usr/bin/lua + +function sortedmatrix(matrix) + local n = {} + for _a, v1 in ipairs(matrix) do + for _b, v2 in ipairs(v1) do + table.insert(n, v2) + end + end + table.sort(n) + return n[3] +end + +if sortedmatrix({{3, 1, 2}, {5, 2, 4}, {0, 1, 3}}) == 1 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if sortedmatrix({{2, 1}, {4, 5}}) == 4 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if sortedmatrix({{1, 0, 3}, {0, 0, 0}, {1, 2, 1}}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-217/roger-bell-west/lua/ch-2.lua b/challenge-217/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..fdff911531 --- /dev/null +++ b/challenge-217/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,66 @@ +#! /usr/bin/lua + +function maxnumber(lst) + local po = {} + local pl = 0 + for _, v in ipairs(lst) do + local ss = tostring(v) + table.insert(po, ss) + pl = math.max(pl, #ss) + end + local pm = {} + for _, so in ipairs(po) do + local sm = so + local c = string.sub(sm, -1) + while string.len(sm) < pl do + sm = sm .. c + end + table.insert(pm, sm) + end + local pi = {} + for i = 1, #pm do + table.insert(pi, i) + end + table.sort(pi, function (a, b) return pm[b] < pm[a] end) + local out = "" + for _, st in ipairs(pi) do + out = out .. po[st] + end + return tonumber(out) +end + +if maxnumber({1, 23}) == 231 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maxnumber({10, 3, 2}) == 3210 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maxnumber({31, 2, 4, 10}) == 431210 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maxnumber({5, 11, 4, 1, 2}) == 542111 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maxnumber({1, 10}) == 110 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-217/roger-bell-west/perl/ch-1.pl b/challenge-217/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..be7ca0e2c3 --- /dev/null +++ b/challenge-217/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,16 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(sortedmatrix([[3, 1, 2], [5, 2, 4], [0, 1, 3]]), 1, 'example 1'); +is(sortedmatrix([[2, 1], [4, 5]]), 4, 'example 2'); +is(sortedmatrix([[1, 0, 3], [0, 0, 0], [1, 2, 1]]), 0, 'example 3'); + +sub sortedmatrix($matrix) { + my @n = map {@$_} @{$matrix}; + return (sort {$a <=> $b} @n)[2]; +} diff --git a/challenge-217/roger-bell-west/perl/ch-2.pl b/challenge-217/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..4d2c348db8 --- /dev/null +++ b/challenge-217/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,36 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is(maxnumber([1, 23]), 231, 'example 1'); +is(maxnumber([10, 3, 2]), 3210, 'example 2'); +is(maxnumber([31, 2, 4, 10]), 431210, 'example 3'); +is(maxnumber([5, 11, 4, 1, 2]), 542111, 'example 4'); +is(maxnumber([1, 10]), 110, 'example 5'); + +use List::Util qw(max); + +sub maxnumber($lst) { + my @po = map{"" . $_} @{$lst}; + my $pl = max(map {length($_)} @po); + my @pm; + foreach my $so (@po) { + my $sm = $so; + my @q = split '',$so; + if (scalar @q < $pl) { + my $c = $q[-1]; + $sm .= $c x ($pl - scalar @q); + } + push @pm, $sm; + } + my $out = ''; + my @pi = (0..$#pm); + foreach my $st (sort {$pm[$b] cmp $pm[$a]} @pi) { + $out .= $po[$st]; + } + return 0 + $out; +} diff --git a/challenge-217/roger-bell-west/postscript/ch-1.ps b/challenge-217/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..75b24b3252 --- /dev/null +++ b/challenge-217/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,139 @@ +%!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 + +/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 { + { quicksort.cmp } quicksort.with_comparator +} 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.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 + +/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 + +/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 + + +% end included library code + +/sortedmatrix { + [ exch + { + aload pop + } forall + ] quicksort 2 get +} bind def + +(sortedmatrix) test.start +[[3 1 2] [5 2 4] [0 1 3]] sortedmatrix 1 eq test +[[2 1] [4 5]] sortedmatrix 4 eq test +[[1 0 3] [0 0 0] [1 2 1]] sortedmatrix 0 eq test +test.end diff --git a/challenge-217/roger-bell-west/postscript/ch-2.ps b/challenge-217/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..1391a580d7 --- /dev/null +++ b/challenge-217/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,271 @@ +%!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 + +/reduce { % array proc -> value + 2 dict begin + /p exch def + /a exch def + a 0 get + 1 1 a length 1 sub { + a exch get + p + } for + end +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/strjoin % [(a) (b) (c)] (j) -> (ajbjc) +{ + 3 dict begin + /j exch def + dup 0 get /out exch def + /first true def + { + first { + pop + /first false def + } { + out j strconcat + exch strconcat + /out exch def + } ifelse + } forall + out + 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.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 + + +/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 + +/quicksort.with_keygen { % [ a c b ] { keygen } -> [ a b c ] + 3 dict begin + /kg exch def + /arr exch def + /kl << arr { + dup kg + } forall >> def + arr { + exch kl exch get exch kl exch get quicksort.cmp + } quicksort.with_comparator +} bind def + +/alloccvs { + 2 dict begin + /n exch def + /a 1 def + n + dup 0 lt { + /a a 1 add def + neg + } if + { + dup 10 lt { + exit + } if + /a a 1 add def + 10 idiv + } loop + pop + n a string cvs + end +} 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 + + +/listmax { + { max } reduce +} bind def + +/quicksort.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} 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 + +/quicksort.with_keylist { % [ a c b ] [ keylist ] -> [ a b c ] + 4 dict begin + /kg exch def + /arr exch def + /kl << + 0 1 arr length 1 sub { + /i exch def + arr i get + kg i get + } for + >> def + arr { + exch kl exch get exch kl exch get quicksort.cmp + } quicksort.with_comparator +} bind def + +/reverse { + 1 dict begin + dup length /l exch def + [ exch + aload pop + 2 1 l { + -1 roll + } for + ] + end +} bind def + +/strconcat % (a) (b) -> (ab) +{ exch dup length + 2 index length add string + dup dup 4 2 roll copy length + 4 -1 roll putinterval +} bind def + +/map { % array proc -> array + 2 dict begin + /p exch def + [ exch + { + p + } forall + ] + end +} bind def + + +% end included library code + +/maxnumber { + 8 dict begin + /lst exch def + /po lst { alloccvs } map def + /pl po { length } map listmax def + /pm [ + po { + /so exch def + /sm so def + so length pl le { + /c so dup length 1 sub get def + /sm pl string def + 0 1 pl 1 sub { + sm exch c put + } for + so sm copy pop + } if + sm + } forall + ] def + po pm quicksort.with_keylist reverse () strjoin cvi + end +} bind def + +(maxnumber) test.start +[1 23] maxnumber 231 eq test +[10 3 2] maxnumber 3210 eq test +[31 2 4 10] maxnumber 431210 eq test +[5 11 4 1 2] maxnumber 542111 eq test +[1 10] maxnumber 110 eq test +test.end diff --git a/challenge-217/roger-bell-west/python/ch-1.py b/challenge-217/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..0d35a4a1f4 --- /dev/null +++ b/challenge-217/roger-bell-west/python/ch-1.py @@ -0,0 +1,23 @@ +#! /usr/bin/python3 + +from itertools import chain + +def sortedmatrix(matrix): + n = list(chain.from_iterable(matrix)) + n.sort() + return n[2] + +import unittest + +class TestSortedmatrix(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(sortedmatrix([[3, 1, 2], [5, 2, 4], [0, 1, 3]]), 1, 'example 1') + + def test_ex2(self): + self.assertEqual(sortedmatrix([[2, 1], [4, 5]]), 4, 'example 2') + + def test_ex3(self): + self.assertEqual(sortedmatrix([[1, 0, 3], [0, 0, 0], [1, 2, 1]]), 0, 'example 3') + +unittest.main() diff --git a/challenge-217/roger-bell-west/python/ch-2.py b/challenge-217/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..91a3bdc714 --- /dev/null +++ b/challenge-217/roger-bell-west/python/ch-2.py @@ -0,0 +1,35 @@ +#! /usr/bin/python3 + +def maxnumber(lst): + po = [str(i) for i in lst] + pl = max(len(i) for i in po) + pm = [] + for so in po: + sm = so + while len(sm) < pl: + sm += sm[-1] + pm.append(sm) + pi = list(range(len(pm))) + pi.sort(key = lambda i: pm[i], reverse = True) + return int("".join(po[st] for st in pi)) + +import unittest + +class TestMaxnumber(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(maxnumber([1, 23]), 231, 'example 1') + + def test_ex2(self): + self.assertEqual(maxnumber([10, 3, 2]), 3210, 'example 2') + + def test_ex3(self): + self.assertEqual(maxnumber([31, 2, 4, 10]), 431210, 'example 3') + + def test_ex4(self): + self.assertEqual(maxnumber([5, 11, 4, 1, 2]), 542111, 'example 4') + + def test_ex5(self): + self.assertEqual(maxnumber([1, 10]), 110, 'example 5') + +unittest.main() diff --git a/challenge-217/roger-bell-west/raku/ch-1.p6 b/challenge-217/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..3052768ccd --- /dev/null +++ b/challenge-217/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,14 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(sortedmatrix([[3, 1, 2], [5, 2, 4], [0, 1, 3]]), 1, 'example 1'); +is(sortedmatrix([[2, 1], [4, 5]]), 4, 'example 2'); +is(sortedmatrix([[1, 0, 3], [0, 0, 0], [1, 2, 1]]), 0, 'example 3'); + +sub sortedmatrix(@matrix) { + my @n = @matrix[*;*]; + return @n.sort({$^a <=> $^b})[2]; +} diff --git a/challenge-217/roger-bell-west/raku/ch-2.p6 b/challenge-217/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..c7b28e8f94 --- /dev/null +++ b/challenge-217/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,32 @@ +#! /usr/bin/raku + +use Test; + +plan 5; + +is(maxnumber([1, 23]), 231, 'example 1'); +is(maxnumber([10, 3, 2]), 3210, 'example 2'); +is(maxnumber([31, 2, 4, 10]), 431210, 'example 3'); +is(maxnumber([5, 11, 4, 1, 2]), 542111, 'example 4'); +is(maxnumber([1, 10]), 110, 'example 5'); + +sub maxnumber(@lst) { + my @po = @lst.map({Str($_)}); + my $pl = @po.map({chars($_)}).max; + my @pm; + for @po -> $so { + my $sm = $so; + my @q = $so.comb; + if (@q.elems < $pl) { + my $c = @q[*-1]; + $sm ~= $c x ($pl - @q.elems); + } + @pm.push($sm); + } + my $out = ''; + my @pi = (0..@pm.end); + for @pi.sort({@pm[$^b] cmp @pm[$^a]}) -> $st { + $out ~= @po[$st]; + } + return 0 + $out; +} diff --git a/challenge-217/roger-bell-west/ruby/ch-1.rb b/challenge-217/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..a1b12395e2 --- /dev/null +++ b/challenge-217/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,25 @@ +#! /usr/bin/ruby + +def sortedmatrix(matrix) + n = matrix.flatten + n.sort! + return n[2] +end + +require 'test/unit' + +class TestSortedmatrix < Test::Unit::TestCase + + def test_ex1 + assert_equal(1, sortedmatrix([[3, 1, 2], [5, 2, 4], [0, 1, 3]])) + end + + def test_ex2 + assert_equal(4, sortedmatrix([[2, 1], [4, 5]])) + end + + def test_ex3 + assert_equal(0, sortedmatrix([[1, 0, 3], [0, 0, 0], [1, 2, 1]])) + end + +end diff --git a/challenge-217/roger-bell-west/ruby/ch-2.rb b/challenge-217/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..21d8191761 --- /dev/null +++ b/challenge-217/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,48 @@ +#! /usr/bin/ruby + +def maxnumber(lst) + po = lst.collect {|i| i.to_s} + pl = po.collect {|i| i.length}.max + pm = [] + po.each do |so| + sm = so + while sm.length < pl do + sm += sm[-1] + end + pm.push(sm) + end + pi = 0.upto(pm.length - 1).to_a + pi.sort_by! { |i| pm[i] } + pi = pi.reverse + out = "" + pi.each do |st| + out += po[st] + end + return out.to_i +end + +require 'test/unit' + +class TestMaxnumber < Test::Unit::TestCase + + def test_ex1 + assert_equal(231, maxnumber([1, 23])) + end + + def test_ex2 + assert_equal(3210, maxnumber([10, 3, 2])) + end + + def test_ex3 + assert_equal(431210, maxnumber([31, 2, 4, 10])) + end + + def test_ex4 + assert_equal(542111, maxnumber([5, 11, 4, 1, 2])) + end + + def test_ex5 + assert_equal(110, maxnumber([1, 10])) + end + +end diff --git a/challenge-217/roger-bell-west/rust/ch-1.rs b/challenge-217/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..0c6db23a7f --- /dev/null +++ b/challenge-217/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,29 @@ +#! /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!( + sortedmatrix(vec![vec![3, 1, 2], vec![5, 2, 4], vec![0, 1, 3]]), + 1 + ); +} + +#[test] +fn test_ex2() { + assert_eq!(sortedmatrix(vec![vec![2, 1], vec![4, 5]]), 4); +} + +#[test] +fn test_ex3() { + assert_eq!( + sortedmatrix(vec![vec![1, 0, 3], vec![0, 0, 0], vec![1, 2, 1]]), + 0 + ); +} + +fn sortedmatrix(matrix: Vec<Vec<i32>>) -> i32 { + let mut n = matrix.iter().flatten().collect::<Vec<&i32>>(); + n.sort(); + *n[2] +} diff --git a/challenge-217/roger-bell-west/rust/ch-2.rs b/challenge-217/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..db9536a65f --- /dev/null +++ b/challenge-217/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!(maxnumber(vec![1, 23]), 231); +} + +#[test] +fn test_ex2() { + assert_eq!(maxnumber(vec![10, 3, 2]), 3210); +} + +#[test] +fn test_ex3() { + assert_eq!(maxnumber(vec![31, 2, 4, 10]), 431210); +} + +#[test] +fn test_ex4() { + assert_eq!(maxnumber(vec![5, 11, 4, 1, 2]), 542111); +} + +#[test] +fn test_ex5() { + assert_eq!(maxnumber(vec![1, 10]), 110); +} + +fn maxnumber(lst: Vec<u32>) -> u32 { + let po = lst.iter().map(|i| i.to_string()).collect::<Vec<String>>(); + |
