diff options
19 files changed, 1002 insertions, 0 deletions
diff --git a/challenge-212/roger-bell-west/javascript/ch-1.js b/challenge-212/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..c5fccd6864 --- /dev/null +++ b/challenge-212/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,29 @@ +#! /usr/bin/node + +"use strict" + +function jumpingletters(word, jump) { + var s = "" + for (let i = 0; i < jump.length; i++) { + let c = word.charCodeAt(i); + let d = c + jump[i] % 26; + if ((c <= 90 && d > 90) || d > 122) { + d -= 26; + } + s += String.fromCharCode(d); + } + return s; +} + +if (jumpingletters('Perl', [2, 22, 19, 9]) == 'Raku') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (jumpingletters('Raku', [24, 4, 7, 17]) == 'Perl') { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-212/roger-bell-west/javascript/ch-2.js b/challenge-212/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..e0733c2def --- /dev/null +++ b/challenge-212/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,96 @@ +#! /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 range(from, to) { + let a = new Array(to - from + 1); + for (let i = from; i <= to; i++) { + a[i - from] = i; + } + return a; +} + +function rearrangegroups(list, size) { + let h = new Map(); + for (let k of list) { + if (h.has(k)) { + h.set(k, h.get(k) + 1); + } else { + h.set(k, 1); + } + } + let out = []; + while (true) { + let m = Math.min(...h.keys()); + let res = range(m, m + size - 1); + for (let n of res) { + if (h.has(n)) { + let p = h.get(n) - 1; + if (p == 0) { + h.delete(n); + } else { + h.set(n, p); + } + } else { + return []; + } + } + out.push(res); + if (h.size == 0) { + break; + } + } + return out; +} + +if (deepEqual(rearrangegroups([1, 2, 3, 5, 1, 2, 7, 6, 3], 3), [[1, 2, 3], [1, 2, 3], [5, 6, 7]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(rearrangegroups([1, 2, 3], 2), [])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(rearrangegroups([1, 2, 4, 3, 5, 3], 3), [[1, 2, 3], [3, 4, 5]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(rearrangegroups([1, 5, 2, 6, 4, 7], 3), [])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-212/roger-bell-west/kotlin/ch-1.kt b/challenge-212/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..7760a4ce7f --- /dev/null +++ b/challenge-212/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,28 @@ +fun jumpingletters(word: String, jump: List<Int>): String { + var s = ArrayList<Char>() + for ((i, c) in word.asSequence().withIndex()) { + var d = c + jump[i] % 26 + if ((c <= 'Z' && d > 'Z') || d > 'z') { + d -= 26; + } + s.add(d) + } + return s.joinToString(separator = "") +} + +fun main() { + + if (jumpingletters("Perl", listOf(2, 22, 19, 9)) == "Raku") { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (jumpingletters("Raku", listOf(24, 4, 7, 17)) == "Perl") { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-212/roger-bell-west/kotlin/ch-2.kt b/challenge-212/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..7532d3ca98 --- /dev/null +++ b/challenge-212/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,61 @@ +fun rearrangegroups(list: List<Int>, size: Int): List<List<Int>> { + var h = mutableMapOf<Int, Int>() + for (k in list) { + if (h.contains(k)) { + h[k] = h[k]!! + 1 + } else { + h[k] = 1 + } + } + var out = ArrayList<List<Int>>() + while (true) { + val m = h.keys.minOrNull()!! + val res = (m .. m + size - 1).toList() + for (n in res) { + if (h.contains(n)) { + val p = h[n]!! - 1; + if (p == 0) { + h.remove(n) + } else { + h[n] = p + } + } else { + return emptyList<List<Int>>() + } + } + out.add(res) + if (h.size == 0) { + break + } + } + return out.toList() +} + +fun main() { + + if (rearrangegroups(listOf(1, 2, 3, 5, 1, 2, 7, 6, 3), 3) == listOf(listOf(1, 2, 3), listOf(1, 2, 3), listOf(5, 6, 7))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (rearrangegroups(listOf(1, 2, 3), 2) == emptyList<List<Int>>()) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (rearrangegroups(listOf(1, 2, 4, 3, 5, 3), 3) == listOf(listOf(1, 2, 3), listOf(3, 4, 5))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (rearrangegroups(listOf(1, 5, 2, 6, 4, 7), 3) == emptyList<List<Int>>()) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-212/roger-bell-west/lua/ch-1.lua b/challenge-212/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..6dd4185582 --- /dev/null +++ b/challenge-212/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,29 @@ +#! /usr/bin/lua + +function jumpingletters(word, jump) + local s = {} + for i, j in ipairs(jump) do + local c = string.byte(word, i) + local d = c + j % 26 + if (c <= 90 and d > 90) or d > 122 then + d = d - 26 + end + table.insert(s, d) + end + return string.char(table.unpack(s)) +end + +if jumpingletters("Perl", {2, 22, 19, 9}) == "Raku" then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if jumpingletters("Raku", {24, 4, 7, 17}) == "Perl" then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-212/roger-bell-west/lua/ch-2.lua b/challenge-212/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..efe0ccd7b1 --- /dev/null +++ b/challenge-212/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,101 @@ +#! /usr/bin/lua + +-- by Michael Anderson at +-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua +function recursive_compare(t1,t2) + if t1==t2 then return true end + if (type(t1)~="table") then return false end + local mt1 = getmetatable(t1) + local mt2 = getmetatable(t2) + if( not recursive_compare(mt1,mt2) ) then return false end + for k1,v1 in pairs(t1) do + local v2 = t2[k1] + if( not recursive_compare(v1,v2) ) then return false end + end + for k2,v2 in pairs(t2) do + local v1 = t1[k2] + if( not recursive_compare(v1,v2) ) then return false end + end + return true +end + +function propersize(t) + local l=0 + for k,v in pairs(t) do + l = l + 1 + end + return l +end + +function rearrangegroups(list, size) + local h = {} + for i, k in ipairs(list) do + if h[k] == nil then + t = 1 + else + t = h[k] + 1 + end + h[k] = t + end + local out = {} + while true do + local m = nil + for k, v in pairs(h) do + if m == nil then + m = k + else + m = math.min(m, k) + end + end + local res = {} + for i = m, m + size - 1 do + table.insert(res, i) + end + for i, n in ipairs(res) do + if h[n] == nil then + return {} + else + local p = h[n] - 1 + if p == 0 then + h[n] = nil + else + h[n] = p + end + end + end + table.insert(out, res) + if propersize(h) == 0 then + break + end + end + return out +end + +if recursive_compare(rearrangegroups({1, 2, 3, 5, 1, 2, 7, 6, 3}, 3), {{1, 2, 3}, {1, 2, 3}, {5, 6, 7}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(rearrangegroups({1, 2, 3}, 2), {}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(rearrangegroups({1, 2, 4, 3, 5, 3}, 3), {{1, 2, 3}, {3, 4, 5}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(rearrangegroups({1, 5, 2, 6, 4, 7}, 3), {}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-212/roger-bell-west/perl/ch-1.pl b/challenge-212/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..a6da01df70 --- /dev/null +++ b/challenge-212/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,24 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(jumpingletters('Perl', [2, 22, 19, 9]), 'Raku', 'example 1'); +is(jumpingletters('Raku', [24, 4, 7, 17]), 'Perl', 'example 2'); + +sub jumpingletters($word, $jump) { + my @s = ''; + my $i = 0; + foreach my $c (split '', $word) { + my $d = ord($c) + $jump->[$i] % 26; + if (($c le 'Z' && $d > 90) || $d > 122) { + $d -= 26; + } + push @s,chr($d); + $i++; + } + return join('', @s); +} diff --git a/challenge-212/roger-bell-west/perl/ch-2.pl b/challenge-212/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..7e95ec5208 --- /dev/null +++ b/challenge-212/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,39 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 4; + +is_deeply(rearrangegroups([1, 2, 3, 5, 1, 2, 7, 6, 3], 3), [[1, 2, 3], [1, 2, 3], [5, 6, 7]], 'example 1'); +is_deeply(rearrangegroups([1, 2, 3], 2), [], 'example 2'); +is_deeply(rearrangegroups([1, 2, 4, 3, 5, 3], 3), [[1, 2, 3], [3, 4, 5]], 'example 3'); +is_deeply(rearrangegroups([1, 5, 2, 6, 4, 7], 3), [], 'example 4'); + +use List::Util qw(min); + +sub rearrangegroups($list, $size) { + my %h; + map {$h{$_}++} @{$list}; + my @out; + while (1) { + my $m = min(keys %h); + my @res = ($m .. $m + $size - 1); + foreach my $n (@res) { + if (exists $h{$n}) { + $h{$n}--; + if ($h{$n} == 0) { + delete $h{$n}; + } + } else { + return []; + } + } + push @out,\@res; + unless (%h) { + last; + } + } + return \@out; +} diff --git a/challenge-212/roger-bell-west/postscript/ch-1.ps b/challenge-212/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..ae293904b2 --- /dev/null +++ b/challenge-212/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,59 @@ +%!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 { + /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 + + +% end included library code + +/jumpingletters { + 5 dict begin + /jump exch def + /word exch def + /s word length string def + 0 1 word length 1 sub { + /i exch def + /d word i get jump i get 26 mod add def + word i get 90 le d 90 gt and d 122 gt or { + /d d 26 sub def + } if + s i d put + } for + s + end +} bind def + +(jumpingletters) test.start +(Perl) [2 22 19 9] jumpingletters (Raku) eq test +(Raku) [24 4 7 17] jumpingletters (Perl) eq test +test.end diff --git a/challenge-212/roger-bell-west/postscript/ch-2.ps b/challenge-212/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..4eb1876cfc --- /dev/null +++ b/challenge-212/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,170 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} 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 + +/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 + +/listmin { + { min } reduce +} 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.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + + +% end included library code + +/rearrangegroups { + 9 dict begin + /size exch def + /list exch def + /h 0 dict def + list { + /k exch def + h k known { + h k h k get 1 add put + } { + h k 1 put + } ifelse + } forall + /fail false def + [ + { + /m h keys listmin def + /res [ m 1 m size add 1 sub { } for ] def + res { + /n exch def + h n known { + /p h n get 1 sub def + p 0 eq { + h n undef + } { + h n p put + } ifelse + } { + ] pop [ + /fail true def + exit + } ifelse + } forall + fail { + exit + } if + res aload length array astore + h length 0 eq { + exit + } if + } loop + ] + end +} bind def + +(rearrangegroups) test.start +[1 2 3 5 1 2 7 6 3] 3 rearrangegroups [[1 2 3] [1 2 3] [5 6 7]] deepeq test +[1 2 3] 2 rearrangegroups [] deepeq test +[1 5 2 6 4 7] 3 rearrangegroups [] deepeq test +test.end diff --git a/challenge-212/roger-bell-west/python/ch-1.py b/challenge-212/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..9587e1ef67 --- /dev/null +++ b/challenge-212/roger-bell-west/python/ch-1.py @@ -0,0 +1,23 @@ +#! /usr/bin/python3 + +import unittest + +def jumpingletters(word, jump): + s = "" + for i, c0 in enumerate(word): + c = ord(c0) + d = c + jump[i] % 26 + if (c <= 90 and d > 90) or d > 122: + d -= 26 + s += chr(d) + return s + +class TestJumpingletters(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(jumpingletters("Perl", [2, 22, 19, 9]), "Raku", 'example 1') + + def test_ex2(self): + self.assertEqual(jumpingletters("Raku", [24, 4, 7, 17]), "Perl", 'example 2') + +unittest.main() diff --git a/challenge-212/roger-bell-west/python/ch-2.py b/challenge-212/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..4fd3853f27 --- /dev/null +++ b/challenge-212/roger-bell-west/python/ch-2.py @@ -0,0 +1,46 @@ +#! /usr/bin/python3 + +import unittest + +from collections import defaultdict + +def rearrangegroups(lst, size): + h = dict() + for k in lst: + if k in h: + h[k] += 1 + else: + h[k] = 1 + out = [] + while True: + m = min(h.keys()) + res = list(range(m, m + size)) + for n in res: + if n in h: + p = h[n] - 1 + if p == 0: + del h[n] + else: + h[n] = p + else: + return [] + out.append(res) + if len(h) == 0: + break + return out + +class TestRearrangegroups(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(rearrangegroups([1, 2, 3, 5, 1, 2, 7, 6, 3], 3), [[1, 2, 3], [1, 2, 3], [5, 6, 7]], 'example 1') + + def test_ex2(self): + self.assertEqual(rearrangegroups([1, 2, 3], 2), [], 'example 2') + + def test_ex3(self): + self.assertEqual(rearrangegroups([1, 2, 4, 3, 5, 3], 3), [[1, 2, 3], [3, 4, 5]], 'example 3') + + def test_ex4(self): + self.assertEqual(rearrangegroups([1, 5, 2, 6, 4, 7], 3), [], 'example 4') + +unittest.main() diff --git a/challenge-212/roger-bell-west/raku/ch-1.p6 b/challenge-212/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..0fcaa83391 --- /dev/null +++ b/challenge-212/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,22 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(jumpingletters('Perl', [2, 22, 19, 9]), 'Raku', 'example 1'); +is(jumpingletters('Raku', [24, 4, 7, 17]), 'Perl', 'example 2'); + +sub jumpingletters($word, @jump) { + my @s = ''; + my $i = 0; + for $word.comb -> $c { + my $d = ord($c) + @jump[$i] % 26; + if (($c le 'Z' && $d > 90) || $d > 122) { + $d -= 26; + } + @s.push(chr($d)); + $i++; + } + return join('', @s); +} diff --git a/challenge-212/roger-bell-west/raku/ch-2.p6 b/challenge-212/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..12acf263e4 --- /dev/null +++ b/challenge-212/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,35 @@ +#! /usr/bin/raku + +use Test; + +plan 4; + +is-deeply(rearrangegroups([1, 2, 3, 5, 1, 2, 7, 6, 3], 3), [[1, 2, 3], [1, 2, 3], [5, 6, 7]], 'example 1'); +is-deeply(rearrangegroups([1, 2, 3], 2), [], 'example 2'); +is-deeply(rearrangegroups([1, 2, 4, 3, 5, 3], 3), [[1, 2, 3], [3, 4, 5]], 'example 3'); +is-deeply(rearrangegroups([1, 5, 2, 6, 4, 7], 3), [], 'example 4'); + +sub rearrangegroups(@list, $size) { + my %h; + map {%h{$_}++},@list; + my @out; + while (True) { + my $m = %h.keys.min; + my @res = ($m + 0 .. $m + $size - 1); + for @res -> $n { + if (%h{$n}:exists) { + %h{$n}--; + if (%h{$n} == 0) { + %h{$n}:delete; + } + } else { + return []; + } + } + @out.push(@res); + unless (%h) { + last; + } + } + return @out; +} diff --git a/challenge-212/roger-bell-west/ruby/ch-1.rb b/challenge-212/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..8755d04bb8 --- /dev/null +++ b/challenge-212/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,27 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def jumpingletters(word, jump) + s = "" + word.split(//).each_with_index do |c, i| + d = c.ord + jump[i] % 26 + if (c <= "Z" && d > 90) || d > 122 then + d -= 26 + end + s += d.chr() + end + return s +end + +class TestJumpingletters < Test::Unit::TestCase + + def test_ex1 + assert_equal('Raku', jumpingletters('Perl', [2, 22, 19, 9])) + end + + def test_ex2 + assert_equal('Perl', jumpingletters('Raku', [24, 4, 7, 17])) + end + +end diff --git a/challenge-212/roger-bell-west/ruby/ch-2.rb b/challenge-212/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..4978379af4 --- /dev/null +++ b/challenge-212/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,56 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def rearrangegroups(list, size) + h = Hash.new + list.each do |k| + if h.has_key?(k) then + h[k] += 1 + else + h[k] = 1 + end + end + out = [] + while true do + m = h.keys.min + res = (m .. m + size - 1).to_a + res.each do |n| + if h.has_key?(n) then + p = h[n] - 1 + if p == 0 then + h.delete(n) + else + h[n] = p + end + else + return [] + end + end + out.push(res) + if h.length == 0 then + break + end + end + return out +end + +class TestRearrangegroups < Test::Unit::TestCase + + def test_ex1 + assert_equal([[1, 2, 3], [1, 2, 3], [5, 6, 7]], rearrangegroups([1, 2, 3, 5, 1, 2, 7, 6, 3], 3)) + end + + def test_ex2 + assert_equal([], rearrangegroups([1, 2, 3], 2)) + end + + def test_ex3 + assert_equal([[1, 2, 3], [3, 4, 5]], rearrangegroups([1, 2, 4, 3, 5, 3], 3)) + end + + def test_ex4 + assert_equal([], rearrangegroups([1, 5, 2, 6, 4, 7], 3)) + end + +end diff --git a/challenge-212/roger-bell-west/rust/ch-1.rs b/challenge-212/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..f053fc6aa5 --- /dev/null +++ b/challenge-212/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 + +#[test] +fn test_ex1() { + assert_eq!(jumpingletters("Perl", vec![2, 22, 19, 9]), "Raku"); +} + +#[test] +fn test_ex2() { + assert_eq!(jumpingletters("Raku", vec![24, 4, 7, 17]), "Perl"); +} + +fn jumpingletters(word: &str, jump: Vec<u8>) -> String { + let mut s: Vec<u8> = Vec::new(); + for (i, c) in word.bytes().enumerate() { + let mut d = c + jump[i] % 26; + if (c <= 90 && d > 90) || d > 122 { + d -= 26; + } + s.push(d); + } + String::from_utf8(s).unwrap() +} diff --git a/challenge-212/roger-bell-west/rust/ch-2.rs b/challenge-212/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..543a88583e --- /dev/null +++ b/challenge-212/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,57 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +use std::collections::HashMap; + +#[test] +fn test_ex1() { + assert_eq!( + rearrangegroups(vec![1, 2, 3, 5, 1, 2, 7, 6, 3], 3), + vec![vec![1, 2, 3], vec![1, 2, 3], vec![5, 6, 7]] + ); +} + +#[test] +fn test_ex2() { + assert_eq!(rearrangegroups(vec![1, 2, 3], 2), Vec::<Vec<i32>>::new()); +} + +#[test] +fn test_ex3() { + assert_eq!(rearrangegroups(vec![1, 2, 4, 3, 5, 3], 3), vec![vec![1, 2, 3], vec![3, 4, 5]]); +} + +#[test] +fn test_ex4() { + assert_eq!(rearrangegroups(vec![1, 5, 2, 6, 4, 7], 3), Vec::<Vec<i32>>::new()); +} + +fn rearrangegroups(list: Vec<i32>, size: i32) -> Vec<Vec<i32>> { + let mut h: HashMap<i32, usize> = HashMap::new(); + for k in list { + let en = h.entry(k).or_insert(0); + *en += 1; + } + let mut out = Vec::new(); + loop { + let m = h.keys().min().unwrap(); + let res = (*m..m + size).collect::<Vec<i32>>(); + for n in &res { + if h.contains_key(n) { + let p = h.get(n).unwrap() - 1; + if p == 0 { + h.remove(n); + } else { + h.insert(*n, p); + } + } else { + return Vec::new(); + } + } + out.push(res); + if h.len() == 0 { + break; + } + } + out +} diff --git a/challenge-212/roger-bell-west/tests.yaml b/challenge-212/roger-bell-west/tests.yaml new file mode 100644 index 0000000000..f99c0c4532 --- /dev/null +++ b/challenge-212/roger-bell-west/tests.yaml @@ -0,0 +1,76 @@ +--- +ch-1: + - function: jumpingletters + multiarg: true + arguments: + - Perl + - - 2 + - 22 + - 19 + - 9 + result: Raku + - multiarg: true + arguments: + - Raku + - - 24 + - 4 + - 7 + - 17 + result: Perl +ch-2: + - function: rearrangegroups + multiarg: true + arguments: + - - 1 + - 2 + - 3 + - 5 + - 1 + - 2 + - 7 + - 6 + - 3 + - 3 + result: + - - 1 + - 2 + - 3 + - - 1 + - 2 + - 3 + - - 5 + - 6 + - 7 + - multiarg: true + arguments: + |
