aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoger Bell_West <roger@firedrake.org>2024-05-15 16:31:05 +0100
committerRoger Bell_West <roger@firedrake.org>2024-05-15 16:31:05 +0100
commit2d5f707c0ba32eaadccd471701e47b8a0f1a6eff (patch)
tree138d79e20d1327464a77322476d59d8a73472810
parent64b8c830c57e76dc96d88d66ead474e829ccb220 (diff)
downloadperlweeklychallenge-club-2d5f707c0ba32eaadccd471701e47b8a0f1a6eff.tar.gz
perlweeklychallenge-club-2d5f707c0ba32eaadccd471701e47b8a0f1a6eff.tar.bz2
perlweeklychallenge-club-2d5f707c0ba32eaadccd471701e47b8a0f1a6eff.zip
RogerBW solutions for challenge no. 269
-rwxr-xr-xchallenge-269/roger-bell-west/crystal/ch-1.cr18
-rwxr-xr-xchallenge-269/roger-bell-west/crystal/ch-2.cr29
-rwxr-xr-xchallenge-269/roger-bell-west/javascript/ch-1.js26
-rwxr-xr-xchallenge-269/roger-bell-west/javascript/ch-2.js64
-rw-r--r--challenge-269/roger-bell-west/kotlin/ch-1.kt26
-rw-r--r--challenge-269/roger-bell-west/kotlin/ch-2.kt37
-rwxr-xr-xchallenge-269/roger-bell-west/lua/ch-1.lua37
-rwxr-xr-xchallenge-269/roger-bell-west/lua/ch-2.lua70
-rwxr-xr-xchallenge-269/roger-bell-west/perl/ch-1.pl15
-rwxr-xr-xchallenge-269/roger-bell-west/perl/ch-2.pl27
-rw-r--r--challenge-269/roger-bell-west/postscript/ch-1.ps61
-rw-r--r--challenge-269/roger-bell-west/postscript/ch-2.ps127
-rwxr-xr-xchallenge-269/roger-bell-west/python/ch-1.py19
-rwxr-xr-xchallenge-269/roger-bell-west/python/ch-2.py28
-rwxr-xr-xchallenge-269/roger-bell-west/raku/ch-1.p613
-rwxr-xr-xchallenge-269/roger-bell-west/raku/ch-2.p623
-rwxr-xr-xchallenge-269/roger-bell-west/ruby/ch-1.rb23
-rwxr-xr-xchallenge-269/roger-bell-west/ruby/ch-2.rb34
-rwxr-xr-xchallenge-269/roger-bell-west/rust/ch-1.rs21
-rwxr-xr-xchallenge-269/roger-bell-west/rust/ch-2.rs31
-rw-r--r--challenge-269/roger-bell-west/scala/ch-1.scala27
-rw-r--r--challenge-269/roger-bell-west/scala/ch-2.scala40
-rw-r--r--challenge-269/roger-bell-west/tests.json32
23 files changed, 828 insertions, 0 deletions
diff --git a/challenge-269/roger-bell-west/crystal/ch-1.cr b/challenge-269/roger-bell-west/crystal/ch-1.cr
new file mode 100755
index 0000000000..e8c77a2a4f
--- /dev/null
+++ b/challenge-269/roger-bell-west/crystal/ch-1.cr
@@ -0,0 +1,18 @@
+#! /usr/bin/crystal
+
+def bitwiseor(a)
+ a.select{|n| n % 2 == 0}.size >= 2
+end
+
+require "spec"
+describe "#tmpl_var name=function>" do
+ it "test_ex1" do
+ bitwiseor([1, 2, 3, 4, 5]).should eq true
+ end
+ it "test_ex2" do
+ bitwiseor([2, 3, 8, 16]).should eq true
+ end
+ it "test_ex3" do
+ bitwiseor([1, 2, 5, 7, 9]).should eq false
+ end
+end
diff --git a/challenge-269/roger-bell-west/crystal/ch-2.cr b/challenge-269/roger-bell-west/crystal/ch-2.cr
new file mode 100755
index 0000000000..38aee32674
--- /dev/null
+++ b/challenge-269/roger-bell-west/crystal/ch-2.cr
@@ -0,0 +1,29 @@
+#! /usr/bin/crystal
+
+def distributeelements(a)
+ x = [a[0]]
+ y = [a[1]]
+ (2 ... a.size).each do |i|
+ n = a[i]
+ if x[-1] > y[-1]
+ x.push(n)
+ else
+ y.push(n)
+ end
+ end
+ x.concat(y)
+ x
+end
+
+require "spec"
+describe "#tmpl_var name=function>" do
+ it "test_ex1" do
+ distributeelements([2, 1, 3, 4, 5]).should eq [2, 3, 4, 5, 1]
+ end
+ it "test_ex2" do
+ distributeelements([3, 2, 4]).should eq [3, 4, 2]
+ end
+ it "test_ex3" do
+ distributeelements([5, 4, 3, 8]).should eq [5, 3, 4, 8]
+ end
+end
diff --git a/challenge-269/roger-bell-west/javascript/ch-1.js b/challenge-269/roger-bell-west/javascript/ch-1.js
new file mode 100755
index 0000000000..d311aeb670
--- /dev/null
+++ b/challenge-269/roger-bell-west/javascript/ch-1.js
@@ -0,0 +1,26 @@
+#! /usr/bin/node
+
+"use strict"
+
+function bitwiseor(a) {
+ return a.filter(n => n % 2 == 0).length >= 2;
+}
+
+if (bitwiseor([1, 2, 3, 4, 5])) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (bitwiseor([2, 3, 8, 16])) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (!bitwiseor([1, 2, 5, 7, 9])) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write("\n");
diff --git a/challenge-269/roger-bell-west/javascript/ch-2.js b/challenge-269/roger-bell-west/javascript/ch-2.js
new file mode 100755
index 0000000000..74ff3197da
--- /dev/null
+++ b/challenge-269/roger-bell-west/javascript/ch-2.js
@@ -0,0 +1,64 @@
+#! /usr/bin/node
+
+"use strict"
+
+function distributeelements(a) {
+ let x = [a[0]];
+ let y = [a[1]];
+ a.slice(2).forEach(n => {
+ if (x.at(-1) > y.at(-1)) {
+ x.push(n);
+ } else {
+ y.push(n);
+ }
+ });
+ x.push(...y);
+ return x;
+}
+
+
+// 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(distributeelements([2, 1, 3, 4, 5]), [2, 3, 4, 5, 1])) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (deepEqual(distributeelements([3, 2, 4]), [3, 4, 2])) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (deepEqual(distributeelements([5, 4, 3, 8]), [5, 3, 4, 8])) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write("\n");
diff --git a/challenge-269/roger-bell-west/kotlin/ch-1.kt b/challenge-269/roger-bell-west/kotlin/ch-1.kt
new file mode 100644
index 0000000000..ad0409cce1
--- /dev/null
+++ b/challenge-269/roger-bell-west/kotlin/ch-1.kt
@@ -0,0 +1,26 @@
+fun bitwiseor(a: List<Int>): Boolean {
+ return a.filter {it % 2 == 0}.size >= 2
+}
+
+fun main() {
+
+ if (bitwiseor(listOf(1, 2, 3, 4, 5))) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ print(" ")
+ if (bitwiseor(listOf(2, 3, 8, 16))) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ print(" ")
+ if (!bitwiseor(listOf(1, 2, 5, 7, 9))) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ println("")
+
+}
diff --git a/challenge-269/roger-bell-west/kotlin/ch-2.kt b/challenge-269/roger-bell-west/kotlin/ch-2.kt
new file mode 100644
index 0000000000..4c87c82276
--- /dev/null
+++ b/challenge-269/roger-bell-west/kotlin/ch-2.kt
@@ -0,0 +1,37 @@
+fun distributeelements(a: List<Int>): List<Int> {
+ var x = ArrayList<Int>(listOf(a[0]))
+ var y = ArrayList<Int>(listOf(a[1]))
+ a.drop(2).forEach { n -> run {
+ if (x.last() > y.last()) {
+ x.add(n)
+ } else {
+ y.add(n)
+ }
+ }
+ }
+ y.toCollection(x)
+ return x.toList()
+}
+
+fun main() {
+
+ if (distributeelements(listOf(2, 1, 3, 4, 5)) == listOf(2, 3, 4, 5, 1)) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ print(" ")
+ if (distributeelements(listOf(3, 2, 4)) == listOf(3, 4, 2)) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ print(" ")
+ if (distributeelements(listOf(5, 4, 3, 8)) == listOf(5, 3, 4, 8)) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ println("")
+
+}
diff --git a/challenge-269/roger-bell-west/lua/ch-1.lua b/challenge-269/roger-bell-west/lua/ch-1.lua
new file mode 100755
index 0000000000..5a34ad1647
--- /dev/null
+++ b/challenge-269/roger-bell-west/lua/ch-1.lua
@@ -0,0 +1,37 @@
+#! /usr/bin/lua
+
+function bitwiseor(a)
+ local count = 0
+ for i, v in ipairs(a) do
+ if v % 2 == 0 then
+ count = count + 1
+ if count >= 2 then
+ return true
+ end
+ end
+ end
+ return false
+end
+
+
+if bitwiseor({1, 2, 3, 4, 5}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if bitwiseor({2, 3, 8, 16}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if not bitwiseor({1, 2, 5, 7, 9}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")
+
diff --git a/challenge-269/roger-bell-west/lua/ch-2.lua b/challenge-269/roger-bell-west/lua/ch-2.lua
new file mode 100755
index 0000000000..bbea7996df
--- /dev/null
+++ b/challenge-269/roger-bell-west/lua/ch-2.lua
@@ -0,0 +1,70 @@
+#! /usr/bin/lua
+
+function distributeelements(a)
+ local x = {a[1]}
+ local y = {a[2]}
+ for i, n in ipairs(a) do
+ if i > 2 then
+ if x[#x] > y[#y] then
+ table.insert(x, n)
+ else
+ table.insert(y, n)
+ end
+ end
+ end
+ for i, n in ipairs(y) do
+ table.insert(x, n)
+ end
+ return x
+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(distributeelements({2, 1, 3, 4, 5}), {2, 3, 4, 5, 1}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if recursive_compare(distributeelements({3, 2, 4}), {3, 4, 2}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if recursive_compare(distributeelements({5, 4, 3, 8}), {5, 3, 4, 8}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")
+
diff --git a/challenge-269/roger-bell-west/perl/ch-1.pl b/challenge-269/roger-bell-west/perl/ch-1.pl
new file mode 100755
index 0000000000..fa37078618
--- /dev/null
+++ b/challenge-269/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 => 3;
+
+is(bitwiseor([1, 2, 3, 4, 5]), 1, 'example 1');
+is(bitwiseor([2, 3, 8, 16]), 1, 'example 2');
+is(bitwiseor([1, 2, 5, 7, 9]), 0, 'example 3');
+
+sub bitwiseor($a) {
+ return (scalar grep {$_ % 2 == 0} @{$a}) >= 2 ? 1 : 0;
+}
diff --git a/challenge-269/roger-bell-west/perl/ch-2.pl b/challenge-269/roger-bell-west/perl/ch-2.pl
new file mode 100755
index 0000000000..d55cc4f9cf
--- /dev/null
+++ b/challenge-269/roger-bell-west/perl/ch-2.pl
@@ -0,0 +1,27 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+use experimental 'signatures';
+
+use Test::More tests => 3;
+
+is_deeply(distributeelements([2, 1, 3, 4, 5]), [2, 3, 4, 5, 1], 'example 1');
+is_deeply(distributeelements([3, 2, 4]), [3, 4, 2], 'example 2');
+is_deeply(distributeelements([5, 4, 3, 8]), [5, 3, 4, 8], 'example 3');
+
+sub distributeelements($a) {
+ my @x = ($a->[0]);
+ my @y = ($a->[1]);
+ foreach my $i (2 .. $#{$a}) {
+ my $n = $a->[$i];
+ if ($x[-1] > $y[-1]) {
+ push @x, $n;
+ } else {
+ push @y, $n;
+ }
+ }
+ use YAML::XS;
+ push @x, @y;
+ return \@x;
+}
diff --git a/challenge-269/roger-bell-west/postscript/ch-1.ps b/challenge-269/roger-bell-west/postscript/ch-1.ps
new file mode 100644
index 0000000000..49a0c8988e
--- /dev/null
+++ b/challenge-269/roger-bell-west/postscript/ch-1.ps
@@ -0,0 +1,61 @@
+%!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
+
+/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
+
+/filter { % array proc(bool) -> array
+ 1 dict begin
+ /p exch def
+ [ exch
+ {
+ dup p not
+ {
+ pop
+ } if
+ } forall
+ ]
+ end
+} bind def
+
+
+% end included library code
+
+/bitwiseor {
+ { 2 mod 0 eq } filter length 2 ge
+} bind def
+
+(bitwiseor) test.start
+[1 2 3 4 5] bitwiseor test
+[2 3 8 16] bitwiseor test
+[1 2 5 7 9] bitwiseor not test
+test.end
diff --git a/challenge-269/roger-bell-west/postscript/ch-2.ps b/challenge-269/roger-bell-west/postscript/ch-2.ps
new file mode 100644
index 0000000000..6610176b18
--- /dev/null
+++ b/challenge-269/roger-bell-west/postscript/ch-2.ps
@@ -0,0 +1,127 @@
+%!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.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
+
+/apush.right { % [a b] c -> [a b c]
+ exch
+ [ exch aload length 2 add -1 roll ]
+} 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
+
+/distributeelements {
+ 0 dict begin
+ aload length /l exch def
+ /x 1 array def
+ l -1 roll x exch 0 exch put
+ /y 1 array def
+ l 1 sub -1 roll y exch 0 exch put
+ l 2 sub array astore {
+ /n exch def
+ x dup length 1 sub get y dup length 1 sub get gt {
+ /x x n apush.right def
+ } {
+ /y y n apush.right def
+ } ifelse
+ } forall
+ x aload pop
+ y aload pop
+ l array astore
+ end
+} bind def
+
+(distributeelements) test.start
+[2 1 3 4 5] distributeelements [2 3 4 5 1] deepeq test
+[3 2 4] distributeelements [3 4 2] deepeq test
+[5 4 3 8] distributeelements [5 3 4 8] deepeq test
+test.end
diff --git a/challenge-269/roger-bell-west/python/ch-1.py b/challenge-269/roger-bell-west/python/ch-1.py
new file mode 100755
index 0000000000..0c8dd4c0e0
--- /dev/null
+++ b/challenge-269/roger-bell-west/python/ch-1.py
@@ -0,0 +1,19 @@
+#! /usr/bin/python3
+
+def bitwiseor(a):
+ return len([n for n in a if n % 2 == 0]) >= 2
+
+import unittest
+
+class TestBitwiseor(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(bitwiseor([1, 2, 3, 4, 5]), True, 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(bitwiseor([2, 3, 8, 16]), True, 'example 2')
+
+ def test_ex3(self):
+ self.assertEqual(bitwiseor([1, 2, 5, 7, 9]), False, 'example 3')
+
+unittest.main()
diff --git a/challenge-269/roger-bell-west/python/ch-2.py b/challenge-269/roger-bell-west/python/ch-2.py
new file mode 100755
index 0000000000..5631cb2587
--- /dev/null
+++ b/challenge-269/roger-bell-west/python/ch-2.py
@@ -0,0 +1,28 @@
+#! /usr/bin/python3
+
+def distributeelements(a):
+ x = [a[0]]
+ y = [a[1]]
+ for i in range(2, len(a)):
+ n = a[i]
+ if x[-1] > y[-1]:
+ x.append(n)
+ else:
+ y.append(n)
+ x.extend(y)
+ return x
+
+import unittest
+
+class TestDistributeelements(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(distributeelements([2, 1, 3, 4, 5]), [2, 3, 4, 5, 1], 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(distributeelements([3, 2, 4]), [3, 4, 2], 'example 2')
+
+ def test_ex3(self):
+ self.assertEqual(distributeelements([5, 4, 3, 8]), [5, 3, 4, 8], 'example 3')
+
+unittest.main()
diff --git a/challenge-269/roger-bell-west/raku/ch-1.p6 b/challenge-269/roger-bell-west/raku/ch-1.p6
new file mode 100755
index 0000000000..6c6617d7a1
--- /dev/null
+++ b/challenge-269/roger-bell-west/raku/ch-1.p6
@@ -0,0 +1,13 @@
+#! /usr/bin/raku
+
+use Test;
+
+plan 3;
+
+is(bitwiseor([1, 2, 3, 4, 5]), True, 'example 1');
+is(bitwiseor([2, 3, 8, 16]), True, 'example 2');
+is(bitwiseor([1, 2, 5, 7, 9]), False, 'example 3');
+
+sub bitwiseor(@a) {
+ return @a.grep({$_ % 2 == 0}).elems >= 2;
+}
diff --git a/challenge-269/roger-bell-west/raku/ch-2.p6 b/challenge-269/roger-bell-west/raku/ch-2.p6
new file mode 100755
index 0000000000..ab01625347
--- /dev/null
+++ b/challenge-269/roger-bell-west/raku/ch-2.p6
@@ -0,0 +1,23 @@
+#! /usr/bin/raku
+
+use Test;
+
+plan 3;
+
+is-deeply(distributeelements([2, 1, 3, 4, 5]), [2, 3, 4, 5, 1], 'example 1');
+is-deeply(distributeelements([3, 2, 4]), [3, 4, 2], 'example 2');
+is-deeply(distributeelements([5, 4, 3, 8]), [5, 3, 4, 8], 'example 3');
+
+sub distributeelements(@a) {
+ my @x = @a[0];
+ my @y = @a[1];
+ for @a.skip(2) -> $n {
+ if (@x[*-1] > @y[*-1]) {
+ @x.push($n);
+ } else {
+ @y.push($n);
+ }
+ }
+ @x.append(@y);
+ return @x;
+}
diff --git a/challenge-269/roger-bell-west/ruby/ch-1.rb b/challenge-269/roger-bell-west/ruby/ch-1.rb
new file mode 100755
index 0000000000..d91f3d21a5
--- /dev/null
+++ b/challenge-269/roger-bell-west/ruby/ch-1.rb
@@ -0,0 +1,23 @@
+#! /usr/bin/ruby
+
+def bitwiseor(a)
+ a.select{|n| n % 2 == 0}.size >= 2
+end
+
+require 'test/unit'
+
+class TestBitwiseor < Test::Unit::TestCase
+
+ def test_ex1
+ assert_equal(true, bitwiseor([1, 2, 3, 4, 5]))
+ end
+
+ def test_ex2
+ assert_equal(true, bitwiseor([2, 3, 8, 16]))
+ end
+
+ def test_ex3
+ assert_equal(false, bitwiseor([1, 2, 5, 7, 9]))
+ end
+
+end
diff --git a/challenge-269/roger-bell-west/ruby/ch-2.rb b/challenge-269/roger-bell-west/ruby/ch-2.rb
new file mode 100755
index 0000000000..f472d108a3
--- /dev/null
+++ b/challenge-269/roger-bell-west/ruby/ch-2.rb
@@ -0,0 +1,34 @@
+#! /usr/bin/ruby
+
+def distributeelements(a)
+ x = [a[0]]
+ y = [a[1]]
+ (2 ... a.size).each do |i|
+ n = a[i]
+ if x[-1] > y[-1]
+ x.push(n)
+ else
+ y.push(n)
+ end
+ end
+ x.concat(y)
+ x
+end
+
+require 'test/unit'
+
+class TestDistributeelements < Test::Unit::TestCase
+
+ def test_ex1
+ assert_equal([2, 3, 4, 5, 1], distributeelements([2, 1, 3, 4, 5]))
+ end
+
+ def test_ex2
+ assert_equal([3, 4, 2], distributeelements([3, 2, 4]))
+ end
+
+ def test_ex3
+ assert_equal([5, 3, 4, 8], distributeelements([5, 4, 3, 8]))
+ end
+
+end
diff --git a/challenge-269/roger-bell-west/rust/ch-1.rs b/challenge-269/roger-bell-west/rust/ch-1.rs
new file mode 100755
index 0000000000..3c9cbd6336
--- /dev/null
+++ b/challenge-269/roger-bell-west/rust/ch-1.rs
@@ -0,0 +1,21 @@
+#! /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!(bitwiseor(vec![1, 2, 3, 4, 5]), true);
+}
+
+#[test]
+fn test_ex2() {
+ assert_eq!(bitwiseor(vec![2, 3, 8, 16]), true);
+}
+
+#[test]
+fn test_ex3() {
+ assert_eq!(bitwiseor(vec![1, 2, 5, 7, 9]), false);
+}
+
+fn bitwiseor(a: Vec<u32>) -> bool {
+ a.into_iter().filter(|n| n % 2 == 0).count() >= 2
+}
diff --git a/challenge-269/roger-bell-west/rust/ch-2.rs b/challenge-269/roger-bell-west/rust/ch-2.rs
new file mode 100755
index 0000000000..b583074137
--- /dev/null
+++ b/challenge-269/roger-bell-west/rust/ch-2.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!(distributeelements(vec![2, 1, 3, 4, 5]), vec![2, 3, 4, 5, 1]);
+}
+
+#[test]
+fn test_ex2() {
+ assert_eq!(distributeelements(vec![3, 2, 4]), vec![3, 4, 2]);
+}
+
+#[test]
+fn test_ex3() {
+ assert_eq!(distributeelements(vec![5, 4, 3, 8]), vec![5, 3, 4, 8]);
+}
+
+fn distributeelements(a: Vec<u32>) -> Vec<u32> {
+ let mut x = vec![a[0]];
+ let mut y = vec![a[1]];
+ for n in a.into_iter().skip(2) {
+ if x.last().unwrap() > y.last().unwrap() {
+ x.push(n);
+ } else {
+ y.push(n);
+ }
+ }
+ x.append(&mut y);
+ x
+}
diff --git a/challenge-269/roger-bell-west/scala/ch-1.scala b/challenge-269/roger-bell-west/scala/ch-1.scala
new file mode 100644
index 0000000000..2d57835aa1
--- /dev/null
+++ b/challenge-269/roger-bell-west/scala/ch-1.scala
@@ -0,0 +1,27 @@
+
+object Bitwiseor {
+ def bitwiseor(a: List[Int]): Boolean = {
+ a.filter(n => n % 2 == 0).length >= 2
+ }
+ def main(args: Array[String]) {
+ if (bitwiseor(List(1, 2, 3, 4, 5))) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ print(" ")
+ if (bitwiseor(List(2, 3, 8, 16))) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ print(" ")
+ if (!bitwiseor(List(1, 2, 5, 7, 9))) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ println("")
+
+ }
+}
diff --git a/challenge-269/roger-bell-west/scala/ch-2.scala b/challenge-269/roger-bell-west/scala/ch-2.scala
new file mode 100644
index 0000000000..a05314ec24
--- /dev/null
+++ b/challenge-269/roger-bell-west/scala/ch-2.scala
@@ -0,0 +1,40 @@
+import scala.collection.mutable.ListBuffer
+
+object Distributeelements {
+ def distributeelements(a: List[Int]): List[Int] = {
+ var x = new ListBuffer[Int]
+ x += a(0)
+ var y = new ListBuffer[Int]
+ y += a(1)
+ a.drop(2).foreach(n => {
+ if (x.last > y.last) {
+ x += n
+ } else {
+ y += n
+ }
+ })
+ x.appendAll(y)
+ x.toList
+ }
+ def main(args: Array[String]) {
+ if (distributeelements(List(2, 1, 3, 4, 5)) == List(2, 3, 4, 5, 1)) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ print(" ")
+ if (distributeelements(List(3, 2, 4)) == List(3, 4, 2)) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ print(" ")
+ if (distributeelements(List(5, 4, 3, 8)) == List(5, 3, 4, 8)) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ println("")
+
+ }
+}
diff --git a/challenge-269/roger-bell-west/tests.json b/challenge-269/roger-bell-west/tests.json
new file mode 100644
index 0000000000..40e0ba8090
--- /dev/null
+++ b/challenge-269/roger-bell-west/tests.json
@@ -0,0 +1,32 @@
+{
+ "ch-1" : [
+ {
+ "function" : "bitwiseor",
+ "arguments" : [ 1, 2, 3, 4, 5 ],
+ "result" : true
+ },
+ {
+ "arguments" : [ 2, 3, 8, 16 ],
+ "result" : true
+ },
+ {
+ "arguments" : [ 1, 2, 5, 7, 9 ],
+ "result" : false
+ }
+ ],
+ "ch-2" : [
+ {
+ "function" : "distributeelements",
+ "arguments" : [ 2, 1, 3, 4, 5 ],
+ "result" : [ 2, 3, 4, 5, 1 ]
+ },
+ {
+ "arguments" : [ 3, 2, 4 ],
+ "result" : [ 3, 4, 2 ]
+ },
+ {
+ "arguments" : [ 5, 4, 3, 8 ],
+ "result" : [ 5, 3, 4, 8 ]
+ }
+ ]
+}