From 1dab6f83512b9ae7a3d5347234470f8d6f4e4598 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Tue, 28 May 2024 09:56:15 +0100 Subject: RogerBW solutions for challenge no. 271 --- challenge-271/roger-bell-west/crystal/ch-1.cr | 25 +++ challenge-271/roger-bell-west/crystal/ch-2.cr | 25 +++ challenge-271/roger-bell-west/javascript/ch-1.js | 28 +++ challenge-271/roger-bell-west/javascript/ch-2.js | 71 +++++++ challenge-271/roger-bell-west/kotlin/ch-1.kt | 30 +++ challenge-271/roger-bell-west/kotlin/ch-2.kt | 39 ++++ challenge-271/roger-bell-west/lua/ch-1.lua | 41 ++++ challenge-271/roger-bell-west/lua/ch-2.lua | 72 +++++++ challenge-271/roger-bell-west/perl/ch-1.pl | 24 +++ challenge-271/roger-bell-west/perl/ch-2.pl | 28 +++ challenge-271/roger-bell-west/postscript/ch-1.ps | 100 ++++++++++ challenge-271/roger-bell-west/postscript/ch-2.ps | 239 +++++++++++++++++++++++ challenge-271/roger-bell-west/python/ch-1.py | 21 ++ challenge-271/roger-bell-west/python/ch-2.py | 22 +++ challenge-271/roger-bell-west/raku/ch-1.p6 | 22 +++ challenge-271/roger-bell-west/raku/ch-2.p6 | 25 +++ challenge-271/roger-bell-west/ruby/ch-1.rb | 30 +++ challenge-271/roger-bell-west/ruby/ch-2.rb | 41 ++++ challenge-271/roger-bell-west/rust/ch-1.rs | 29 +++ challenge-271/roger-bell-west/rust/ch-2.rs | 25 +++ challenge-271/roger-bell-west/scala/ch-1.scala | 29 +++ challenge-271/roger-bell-west/scala/ch-2.scala | 41 ++++ challenge-271/roger-bell-west/tests.json | 38 ++++ 23 files changed, 1045 insertions(+) create mode 100755 challenge-271/roger-bell-west/crystal/ch-1.cr create mode 100755 challenge-271/roger-bell-west/crystal/ch-2.cr create mode 100755 challenge-271/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-271/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-271/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-271/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-271/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-271/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-271/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-271/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-271/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-271/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-271/roger-bell-west/python/ch-1.py create mode 100755 challenge-271/roger-bell-west/python/ch-2.py create mode 100755 challenge-271/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-271/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-271/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-271/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-271/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-271/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-271/roger-bell-west/scala/ch-1.scala create mode 100644 challenge-271/roger-bell-west/scala/ch-2.scala create mode 100644 challenge-271/roger-bell-west/tests.json diff --git a/challenge-271/roger-bell-west/crystal/ch-1.cr b/challenge-271/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..b892354b6f --- /dev/null +++ b/challenge-271/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,25 @@ +#! /usr/bin/crystal + +def maximumones(a) + ax = a.map { |r| r.sum } + am = ax.max + ax.each_with_index do |n, i| + if n == am + return i + 1 + end + end + 0 +end + +require "spec" +describe "#tmpl_var name=function>" do + it "test_ex1" do + maximumones([[0, 1], [1, 0]]).should eq 1 + end + it "test_ex2" do + maximumones([[0, 0, 0], [1, 0, 1]]).should eq 2 + end + it "test_ex3" do + maximumones([[0, 0], [1, 1], [0, 0]]).should eq 2 + end +end diff --git a/challenge-271/roger-bell-west/crystal/ch-2.cr b/challenge-271/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..bd112f9963 --- /dev/null +++ b/challenge-271/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,25 @@ +#! /usr/bin/crystal + +def sortbyonebits(a) + c = Hash(Int32, Int32).new + Set.new(a).each do |n| + c[n] = n.popcount + end + a.sort do |aa, bb| + if c[aa] == c[bb] + aa <=> bb + else + c[aa] <=> c[bb] + end + end +end + +require "spec" +describe "#tmpl_var name=function>" do + it "test_ex1" do + sortbyonebits([0, 1, 2, 3, 4, 5, 6, 7, 8]).should eq [0, 1, 2, 4, 8, 3, 5, 6, 7] + end + it "test_ex2" do + sortbyonebits([1024, 512, 256, 128, 64]).should eq [64, 128, 256, 512, 1024] + end +end diff --git a/challenge-271/roger-bell-west/javascript/ch-1.js b/challenge-271/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..c1feb13490 --- /dev/null +++ b/challenge-271/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,28 @@ +#! /usr/bin/node + +"use strict" + +function maximumones(a) { + const ax = a.map(r => r.reduce((x, y) => x + y)); + const am = Math.max(...ax); + return ax.indexOf(am) + 1; +} + +if (maximumones([[0, 1], [1, 0]]) == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maximumones([[0, 0, 0], [1, 0, 1]]) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maximumones([[0, 0], [1, 1], [0, 0]]) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-271/roger-bell-west/javascript/ch-2.js b/challenge-271/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..2ab90b66f5 --- /dev/null +++ b/challenge-271/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,71 @@ +#! /usr/bin/node + +"use strict" + +function popcount64(x0) { + const M1 = 0x5555555555555555n; + const M2 = 0x3333333333333333n; + const M4 = 0x0f0f0f0f0f0f0f0fn; + const H01 = 0x0101010101010101n; + let x = BigInt(x0); + x -= (x >> 1n) & M1; + x = (x & M2) + ((x >> 2n) & M2); + x = (x + (x >> 4n)) & M4; + return Number((x * H01) >> 56n); +} + +function sortbyonebits(a) { + let b = a; + let c = new Map; + for (let n of a) { + c.set(n, popcount64(n)); + } + b.sort(function(aa, bb) { + if (c.get(aa) == c.get(bb)) { + return aa - bb; + } else { + return c.get(aa) - c.get(bb); + } + }); + return b; +} + +// 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; + } +} + +if (deepEqual(sortbyonebits([0, 1, 2, 3, 4, 5, 6, 7, 8]), [0, 1, 2, 4, 8, 3, 5, 6, 7])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(sortbyonebits([1024, 512, 256, 128, 64]), [64, 128, 256, 512, 1024])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-271/roger-bell-west/kotlin/ch-1.kt b/challenge-271/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..12d2840d6f --- /dev/null +++ b/challenge-271/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,30 @@ +import kotlin.math.* + +fun maximumones(a: List>): Int { + val ax = a.map{it.sum()}.toList() + val am = ax.maxOrNull()!! + return ax.indexOf(am) + 1 +} + +fun main() { + + if (maximumones(listOf(listOf(0, 1), listOf(1, 0))) == 1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maximumones(listOf(listOf(0, 0, 0), listOf(1, 0, 1))) == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maximumones(listOf(listOf(0, 0), listOf(1, 1), listOf(0, 0))) == 2) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-271/roger-bell-west/kotlin/ch-2.kt b/challenge-271/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..b4cd5688dd --- /dev/null +++ b/challenge-271/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,39 @@ +fun popcount64(x0: Int): Int { + val M1 = 0x5555555555555555 + val M2 = 0x3333333333333333 + val M4 = 0x0f0f0f0f0f0f0f0f + val H01 = 0x0101010101010101 + var x = x0.toLong() + x -= (x shr 1) and M1 + x = (x and M2) + ((x shr 2) and M2) + x = (x + (x shr 4)) and M4 + return ((x * H01) shr 56).toInt() +} + +fun sortbyonebits(a: List): List { + var b = ArrayList(a) + var c = mutableMapOf() + for (v in a.toSet()) { + c[v] = popcount64(v); + } + b.sort() + b.sortBy { c[it] } + return b.toList() +} + +fun main() { + + if (sortbyonebits(listOf(0, 1, 2, 3, 4, 5, 6, 7, 8)) == listOf(0, 1, 2, 4, 8, 3, 5, 6, 7)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (sortbyonebits(listOf(1024, 512, 256, 128, 64)) == listOf(64, 128, 256, 512, 1024)) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-271/roger-bell-west/lua/ch-1.lua b/challenge-271/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..f905d09c15 --- /dev/null +++ b/challenge-271/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,41 @@ +#! /usr/bin/lua + +function maximumones(a) + local ax = {} + for i, r in ipairs(a) do + local s = 0 + for j, t in ipairs(r) do + s = s + t + end + table.insert(ax, s) + end + local am = math.max(table.unpack(ax)) + for i, n in ipairs(ax) do + if n == am then + return i + end + end + return 0 +end + +if maximumones({{0, 1}, {1, 0}}) == 1 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maximumones({{0, 0, 0}, {1, 0, 1}}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maximumones({{0, 0}, {1, 1}, {0, 0}}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-271/roger-bell-west/lua/ch-2.lua b/challenge-271/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..2dcf713dec --- /dev/null +++ b/challenge-271/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,72 @@ +#! /usr/bin/lua + +function popcount(x0) -- adapted from https://gist.github.com/davidm/2065267 + local x = x0 + local c = 0 + while x ~= 0 do + x = x & (x - 1) + c = c + 1 + end + return c +end + +function sortbyonebits(a) + local b = a + local c = {} + for _, v in ipairs(b) do + if c[v] == nil then + c[v] = popcount(v) + end + end + table.sort(b, function(a, b) + if c[a] == c[b] then + return a < b + else + return c[a] < c[b] + end + end) + return b +end + +-- 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 + +if recursive_compare(sortbyonebits({0, 1, 2, 3, 4, 5, 6, 7, 8}), {0, 1, 2, 4, 8, 3, 5, 6, 7}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(sortbyonebits({1024, 512, 256, 128, 64}), {64, 128, 256, 512, 1024}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-271/roger-bell-west/perl/ch-1.pl b/challenge-271/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..dd9e831a7f --- /dev/null +++ b/challenge-271/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 => 3; + +is(maximumones([[0, 1], [1, 0]]), 1, 'example 1'); +is(maximumones([[0, 0, 0], [1, 0, 1]]), 2, 'example 2'); +is(maximumones([[0, 0], [1, 1], [0, 0]]), 2, 'example 3'); + +use List::Util qw(sum max); + +sub maximumones($a) { + my @ax = map {sum(@{$_})} @{$a}; + my $am = max(@ax); + while (my ($i, $n) = each @ax) { + if ($n == $am) { + return $i + 1; + } + } + return 0; +} diff --git a/challenge-271/roger-bell-west/perl/ch-2.pl b/challenge-271/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..025ed6a2f6 --- /dev/null +++ b/challenge-271/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,28 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is_deeply(sortbyonebits([0, 1, 2, 3, 4, 5, 6, 7, 8]), [0, 1, 2, 4, 8, 3, 5, 6, 7], 'example 1'); +is_deeply(sortbyonebits([1024, 512, 256, 128, 64]), [64, 128, 256, 512, 1024], 'example 2'); + +sub popcount64($x0) { + no warnings 'portable'; + use constant M1 => 0x5555555555555555; + use constant M2 => 0x3333333333333333; + use constant M4 => 0x0f0f0f0f0f0f0f0f; + use constant H01 => 0x0101010101010101; + my $x = $x0; + $x -= ($x >> 1) & M1; + $x = ($x & M2) + (($x >> 2) & M2); + $x = ($x + ($x >> 4)) & M4; + return ($x * H01) >> 56; +} + +sub sortbyonebits($a) { + my %c = map {$_ => popcount64($_)} @{$a}; + return [sort {$c{$::a} <=> $c{$::b} || $::a <=> $::b} @{$a}]; +} diff --git a/challenge-271/roger-bell-west/postscript/ch-1.ps b/challenge-271/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..0ae7a744b4 --- /dev/null +++ b/challenge-271/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,100 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 + +/listmax { + { max } 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 + +/map { % array proc -> array + 2 dict begin + /p exch def + [ exch + { + p + } forall + ] + 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 + +/enumerate.array { + 1 dict begin + /a exch def + [ + 0 1 a length 1 sub { + [ exch dup a exch get ] + } for + ] + end +} bind def + + +% end included library code + +/maximumones { + 0 dict begin + /ax exch { { add } reduce } map def + /am ax listmax def + -1 + ax enumerate.array { + aload pop + /n exch def + /i exch def + n am eq { + pop i + exit + } if + } forall + 1 add + end +} bind def + +(maximumones) test.start +[[0 1] [1 0]] maximumones 1 eq test +[[0 0 0] [1 0 1]] maximumones 2 eq test +[[0 0] [1 1] [0 0]] maximumones 2 eq test +test.end diff --git a/challenge-271/roger-bell-west/postscript/ch-2.ps b/challenge-271/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..b11dd84478 --- /dev/null +++ b/challenge-271/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,239 @@ +%!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 + +/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 + +/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 + +/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.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} bind def + +/unique { + toset keys +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} 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 + +/toset { % array -> dict of (value, true) + << exch + { + true + } forall + >> +} 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 + +/popcount32 { + 0 dict begin + /M1 16#55555555 def + /M2 16#33333333 def + /M4 16#0f0f0f0f def + /H01 16#01010101 def + /x exch def + /x x x -1 bitshift M1 and sub def + /x x M2 and x -2 bitshift M2 and add def + /x x x -4 bitshift add M4 and def + x H01 mul -24 bitshift + end +} bind def + +/sortbyonebits { + 2 dict begin + /a exch def + /c << + a unique { + dup popcount32 + } forall + >> def + a { + 2 copy c exch get exch c exch get exch quicksort.cmp + dup + 0 eq { + pop + quicksort.cmp + } { + 3 1 roll pop pop + } ifelse + } quicksort.with_comparator + end +} bind def + +(sortbyonebits) test.start +[0 1 2 3 4 5 6 7 8] sortbyonebits [0 1 2 4 8 3 5 6 7] deepeq test +[1024 512 256 128 64] sortbyonebits [64 128 256 512 1024] deepeq test +test.end diff --git a/challenge-271/roger-bell-west/python/ch-1.py b/challenge-271/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..b9dfca3fb9 --- /dev/null +++ b/challenge-271/roger-bell-west/python/ch-1.py @@ -0,0 +1,21 @@ +#! /usr/bin/python3 + +def maximumones(a): + ax = [sum(r) for r in a] + am = max(ax) + return [i for i, n in enumerate(ax) if n == am][0] + 1 + +import unittest + +class TestMaximumones(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(maximumones([[0, 1], [1, 0]]), 1, 'example 1') + + def test_ex2(self): + self.assertEqual(maximumones([[0, 0, 0], [1, 0, 1]]), 2, 'example 2') + + def test_ex3(self): + self.assertEqual(maximumones([[0, 0], [1, 1], [0, 0]]), 2, 'example 3') + +unittest.main() diff --git a/challenge-271/roger-bell-west/python/ch-2.py b/challenge-271/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..07cd54d1f1 --- /dev/null +++ b/challenge-271/roger-bell-west/python/ch-2.py @@ -0,0 +1,22 @@ +#! /usr/bin/python3 + +def sortbyonebits(a): + c = dict() + for i in a: + c[i] = i.bit_count() + b = a + b.sort() + b.sort(key = lambda i: c[i]) + return b + +import unittest + +class TestSortbyonebits(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(sortbyonebits([0, 1, 2, 3, 4, 5, 6, 7, 8]), [0, 1, 2, 4, 8, 3, 5, 6, 7], 'example 1') + + def test_ex2(self): + self.assertEqual(sortbyonebits([1024, 512, 256, 128, 64]), [64, 128, 256, 512, 1024], 'example 2') + +unittest.main() diff --git a/challenge-271/roger-bell-west/raku/ch-1.p6 b/challenge-271/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..1763a33cb3 --- /dev/null +++ b/challenge-271/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,22 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(maximumones([[0, 1], [1, 0]]), 1, 'example 1'); +is(maximumones([[0, 0, 0], [1, 0, 1]]), 2, 'example 2'); +is(maximumones([[0, 0], [1, 1], [0, 0]]), 2, 'example 3'); + +sub maximumones(@a) { + my @ax = @a.map({$_.sum()}); + my $am = @ax.max(); + for @ax.kv -> $i, $n { + if ($n == $am) { + return $i + 1; + } + } + return 0; +} + + diff --git a/challenge-271/roger-bell-west/raku/ch-2.p6 b/challenge-271/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..0852ebd042 --- /dev/null +++ b/challenge-271/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,25 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is-deeply(sortbyonebits([0, 1, 2, 3, 4, 5, 6, 7, 8]), [0, 1, 2, 4, 8, 3, 5, 6, 7], 'example 1'); +is-deeply(sortbyonebits([1024, 512, 256, 128, 64]), [64, 128, 256, 512, 1024], 'example 2'); + +sub popcount64($x0) { + constant $M1 = 0x5555555555555555; + constant $M2 = 0x3333333333333333; + constant $M4 = 0x0f0f0f0f0f0f0f0f; + constant $H01 = 0x0101010101010101; + my $x = $x0; + $x -= ($x +> 1) +& $M1; + $x = ($x +& $M2) + (($x +> 2) +& $M2); + $x = ($x + ($x +> 4)) +& $M4; + return ($x * $H01) +> 56; +} + +sub sortbyonebits(@a) { + my %c = map {$_ => popcount64($_)}, @a; + return [sort {%c{$^a} <=> %c{$^b} || $^a <=> $^b}, @a]; +} diff --git a/challenge-271/roger-bell-west/ruby/ch-1.rb b/challenge-271/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..5394f83402 --- /dev/null +++ b/challenge-271/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,30 @@ +#! /usr/bin/ruby + +def maximumones(a) + ax = a.map { |r| r.sum } + am = ax.max + ax.each_with_index do |n, i| + if n == am then + return i + 1 + end + end + return 0 +end + +require 'test/unit' + +class TestMaximumones < Test::Unit::TestCase + + def test_ex1 + assert_equal(1, maximumones([[0, 1], [1, 0]])) + end + + def test_ex2 + assert_equal(2, maximumones([[0, 0, 0], [1, 0, 1]])) + end + + def test_ex3 + assert_equal(2, maximumones([[0, 0], [1, 1], [0, 0]])) + end + +end diff --git a/challenge-271/roger-bell-west/ruby/ch-2.rb b/challenge-271/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..7a2ac232cd --- /dev/null +++ b/challenge-271/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,41 @@ +#! /usr/bin/ruby + +require "set" + +def popcount(x0) + x = x0 + c = 0 + while x > 0 do + x &= x - 1 + c += 1 + end + return c +end + +def sortbyonebits(a) + c = Hash.new + Set.new(a).each do |n| + c[n] = popcount(n) + end + return a.sort do |aa, bb| + if c[aa] == c[bb] then + aa <=> bb + else + c[aa] <=> c[bb] + end + end +end + +require 'test/unit' + +class TestSortbyonebits < Test::Unit::TestCase + + def test_ex1 + assert_equal([0, 1, 2, 4, 8, 3, 5, 6, 7], sortbyonebits([0, 1, 2, 3, 4, 5, 6, 7, 8])) + end + + def test_ex2 + assert_equal([64, 128, 256, 512, 1024], sortbyonebits([1024, 512, 256, 128, 64])) + end + +end diff --git a/challenge-271/roger-bell-west/rust/ch-1.rs b/challenge-271/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..26bc2ddf04 --- /dev/null +++ b/challenge-271/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!(maximumones(vec![vec![0, 1], vec![1, 0]]), 1); +} + +#[test] +fn test_ex2() { + assert_eq!(maximumones(vec![vec![0, 0, 0], vec![1, 0, 1]]), 2); +} + +#[test] +fn test_ex3() { + assert_eq!(maximumones(vec![vec![0, 0], vec![1, 1], vec![0, 0]]), 2); +} + +fn maximumones(a: Vec>) -> usize { + let ax = a.into_iter().map(|r| r.into_iter().sum()).collect::>(); + let am = ax.clone().into_iter().max().unwrap(); + ax.into_iter() + .enumerate() + .filter(|(_i, n)| *n == am) + .map(|(i, _n)| i) + .nth(0) + .unwrap() + + 1 +} diff --git a/challenge-271/roger-bell-west/rust/ch-2.rs b/challenge-271/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..584541f454 --- /dev/null +++ b/challenge-271/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,25 @@ +#! /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!( + sortbyonebits(vec![0, 1, 2, 3, 4, 5, 6, 7, 8]), + vec![0, 1, 2, 4, 8, 3, 5, 6, 7] + ); +} + +#[test] +fn test_ex2() { + assert_eq!( + sortbyonebits(vec![1024, 512, 256, 128, 64]), + vec![64, 128, 256, 512, 1024] + ); +} + +fn sortbyonebits(a0: Vec) -> Vec { + let mut a = a0.clone(); + a.sort_unstable(); + a.sort_by_cached_key(|x| x.count_ones()); + a +} diff --git a/challenge-271/roger-bell-west/scala/ch-1.scala b/challenge-271/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..be6ee99025 --- /dev/null +++ b/challenge-271/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,29 @@ + +object Maximumones { + def maximumones(a: List[List[Int]]): Int = { + val ax = a.map(r => r.sum).toList + val am = ax.max + ax.indexOf(am) + 1 + } + def main(args: Array[String]) { + if (maximumones(List(List(0, 1), List(1, 0))) == 1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maximumones(List(List(0, 0, 0), List(1, 0, 1))) == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maximumones(List(List(0, 0), List(1, 1), List(0, 0))) == 2) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-271/roger-bell-west/scala/ch-2.scala b/challenge-271/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..f8856caefd --- /dev/null +++ b/challenge-271/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,41 @@ + +object Sortbyonebits { + def popcount64(x0: Int): Int = { + val M1 = 0x5555555555555555L + val M2 = 0x3333333333333333L + val M4 = 0x0f0f0f0f0f0f0f0fL + val H01 = 0x0101010101010101L + var x = x0.toLong + x -= (x >> 1) & M1 + x = (x & M2) + ((x >> 2) & M2) + x = (x + (x >> 4)) & M4 + return ((x * H01) >> 56).toInt + } + def sortbyonebits(a: List[Int]): List[Int] = { + val c = a.map(n => n -> popcount64(n)).toMap + var b = a.toArray + b = b.sortWith((aa, bb) => { + if (c(aa) == c(bb)) { + aa < bb + } else { + c(aa) < c(bb) + } + }) + return b.toList + } + def main(args: Array[String]) { + if (sortbyonebits(List(0, 1, 2, 3, 4, 5, 6, 7, 8)) == List(0, 1, 2, 4, 8, 3, 5, 6, 7)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (sortbyonebits(List(1024, 512, 256, 128, 64)) == List(64, 128, 256, 512, 1024)) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-271/roger-bell-west/tests.json b/challenge-271/roger-bell-west/tests.json new file mode 100644 index 0000000000..b707e9057f --- /dev/null +++ b/challenge-271/roger-bell-west/tests.json @@ -0,0 +1,38 @@ +{ + "ch-1" : [ + { + "function" : "maximumones", + "arguments" : [ + [ 0, 1 ], + [ 1, 0 ] + ], + "result" : 1 + }, + { + "arguments" : [ + [ 0, 0, 0 ], + [ 1, 0, 1 ] + ], + "result" : 2 + }, + { + "arguments" : [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 0 ] + ], + "result" : 2 + } + ], + "ch-2" : [ + { + "function" : "sortbyonebits", + "arguments" : [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ], + "result" : [ 0, 1, 2, 4, 8, 3, 5, 6, 7 ] + }, + { + "arguments" : [ 1024, 512, 256, 128, 64 ], + "result" : [ 64, 128, 256, 512, 1024 ] + } + ] +} -- cgit