diff options
21 files changed, 966 insertions, 0 deletions
diff --git a/challenge-253/roger-bell-west/javascript/ch-1.js b/challenge-253/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..eec930e572 --- /dev/null +++ b/challenge-253/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,52 @@ +#! /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 splitstrings(a, sep) { + let p = []; + for (let s of a) { + let r = s.split(sep).filter(n => n.length > 0); + p = p.concat(r) + } + return p; +} + +if (deepEqual(splitstrings(['one.two.three', 'four.five', 'six'], '.'), ['one', 'two', 'three', 'four', 'five', 'six'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(splitstrings(['$perl$$', '$$raku$'], '$'), ['perl', 'raku'])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-253/roger-bell-west/javascript/ch-2.js b/challenge-253/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..dc526d0b86 --- /dev/null +++ b/challenge-253/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,52 @@ +#! /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 weakestrows(a) { + let p = Array(a.length).fill().map((element, index) => index); + let b = a.map(n => n.reduce((x,y) => x+y)); + p.sort(function(l, r) { + return b[l] - b[r]; + }); + return p; +} + +if (deepEqual(weakestrows([[1, 1, 0, 0, 0], [1, 1, 1, 1, 0], [1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 1, 1, 1, 1]]), [2, 0, 3, 1, 4])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(weakestrows([[1, 0, 0, 0], [1, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 0]]), [0, 2, 3, 1])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-253/roger-bell-west/kotlin/ch-1.kt b/challenge-253/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..3918a407bb --- /dev/null +++ b/challenge-253/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,24 @@ +fun splitstrings(a: List<String>, sep: String): List<String> { + var p = ArrayList<String>() + for (s in a) { + p.addAll(s.split(sep).filter{it.length > 0}) + } + return p.toList() +} + +fun main() { + + if (splitstrings(listOf("one.two.three", "four.five", "six"), ".") == listOf("one", "two", "three", "four", "five", "six")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (splitstrings(listOf("\$perl\$\$", "\$\$raku\$"), "\$") == listOf("perl", "raku")) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-253/roger-bell-west/kotlin/ch-2.kt b/challenge-253/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..4a0b428e71 --- /dev/null +++ b/challenge-253/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,23 @@ +fun weakestrows(a: List<List<Int>>): List<Int> { + var p = ArrayList(generateSequence(0) { it + 1 }.take(a.size).toList()) + val b = a.map{it.sum()} + p.sortBy {b[it]} + return p.toList() +} + +fun main() { + + if (weakestrows(listOf(listOf(1, 1, 0, 0, 0), listOf(1, 1, 1, 1, 0), listOf(1, 0, 0, 0, 0), listOf(1, 1, 0, 0, 0), listOf(1, 1, 1, 1, 1))) == listOf(2, 0, 3, 1, 4)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (weakestrows(listOf(listOf(1, 0, 0, 0), listOf(1, 1, 1, 1), listOf(1, 0, 0, 0), listOf(1, 0, 0, 0))) == listOf(0, 2, 3, 1)) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-253/roger-bell-west/lua/ch-1.lua b/challenge-253/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..bef4c6baf3 --- /dev/null +++ b/challenge-253/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,69 @@ +#! /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 + +-- bart at https://stackoverflow.com/questions/1426954/split-string-in-lua +function split(inputstr, sep) + sep=sep or '%s' + local t={} + for field,s in string.gmatch(inputstr, "([^"..sep.."]*)("..sep.."?)") do + table.insert(t,field) + if s=="" then + return t + end + end +end + +function splitstrings(a, sep) + local p = {} + for _i, s in ipairs(a) do + for _j, n in ipairs(split(s), sep) do + if #n > 0 then + table.insert(p, n) + end + end + end + return p +end + +if recursive_compare(splitstrings({"one.two.three", "four.five", "six"}, "."), {"one", "two", "three", "four", "five", "six"}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(splitstrings({"$perl$$", "$$raku$"}, "$"), {"perl", "raku"}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-253/roger-bell-west/lua/ch-2.lua b/challenge-253/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..bc79e3a4b0 --- /dev/null +++ b/challenge-253/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,60 @@ +#! /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 weakestrows(a) + local p = {} + local b = {} + for i, n in ipairs(a) do + p[i] = i - 1 + local sum = 0 + for _j, m in ipairs(n) do + sum = sum + m + end + table.insert(b, sum) + end + table.sort(p, function(i, j) return b[i + 1] < b[j + 1] end) + return p +end + +if recursive_compare(weakestrows({{1, 1, 0, 0, 0}, {1, 1, 1, 1, 0}, {1, 0, 0, 0, 0}, {1, 1, 0, 0, 0}, {1, 1, 1, 1, 1}}), {2, 0, 3, 1, 4}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(weakestrows({{1, 0, 0, 0}, {1, 1, 1, 1}, {1, 0, 0, 0}, {1, 0, 0, 0}}), {0, 2, 3, 1}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-253/roger-bell-west/perl/ch-1.pl b/challenge-253/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..714727e35a --- /dev/null +++ b/challenge-253/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,19 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is_deeply(splitstrings(['one.two.three', 'four.five', 'six'], '.'), ['one', 'two', 'three', 'four', 'five', 'six'], 'example 1'); +is_deeply(splitstrings(['$perl$$', '$$raku$'], '$'), ['perl', 'raku'], 'example 2'); + +sub splitstrings($a, $ssep) { + my @p; + (my $sep = $ssep) =~ s/([^A-Z0-9])/\\$1/gi; + foreach my $s (@{$a}) { + push @p, grep /./, split($sep, $s); + } + return \@p; +} diff --git a/challenge-253/roger-bell-west/perl/ch-2.pl b/challenge-253/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..f853ab3517 --- /dev/null +++ b/challenge-253/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,19 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is_deeply(weakestrows([[1, 1, 0, 0, 0], [1, 1, 1, 1, 0], [1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 1, 1, 1, 1]]), [2, 0, 3, 1, 4], 'example 1'); +is_deeply(weakestrows([[1, 0, 0, 0], [1, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 0]]), [0, 2, 3, 1], 'example 2'); + +use List::Util qw(sum); + +sub weakestrows($aa) { + my @p = (0 .. $#{$aa}); + my @bb = map {sum(@{$_})} @{$aa}; + @p = sort {$bb[$a] <=> $bb[$b]} @p; + return \@p; +} diff --git a/challenge-253/roger-bell-west/postscript/ch-1.ps b/challenge-253/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..067989d10d --- /dev/null +++ b/challenge-253/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,138 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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.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 + +/strsplit % (ajbjc) (j) -> [ (a) (b) (c) ] +{ + 1 dict begin + /sep exch def + [ exch + { + dup length 0 eq { + pop + exit + } { + sep search { + exch pop + dup length 0 eq { + pop + } { + exch + } ifelse + } { + () + } ifelse + } ifelse + } loop + ] + 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 + +/splitstrings { + 0 dict begin + /sep exch def + [ exch + { + sep strsplit + aload pop + } forall + ] + end +} bind def + +(splitstrings) test.start +[(one.two.three) (four.five) (six)] (.) splitstrings [(one) (two) (three) (four) (five) (six)] deepeq test +[($perl$$) ($$raku$)] ($) splitstrings [(perl) (raku)] deepeq test +test.end diff --git a/challenge-253/roger-bell-west/postscript/ch-2.ps b/challenge-253/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..c013e8511a --- /dev/null +++ b/challenge-253/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,193 @@ +%!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 + +/map { % array proc -> array + 2 dict begin + /p exch def + [ exch + { + p + } forall + ] + end +} 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.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} bind def + +/mergesort.merge { + 4 dict begin + /right exch def + /left exch def + /li 0 def + /ri 0 def + [ + { + li left length ge ri right length ge or { + exit + } if + left li get right ri get cmp 0 le { + left li get + /li li 1 add def + } { + right ri get + /ri ri 1 add def + } ifelse + } loop + li left length lt { + left li left length li sub getinterval aload pop + } if + ri right length lt { + right ri right length ri sub getinterval aload pop + } if + ] + 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 + +/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 + +/mergesort.with_comparator { % [ a c b ] { comparator } -> [ a b c ] + 5 dict begin + /cmp where { + pop + } { + /cmp exch def + } ifelse + /m exch def + m length 1 le { + m + } { + /l2 m length 2 idiv def + /left m 0 l2 getinterval mergesort.with_comparator def + /right m l2 m length l2 sub getinterval mergesort.with_comparator def + left right mergesort.merge + } 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 + +/weakestrows { + 0 dict begin + /a exch def + /p [ 0 1 a length 1 sub {} for ] def + /b a { { add } reduce } map def + p { b exch get exch b exch get exch quicksort.cmp } mergesort.with_comparator + end +} bind def + +(weakestrows) test.start +[[1 1 0 0 0] [1 1 1 1 0] [1 0 0 0 0] [1 1 0 0 0] [1 1 1 1 1]] weakestrows [2 0 3 1 4] deepeq test +[[1 0 0 0] [1 1 1 1] [1 0 0 0] [1 0 0 0]] weakestrows [0 2 3 1] deepeq test +test.end diff --git a/challenge-253/roger-bell-west/python/ch-1.py b/challenge-253/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..af4f110b05 --- /dev/null +++ b/challenge-253/roger-bell-west/python/ch-1.py @@ -0,0 +1,21 @@ +#! /usr/bin/python3 + +def splitstrings(a, sep): + p = [] + for s in a: + for n in s.split(sep): + if len(n) > 0: + p.append(n) + return p + +import unittest + +class TestSplitstrings(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(splitstrings(["one.two.three", "four.five", "six"], "."), ["one", "two", "three", "four", "five", "six"], 'example 1') + + def test_ex2(self): + self.assertEqual(splitstrings(["$perl$$", "$$raku$"], "$"), ["perl", "raku"], 'example 2') + +unittest.main() diff --git a/challenge-253/roger-bell-west/python/ch-2.py b/challenge-253/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..80fa491683 --- /dev/null +++ b/challenge-253/roger-bell-west/python/ch-2.py @@ -0,0 +1,19 @@ +#! /usr/bin/python3 + +def weakestrows(a): + p = list(range(len(a))) + b = [sum(n) for n in a] + p.sort(key = lambda i: b[i]) + return p + +import unittest + +class TestWeakestrows(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(weakestrows([[1, 1, 0, 0, 0], [1, 1, 1, 1, 0], [1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 1, 1, 1, 1]]), [2, 0, 3, 1, 4], 'example 1') + + def test_ex2(self): + self.assertEqual(weakestrows([[1, 0, 0, 0], [1, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 0]]), [0, 2, 3, 1], 'example 2') + +unittest.main() diff --git a/challenge-253/roger-bell-west/raku/ch-1.p6 b/challenge-253/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..72d235811e --- /dev/null +++ b/challenge-253/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,18 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is-deeply(splitstrings(['one.two.three', 'four.five', 'six'], '.'), ['one', 'two', 'three', 'four', 'five', 'six'], 'example 1'); +is-deeply(splitstrings(['$perl$$', '$$raku$'], '$'), ['perl', 'raku'], 'example 2'); + +sub splitstrings(@a, $ssep) { + my @p; + my $sep = $ssep; + $sep ~~ s:g/(<[^A..Z0..9]>)/\\$1/; + for @a -> $s { + @p.append($s.split($sep).grep(/./)); + } + return @p; +} diff --git a/challenge-253/roger-bell-west/raku/ch-2.p6 b/challenge-253/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..0d3989e624 --- /dev/null +++ b/challenge-253/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,15 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is-deeply(weakestrows([[1, 1, 0, 0, 0], [1, 1, 1, 1, 0], [1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 1, 1, 1, 1]]), [2, 0, 3, 1, 4], 'example 1'); +is-deeply(weakestrows([[1, 0, 0, 0], [1, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 0]]), [0, 2, 3, 1], 'example 2'); + +sub weakestrows(@aa) { + my @p = (0 .. @aa.end); + my @bb = @aa.map({$_.sum}); + @p = @p.sort({@bb[$^a] <=> @bb[$^b]}); + return @p; +} diff --git a/challenge-253/roger-bell-west/ruby/ch-1.rb b/challenge-253/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..6f77b10013 --- /dev/null +++ b/challenge-253/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,23 @@ +#! /usr/bin/ruby + +def splitstrings(a, sep) + p = [] + a.each do |s| + p += s.split(sep).find_all {|n| n.length > 0} + end + return p +end + +require 'test/unit' + +class TestSplitstrings < Test::Unit::TestCase + + def test_ex1 + assert_equal(['one', 'two', 'three', 'four', 'five', 'six'], splitstrings(['one.two.three', 'four.five', 'six'], '.')) + end + + def test_ex2 + assert_equal(['perl', 'raku'], splitstrings(['$perl$$', '$$raku$'], '$')) + end + +end diff --git a/challenge-253/roger-bell-west/ruby/ch-2.rb b/challenge-253/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..93fb225bda --- /dev/null +++ b/challenge-253/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,22 @@ +#! /usr/bin/ruby + +def weakestrows(a) + p = (0...a.length()) + b = a.map { |n| n.sum } + p = p.sort_by { |x| b[x] } + return p +end + +require 'test/unit' + +class TestWeakestrows < Test::Unit::TestCase + + def test_ex1 + assert_equal([2, 0, 3, 1, 4], weakestrows([[1, 1, 0, 0, 0], [1, 1, 1, 1, 0], [1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 1, 1, 1, 1]])) + end + + def test_ex2 + assert_equal([0, 2, 3, 1], weakestrows([[1, 0, 0, 0], [1, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 0]])) + end + +end diff --git a/challenge-253/roger-bell-west/rust/ch-1.rs b/challenge-253/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..eb0e724a7e --- /dev/null +++ b/challenge-253/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,31 @@ +#! /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!( + splitstrings(vec!["one.two.three", "four.five", "six"], "."), + vec!["one", "two", "three", "four", "five", "six"] + ); +} + +#[test] +fn test_ex2() { + assert_eq!( + splitstrings(vec!["$perl$$", "$$raku$"], "$"), + vec!["perl", "raku"] + ); +} + +fn splitstrings(a: Vec<&str>, sep: &str) -> Vec<String> { + let mut p = Vec::new(); + for s in a { + let mut r = s + .split(sep) + .filter(|n| n.len() > 0) + .map(|n| n.to_string()) + .collect::<Vec<_>>(); + p.append(&mut r); + } + p +} diff --git a/challenge-253/roger-bell-west/rust/ch-2.rs b/challenge-253/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..8a3b806db6 --- /dev/null +++ b/challenge-253/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,36 @@ +#! /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!( + weakestrows(vec![ + vec![1, 1, 0, 0, 0], + vec![1, 1, 1, 1, 0], + vec![1, 0, 0, 0, 0], + vec![1, 1, 0, 0, 0], + vec![1, 1, 1, 1, 1] + ]), + vec![2, 0, 3, 1, 4] + ); +} + +#[test] +fn test_ex2() { + assert_eq!( + weakestrows(vec![ + vec![1, 0, 0, 0], + vec![1, 1, 1, 1], + vec![1, 0, 0, 0], + vec![1, 0, 0, 0] + ]), + vec![0, 2, 3, 1] + ); +} + +fn weakestrows(a: Vec<Vec<u32>>) -> Vec<usize> { + let mut p = (0..a.len()).collect::<Vec<usize>>(); + let b: Vec<u32> = a.iter().map(|n| n.iter().sum()).collect(); + p.sort_by(|l, r| b[*l].cmp(&b[*r])); + p +} diff --git a/challenge-253/roger-bell-west/scala/ch-1.scala b/challenge-253/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..9bc91f9eda --- /dev/null +++ b/challenge-253/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,26 @@ +import scala.collection.mutable.ListBuffer + +object Splitstrings { + def splitstrings(a: List[String], sep: String): List[String] = { + var p = new ListBuffer[String] + for (s <- a) { + p.append(s.split("[" + sep + "]").filter(n => n.length > 0): _*) + } + return p.toList + } + def main(args: Array[String]) { + if (splitstrings(List("one.two.three", "four.five", "six"), ".") == List("one", "two", "three", "four", "five", "six")) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (splitstrings(List("$perl$$", "$$raku$"), "$") == List("perl", "raku")) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-253/roger-bell-west/scala/ch-2.scala b/challenge-253/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..9c5034fd89 --- /dev/null +++ b/challenge-253/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,25 @@ +import scala.collection.mutable.ListBuffer + +object Weakestrows { + def weakestrows(a: List[List[Int]]): List[Int] = { + var p = List.range(0, a.size).to[ListBuffer] + val b = a.map(n => n.sum) + p = p.sortWith(b(_) < b(_)) + return p.toList + } + def main(args: Array[String]) { + if (weakestrows(List(List(1, 1, 0, 0, 0), List(1, 1, 1, 1, 0), List(1, 0, 0, 0, 0), List(1, 1, 0, 0, 0), List(1, 1, 1, 1, 1))) == List(2, 0, 3, 1, 4)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (weakestrows(List(List(1, 0, 0, 0), List(1, 1, 1, 1), List(1, 0, 0, 0), List(1, 0, 0, 0))) == List(0, 2, 3, 1)) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} |
