diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-28 17:43:45 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-28 17:43:45 +0000 |
| commit | 871f20d8ecc70f67143baa9961a74d142341e68d (patch) | |
| tree | 419f72e048a924f2dbfe0c2fcfc8ffa2b06d8678 | |
| parent | a0fba1995397464d5e3a2f24741f6400e6a872d7 (diff) | |
| parent | 071caa7f304d5327b17e86dacb100979c79e8cca (diff) | |
| download | perlweeklychallenge-club-871f20d8ecc70f67143baa9961a74d142341e68d.tar.gz perlweeklychallenge-club-871f20d8ecc70f67143baa9961a74d142341e68d.tar.bz2 perlweeklychallenge-club-871f20d8ecc70f67143baa9961a74d142341e68d.zip | |
Merge pull request #9161 from Firedrake/rogerbw-challenge-245
RogerBW solutions for challenge no. 245
21 files changed, 1071 insertions, 0 deletions
diff --git a/challenge-245/roger-bell-west/javascript/ch-1.js b/challenge-245/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..f3ea366d19 --- /dev/null +++ b/challenge-245/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,51 @@ +#! /usr/bin/node + +"use strict" + +// by Frank Tan +// https://stackoverflow.com/questions/38400594/javascript-deep-comparison +function deepEqual(a,b) +{ + if( (typeof a == 'object' && a != null) && + (typeof b == 'object' && b != null) ) + { + var count = [0,0]; + for( var key in a) count[0]++; + for( var key in b) count[1]++; + if( count[0]-count[1] != 0) {return false;} + for( var key in a) + { + if(!(key in b) || !deepEqual(a[key],b[key])) {return false;} + } + for( var key in b) + { + if(!(key in a) || !deepEqual(b[key],a[key])) {return false;} + } + return true; + } + else + { + return a === b; + } +} + +function sortlanguage(langs, popularities) { + let ix = Array(langs.length).fill().map((element, index) => index); + ix.sort(function(a,b) { + return popularities[a] - popularities[b]; + }); + return ix.map(n => langs[n]); +} + +if (deepEqual(sortlanguage(['perl', 'c', 'python'], [2, 1, 3]), ['c', 'perl', 'python'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(sortlanguage(['c++', 'haskell', 'java'], [1, 3, 2]), ['c++', 'java', 'haskell'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-245/roger-bell-west/javascript/ch-2.js b/challenge-245/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..e892748faa --- /dev/null +++ b/challenge-245/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,64 @@ +#! /usr/bin/node + +"use strict" + +function combinations(arr, k) { + let c = []; + for (let i = 0; i < k; i++) { + c.push(i); + } + c.push(arr.length); + c.push(0); + let out = []; + while (true) { + let inner = []; + for (let i = k-1; i >= 0; i--) { + inner.push(arr[c[i]]); + } + out.push(inner); + let j = 0; + while (c[j] + 1 == c[j + 1]) { + c[j] = j; + j += 1; + } + if (j >= k) { + break; + } + c[j] += 1; + } + return out; +} + +function largestofthree(digits) { + let ordered = [...digits]; + ordered.sort(function(a,b) { + return a-b; + }); + let mx = 0; + for (let n = ordered.length - 1; n >= 0; n--) { + for (let c of combinations(ordered, n + 1)) { + let t = 0; + for (let d of c) { + t *= 10 + t += d; + } + if (t > mx && t % 3 == 0) { + mx = t; + } + } + } + return mx; +} + +if (largestofthree([8, 1, 9]) == 981) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (largestofthree([8, 6, 7, 1, 0]) == 8760) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-245/roger-bell-west/kotlin/ch-1.kt b/challenge-245/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..497fb6c708 --- /dev/null +++ b/challenge-245/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,22 @@ +fun sortlanguage(langs: List<String>, popularities: List<Int>): List<String> { + var ix = ArrayList(generateSequence(0) { it + 1 }.take(langs.size).toList()) + ix.sortBy { popularities[it] } + return ix.map {n -> langs[n]} +} + +fun main() { + + if (sortlanguage(listOf("perl", "c", "python"), listOf(2, 1, 3)) == listOf("c", "perl", "python")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (sortlanguage(listOf("c++", "haskell", "java"), listOf(1, 3, 2)) == listOf("c++", "java", "haskell")) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-245/roger-bell-west/kotlin/ch-2.kt b/challenge-245/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..4f2e74575e --- /dev/null +++ b/challenge-245/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,61 @@ +fun combinations(arr: List<Int>, k: Int): List<List<Int>> { + var c = ArrayList<Int>() + for (i in 0 .. k-1) { + c.add(i) + } + c.add(arr.size) + c.add(0) + var out = ArrayList<List<Int>>() + while (true) { + var inner = ArrayList<Int>() + for (i in k-1 downTo 0) { + inner.add(arr[c[i]]) + } + out.add(inner.toList()) + var j = 0 + while (c[j] + 1 == c[j + 1]) { + c[j] = j + j += 1 + } + if (j >= k) { + break + } + c[j] += 1 + } + return out.toList() +} + +fun largestofthree(digits: List<Int>): Int { + val ordered = digits.sorted() + var mx = 0 + for (n in ordered.size - 1 downTo 0) { + for (c in combinations(ordered, n + 1)) { + var t = 0 + for (d in c) { + t *= 10 + t += d + } + if (t > mx && t % 3 == 0) { + mx = t + } + } + } + return mx +} + +fun main() { + + if (largestofthree(listOf(8, 1, 9)) == 981) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (largestofthree(listOf(8, 6, 7, 1, 0)) == 8760) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-245/roger-bell-west/lua/ch-1.lua b/challenge-245/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..65b82c4f19 --- /dev/null +++ b/challenge-245/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,58 @@ +#! /usr/bin/lua + +-- by Michael Anderson at +-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua +-- modified by Roger +function recursive_compare(t1,t2) + -- Use usual comparison first. + if t1==t2 then return true end + -- We only support non-default behavior for tables + if (type(t1)~="table") then return false end + -- They better have the same metatables + local mt1 = getmetatable(t1) + local mt2 = getmetatable(t2) + if( not recursive_compare(mt1,mt2) ) then return false end + -- Build list of all keys + local kk = {} + for k1, _ in pairs(t1) do + kk[k1] = true + end + for k2, _ in pairs(t2) do + kk[k2] = true + end + -- Check each key that exists in at least one table + for _, k in ipairs(kk) do + if (not recursive_compare(t1[k], t2[k])) then + return false + end + end + return true +end + +function sortlanguage(langs, popularities) + local ix = {} + for n = 1, #langs do + table.insert(ix, n) + end + table.sort(ix, function (i, j) return popularities[i] < popularities[j] end) + local out = {} + for k, v in ipairs(ix) do + table.insert(out, langs[v]) + end + return out +end + +if recursive_compare(sortlanguage({"perl", "c", "python"}, {2, 1, 3}), {"c", "perl", "python"}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(sortlanguage({"c++", "haskell", "java"}, {1, 3, 2}), {"c++", "java", "haskell"}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-245/roger-bell-west/lua/ch-2.lua b/challenge-245/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..063ad63ad8 --- /dev/null +++ b/challenge-245/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,61 @@ +#! /usr/bin/lua + +function combinations(arr, k) + local c = {} + for i = 1, k do + table.insert(c, i) + end + table.insert(c, #arr + 1) + table.insert(c, 0) + local out = {} + while true do + local inner = {} + for i = k, 1, -1 do + table.insert(inner, arr[c[i]]) + end + table.insert(out, inner) + local j = 1 + while c[j] + 1 == c[j + 1] do + c[j] = j + j = j + 1 + end + if j > k then + break + end + c[j] = c[j] + 1 + end + return ipairs(out) +end + +function largestofthree(digits) + ordered = digits + table.sort(ordered, function (i,j) return i < j end) + local mx = 0 + for n = #ordered, 1, -1 do + for _a, c in combinations(ordered, n) do + local t = 0 + for _b, d in ipairs(c) do + t = t * 10 + d + end + if t > mx and t % 3 == 0 then + mx = t + end + end + end + return mx +end + +if largestofthree({8, 1, 9}) == 981 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if largestofthree({8, 6, 7, 1, 0}) == 8760 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-245/roger-bell-west/perl/ch-1.pl b/challenge-245/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..8fed5fa309 --- /dev/null +++ b/challenge-245/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,15 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is_deeply(sortlanguage(['perl', 'c', 'python'], [2, 1, 3]), ['c', 'perl', 'python'], 'example 1'); +is_deeply(sortlanguage(['c++', 'haskell', 'java'], [1, 3, 2]), ['c++', 'java', 'haskell'], 'example 2'); + +sub sortlanguage($langs, $popularities) { + my @ix = sort {$popularities->[$a] <=> $popularities->[$b]} (0 .. $#{$langs}); + return [map {$langs->[$_]} @ix]; +} diff --git a/challenge-245/roger-bell-west/perl/ch-2.pl b/challenge-245/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..04d6e280cc --- /dev/null +++ b/challenge-245/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,30 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(largestofthree([8, 1, 9]), 981, 'example 1'); +is(largestofthree([8, 6, 7, 1, 0]), 8760, 'example 2'); + +use Algorithm::Combinatorics qw(combinations); + +sub largestofthree($digits) { + my @ordered = sort {$b <=> $a} @{$digits}; + my $mx = 0; + foreach my $n (reverse(0 .. $#ordered)) { + foreach my $c (combinations(\@ordered, $n + 1)) { + my $t = 0; + foreach my $d (@{$c}) { + $t *= 10; + $t += $d; + } + if ($t > $mx && $t % 3 == 0) { + $mx = $t; + } + } + } + return $mx; +} diff --git a/challenge-245/roger-bell-west/postscript/ch-1.ps b/challenge-245/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..7ca0ca1362 --- /dev/null +++ b/challenge-245/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,204 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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.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 + +/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 + +/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 + +/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.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 + + +% end included library code + +/sortlanguage { + quicksort.with_keylist +} bind def + +(sortlanguage) test.start +[(perl) (c) (python)] [2 1 3] sortlanguage [(c) (perl) (python)] deepeq test +[(c++) (haskell) (java)] [1 3 2] sortlanguage [(c++) (java) (haskell)] deepeq test +test.end diff --git a/challenge-245/roger-bell-west/postscript/ch-2.ps b/challenge-245/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..870f26b893 --- /dev/null +++ b/challenge-245/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,182 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/quicksort { + { quicksort.cmp } quicksort.with_comparator +} 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 + +/combinations { + 4 dict begin + /k exch def + /arr exch def + /c [ + 0 1 k 1 sub { } for + arr length + 0 + ] def + [ + { + [ + k 1 sub -1 0 { + c exch get arr exch get + } for + ] + /j 0 def + { + c j get 1 add c j 1 add get ne { + exit + } if + c j j put + /j j 1 add def + } loop + j k ge { + exit + } if + c j c j get 1 add put + } 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.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 + +/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.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.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 + + +% end included library code + +/largestofthree { + 0 dict begin + quicksort /ordered exch def + /mx 0 def + ordered length 1 sub 1 neg 0 { + ordered exch 1 add combinations { + /t 0 def + { + t 10 mul add /t exch def + } forall + t mx gt t 3 mod 0 eq and { + /mx t def + } if + } forall + } for + mx + end +} bind def + +(largestofthree) test.start +[8 1 9] largestofthree 981 eq test +[8 6 7 1 0] largestofthree 8760 eq test +test.end diff --git a/challenge-245/roger-bell-west/python/ch-1.py b/challenge-245/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..f2714b0bc2 --- /dev/null +++ b/challenge-245/roger-bell-west/python/ch-1.py @@ -0,0 +1,18 @@ +#! /usr/bin/python3 + +def sortlanguage(langs, popularities): + ix = list(range(len(langs))) + ix.sort(key = lambda n: popularities[n]) + return [langs[n] for n in ix] + +import unittest + +class TestSortlanguage(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(sortlanguage(["perl", "c", "python"], [2, 1, 3]), ["c", "perl", "python"], 'example 1') + + def test_ex2(self): + self.assertEqual(sortlanguage(["c++", "haskell", "java"], [1, 3, 2]), ["c++", "java", "haskell"], 'example 2') + +unittest.main() diff --git a/challenge-245/roger-bell-west/python/ch-2.py b/challenge-245/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..6460369c52 --- /dev/null +++ b/challenge-245/roger-bell-west/python/ch-2.py @@ -0,0 +1,30 @@ +#! /usr/bin/python3 + +from itertools import combinations + +def largestofthree(digits): + ordered = digits.copy() + ordered.sort() + ordered.reverse() + mx = 0 + for n in range(0, len(ordered)): + for c in combinations(ordered, n + 1): + t = 0 + for d in c: + t *= 10 + t += d + if t > mx and t % 3 == 0: + mx = t + return mx + +import unittest + +class TestLargestofthree(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(largestofthree([8, 1, 9]), 981, 'example 1') + + def test_ex2(self): + self.assertEqual(largestofthree([8, 6, 7, 1, 0]), 8760, 'example 2') + +unittest.main() diff --git a/challenge-245/roger-bell-west/raku/ch-1.p6 b/challenge-245/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..6bb0e41b68 --- /dev/null +++ b/challenge-245/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,13 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is-deeply(sortlanguage(['perl', 'c', 'python'], [2, 1, 3]), ['c', 'perl', 'python'], 'example 1'); +is-deeply(sortlanguage(['c++', 'haskell', 'java'], [1, 3, 2]), ['c++', 'java', 'haskell'], 'example 2'); + +sub sortlanguage(@langs, @popularities) { + my @ix = [0 .. @langs.end].sort({@popularities[$^a] <=> @popularities[$^b]}); + return Array(@ix.map({@langs[$_]})); +} diff --git a/challenge-245/roger-bell-west/raku/ch-2.p6 b/challenge-245/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..d5f187c927 --- /dev/null +++ b/challenge-245/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,26 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(largestofthree([8, 1, 9]), 981, 'example 1'); +is(largestofthree([8, 6, 7, 1, 0]), 8760, 'example 2'); + +sub largestofthree(@digits) { + my @ordered = @digits.sort({$^b <=> $^a}); + my $mx = 0; + for (0 .. @ordered.end).reverse -> $n { + for combinations(@ordered, $n + 1) -> @c { + my $t = 0; + for @c -> $d { + $t *= 10; + $t += $d; + } + if ($t > $mx && $t % 3 == 0) { + $mx = $t; + } + } + } + return $mx; +} diff --git a/challenge-245/roger-bell-west/ruby/ch-1.rb b/challenge-245/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..4ead71351e --- /dev/null +++ b/challenge-245/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,21 @@ +#! /usr/bin/ruby + +def sortlanguage(langs, popularities) + ix = (0...langs.length).to_a + ix = ix.sort_by {|n| popularities[n]} + return ix.collect {|n| langs[n]} +end + +require 'test/unit' + +class TestSortlanguage < Test::Unit::TestCase + + def test_ex1 + assert_equal(['c', 'perl', 'python'], sortlanguage(['perl', 'c', 'python'], [2, 1, 3])) + end + + def test_ex2 + assert_equal(['c++', 'java', 'haskell'], sortlanguage(['c++', 'haskell', 'java'], [1, 3, 2])) + end + +end diff --git a/challenge-245/roger-bell-west/ruby/ch-2.rb b/challenge-245/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..b8ee053ea3 --- /dev/null +++ b/challenge-245/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,33 @@ +#! /usr/bin/ruby + +def largestofthree(digits) + ordered = digits.sort {|a,b| b <=> a} + mx = 0 + (ordered.length-1).downto(0) do |n| + ordered.combination(n + 1) do |c| + t = 0 + c.each do |d| + t *= 10 + t += d + end + if t > mx && t % 3 == 0 then + mx = t + end + end + end + return mx +end + +require 'test/unit' + +class TestLargestofthree < Test::Unit::TestCase + + def test_ex1 + assert_equal(981, largestofthree([8, 1, 9])) + end + + def test_ex2 + assert_equal(8760, largestofthree([8, 6, 7, 1, 0])) + end + +end diff --git a/challenge-245/roger-bell-west/rust/ch-1.rs b/challenge-245/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..ef1fb3b415 --- /dev/null +++ b/challenge-245/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,24 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +fn sortlanguage(langs: Vec<&str>, popularities: Vec<u32>) -> Vec<String> { + let mut ix = (0..langs.len()).collect::<Vec<usize>>(); + ix.sort_unstable_by_key(|n| popularities[*n]); + ix.iter().map(|n| langs[*n].to_string()).collect::<Vec<String>>() +} + +#[test] +fn test_ex1() { + assert_eq!( + sortlanguage(vec!["perl", "c", "python"], vec![2, 1, 3]), + vec!["c", "perl", "python"] + ); +} + +#[test] +fn test_ex2() { + assert_eq!( + sortlanguage(vec!["c++", "haskell", "java"], vec![1, 3, 2]), + vec!["c++", "java", "haskell"] + ); +} diff --git a/challenge-245/roger-bell-west/rust/ch-2.rs b/challenge-245/roger-bell-west/rust/ch-2.rs new file |
