aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoger Bell_West <roger@firedrake.org>2025-01-28 12:18:54 +0000
committerRoger Bell_West <roger@firedrake.org>2025-01-28 12:18:54 +0000
commitffd1c49cd82e7bd0be11ad57ac31090c0dc99b2e (patch)
treeb2ef3d7b4f57f9c049bbc3d1279f32074b4c3470
parent4bb3874d4bbd36d0b85b74031a74479faa9306b7 (diff)
downloadperlweeklychallenge-club-ffd1c49cd82e7bd0be11ad57ac31090c0dc99b2e.tar.gz
perlweeklychallenge-club-ffd1c49cd82e7bd0be11ad57ac31090c0dc99b2e.tar.bz2
perlweeklychallenge-club-ffd1c49cd82e7bd0be11ad57ac31090c0dc99b2e.zip
RogerBW solutions for challenge no. 306
-rwxr-xr-xchallenge-306/roger-bell-west/crystal/ch-1.cr24
-rwxr-xr-xchallenge-306/roger-bell-west/crystal/ch-2.cr29
-rwxr-xr-xchallenge-306/roger-bell-west/javascript/ch-1.js37
-rwxr-xr-xchallenge-306/roger-bell-west/javascript/ch-2.js36
-rw-r--r--challenge-306/roger-bell-west/kotlin/ch-1.kt28
-rw-r--r--challenge-306/roger-bell-west/kotlin/ch-2.kt34
-rwxr-xr-xchallenge-306/roger-bell-west/lua/ch-1.lua43
-rwxr-xr-xchallenge-306/roger-bell-west/lua/ch-2.lua34
-rwxr-xr-xchallenge-306/roger-bell-west/perl/ch-1.pl28
-rwxr-xr-xchallenge-306/roger-bell-west/perl/ch-2.pl28
-rw-r--r--challenge-306/roger-bell-west/postscript/ch-1.ps90
-rw-r--r--challenge-306/roger-bell-west/postscript/ch-2.ps163
-rwxr-xr-xchallenge-306/roger-bell-west/python/ch-1.py36
-rwxr-xr-xchallenge-306/roger-bell-west/python/ch-2.py26
-rwxr-xr-xchallenge-306/roger-bell-west/raku/ch-1.p620
-rwxr-xr-xchallenge-306/roger-bell-west/raku/ch-2.p626
-rwxr-xr-xchallenge-306/roger-bell-west/ruby/ch-1.rb27
-rwxr-xr-xchallenge-306/roger-bell-west/ruby/ch-2.rb33
-rwxr-xr-xchallenge-306/roger-bell-west/rust/ch-1.rs25
-rwxr-xr-xchallenge-306/roger-bell-west/rust/ch-2.rs30
-rw-r--r--challenge-306/roger-bell-west/scala/ch-1.scala29
-rw-r--r--challenge-306/roger-bell-west/scala/ch-2.scala42
-rw-r--r--challenge-306/roger-bell-west/tests.json24
23 files changed, 892 insertions, 0 deletions
diff --git a/challenge-306/roger-bell-west/crystal/ch-1.cr b/challenge-306/roger-bell-west/crystal/ch-1.cr
new file mode 100755
index 0000000000..572acd56aa
--- /dev/null
+++ b/challenge-306/roger-bell-west/crystal/ch-1.cr
@@ -0,0 +1,24 @@
+#! /usr/bin/crystal
+
+def oddsum(a)
+ out = a.sum
+ l = 3
+ while l <= a.size
+ a.each_cons(l) do |s|
+ out += s.sum
+ end
+ l += 2
+ end
+ out
+end
+
+require "spec"
+
+describe "oddsum" do
+ it "test_ex1" do
+ oddsum([2, 5, 3, 6, 4]).should eq 77
+ end
+ it "test_ex2" do
+ oddsum([1, 3]).should eq 4
+ end
+end
diff --git a/challenge-306/roger-bell-west/crystal/ch-2.cr b/challenge-306/roger-bell-west/crystal/ch-2.cr
new file mode 100755
index 0000000000..d97d24c278
--- /dev/null
+++ b/challenge-306/roger-bell-west/crystal/ch-2.cr
@@ -0,0 +1,29 @@
+#! /usr/bin/crystal
+
+def lastelement(a)
+ b = a
+ while true
+ b.sort!
+ f = b.pop
+ s = b.pop
+ if f > s
+ b.push(f - s)
+ end
+ if b.size == 0
+ return 0
+ end
+ if b.size == 1
+ return b[0]
+ end
+ end
+end
+
+require "spec"
+describe "lastelement" do
+ it "test_ex1" do
+ lastelement([3, 8, 5, 2, 9, 2]).should eq 1
+ end
+ it "test_ex2" do
+ lastelement([3, 2, 5]).should eq 0
+ end
+end
diff --git a/challenge-306/roger-bell-west/javascript/ch-1.js b/challenge-306/roger-bell-west/javascript/ch-1.js
new file mode 100755
index 0000000000..d0875522fb
--- /dev/null
+++ b/challenge-306/roger-bell-west/javascript/ch-1.js
@@ -0,0 +1,37 @@
+#! /usr/bin/node
+
+"use strict"
+
+// by VLAZ
+// https://stackoverflow.com/a/59322890
+function toWindows(inputArray, size) {
+ return Array.from(
+ {length: inputArray.length - (size - 1)}, //get the appropriate length
+ (_, index) => inputArray.slice(index, index+size) //create the windows
+ )
+}
+
+function oddsum(a) {
+ let out = a.reduce((x, y) => x + y);
+ let l = 3;
+ while (l <= a.length) {
+ for (let s of toWindows(a, l)) {
+ out += s.reduce((x, y) => x + y);
+ }
+ l += 2;
+ }
+ return out;
+}
+
+if (oddsum([2, 5, 3, 6, 4]) == 77) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (oddsum([1, 3]) == 4) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write("\n");
diff --git a/challenge-306/roger-bell-west/javascript/ch-2.js b/challenge-306/roger-bell-west/javascript/ch-2.js
new file mode 100755
index 0000000000..910ba82fa0
--- /dev/null
+++ b/challenge-306/roger-bell-west/javascript/ch-2.js
@@ -0,0 +1,36 @@
+#! /usr/bin/node
+
+"use strict"
+
+function lastelement(a) {
+ let b = [...a];
+ while (true) {
+ b.sort(function(aa, bb) {
+ return aa - bb;
+ });
+ let f = b.pop();
+ let s = b.pop();
+ if (f > s) {
+ b.push(f - s);
+ }
+ if (b.length == 0) {
+ return 0;
+ }
+ if (b.length == 1) {
+ return b[0];
+ }
+ }
+}
+
+if (lastelement([3, 8, 5, 2, 9, 2]) == 1) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (lastelement([3, 2, 5]) == 0) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write("\n");
diff --git a/challenge-306/roger-bell-west/kotlin/ch-1.kt b/challenge-306/roger-bell-west/kotlin/ch-1.kt
new file mode 100644
index 0000000000..d104603c62
--- /dev/null
+++ b/challenge-306/roger-bell-west/kotlin/ch-1.kt
@@ -0,0 +1,28 @@
+fun oddsum(a: List<Int>): Int {
+ var out = a.sum()
+ var l = 3
+ while (l <= a.size) {
+ for (s in a.windowed(l)) {
+ out += s.sum()
+ }
+ l += 2
+ }
+ return out
+}
+
+fun main() {
+
+ if (oddsum(listOf(2, 5, 3, 6, 4)) == 77) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ print(" ")
+ if (oddsum(listOf(1, 3)) == 4) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ println("")
+
+}
diff --git a/challenge-306/roger-bell-west/kotlin/ch-2.kt b/challenge-306/roger-bell-west/kotlin/ch-2.kt
new file mode 100644
index 0000000000..be572837bf
--- /dev/null
+++ b/challenge-306/roger-bell-west/kotlin/ch-2.kt
@@ -0,0 +1,34 @@
+fun lastelement(a: List<Int>): Int {
+ var b = ArrayList(a)
+ while (true) {
+ b.sort()
+ var f = b.removeLast()
+ var s = b.removeLast()
+ if (f > s) {
+ b.add(f - s)
+ }
+ if (b.size == 0) {
+ return 0
+ }
+ if (b.size == 1) {
+ return b[0]
+ }
+ }
+}
+
+fun main() {
+
+ if (lastelement(listOf(3, 8, 5, 2, 9, 2)) == 1) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ print(" ")
+ if (lastelement(listOf(3, 2, 5)) == 0) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ println("")
+
+}
diff --git a/challenge-306/roger-bell-west/lua/ch-1.lua b/challenge-306/roger-bell-west/lua/ch-1.lua
new file mode 100755
index 0000000000..1ab8b6d95c
--- /dev/null
+++ b/challenge-306/roger-bell-west/lua/ch-1.lua
@@ -0,0 +1,43 @@
+#! /usr/bin/lua
+
+function windowed(a, n)
+ local out = {}
+ for i = 1, #a - n + 1 do
+ local t = {}
+ for j = i, i + n - 1 do
+ table.insert(t, a[j])
+ end
+ table.insert(out, t)
+ end
+ return out
+end
+
+function oddsum(a)
+ local out = 0
+ local l = 1
+ while l <= #a do
+ for _i, s in ipairs(windowed(a, l)) do
+ for _j, nn in ipairs(s) do
+ out = out + nn
+ end
+ end
+ l = l + 2
+ end
+ return out
+end
+
+
+if oddsum({2, 5, 3, 6, 4}) == 77 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if oddsum({1, 3}) == 4 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")
+
diff --git a/challenge-306/roger-bell-west/lua/ch-2.lua b/challenge-306/roger-bell-west/lua/ch-2.lua
new file mode 100755
index 0000000000..1d3179e5cb
--- /dev/null
+++ b/challenge-306/roger-bell-west/lua/ch-2.lua
@@ -0,0 +1,34 @@
+#! /usr/bin/lua
+
+function lastelement(a)
+ local b = a
+ while true do
+ table.sort(b)
+ local f = table.remove(b, #b)
+ local s = table.remove(b, #b)
+ if f > s then
+ table.insert(b, f - s)
+ end
+ if #b == 0 then
+ return 0
+ end
+ if #b == 1 then
+ return b[1]
+ end
+ end
+end
+
+if lastelement({3, 8, 5, 2, 9, 2}) == 1 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if lastelement({3, 2, 5}) == 0 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")
+
diff --git a/challenge-306/roger-bell-west/perl/ch-1.pl b/challenge-306/roger-bell-west/perl/ch-1.pl
new file mode 100755
index 0000000000..21851fdb10
--- /dev/null
+++ b/challenge-306/roger-bell-west/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+use experimental 'signatures';
+
+use Test::More tests => 2;
+
+is(oddsum([2, 5, 3, 6, 4]), 77, 'example 1');
+is(oddsum([1, 3]), 4, 'example 2');
+
+use List::Util qw(sum);
+use List::MoreUtils qw(slideatatime);
+
+sub oddsum($a) {
+ my $out = sum(@{$a});
+ my $l = 3;
+ while ($l <= scalar @{$a}) {
+ my $dd = slideatatime 1, $l, @{$a};
+ while (my @s = $dd->()) {
+ if (scalar @s == $l) {
+ $out += sum(@s);
+ }
+ }
+ $l += 2;
+ }
+ $out;
+}
diff --git a/challenge-306/roger-bell-west/perl/ch-2.pl b/challenge-306/roger-bell-west/perl/ch-2.pl
new file mode 100755
index 0000000000..fca3c565bd
--- /dev/null
+++ b/challenge-306/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(lastelement([3, 8, 5, 2, 9, 2]), 1, 'example 1');
+is(lastelement([3, 2, 5]), 0, 'example 2');
+
+sub lastelement($a) {
+ my @b = @{$a};
+ while (1) {
+ @b = sort {$::a <=> $::b} @b;
+ my $f = pop @b;
+ my $s = pop @b;
+ if ($f > $s) {
+ push @b, $f - $s;
+ }
+ if (scalar @b == 0) {
+ return 0;
+ }
+ if (scalar @b == 1) {
+ return $b[0];
+ }
+ }
+}
diff --git a/challenge-306/roger-bell-west/postscript/ch-1.ps b/challenge-306/roger-bell-west/postscript/ch-1.ps
new file mode 100644
index 0000000000..9c53e9779c
--- /dev/null
+++ b/challenge-306/roger-bell-west/postscript/ch-1.ps
@@ -0,0 +1,90 @@
+%!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
+
+/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
+
+/rotor {
+ 5 dict begin
+ /delta exch def
+ /size exch def
+ dup length /len exch def
+ /ar exch def
+ /ix 0 def
+ [
+ {
+ ix size add len gt {
+ exit
+ } if
+ ar ix size getinterval
+ /ix ix size delta add add def
+ } loop
+ ]
+ 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
+
+
+% end included library code
+
+/oddsum {
+ 0 dict begin
+ /a exch def
+ a { add } reduce
+ /l 3 def
+ {
+ l a length gt {
+ exit
+ } if
+ a l dup 1 sub neg rotor {
+ { add } reduce add
+ } forall
+ /l l 2 add def
+ } loop
+ end
+} bind def
+
+(oddsum) test.start
+[2 5 3 6 4] oddsum 77 eq test
+[1 3] oddsum 4 eq test
+test.end
diff --git a/challenge-306/roger-bell-west/postscript/ch-2.ps b/challenge-306/roger-bell-west/postscript/ch-2.ps
new file mode 100644
index 0000000000..2769a698ce
--- /dev/null
+++ b/challenge-306/roger-bell-west/postscript/ch-2.ps
@@ -0,0 +1,163 @@
+%!PS
+
+% begin included library code
+% see https://codeberg.org/Firedrake/postscript-libraries/
+/apop.right { % [a b c] -> [a b] c
+ [ exch aload length 1 add 1 roll ] exch
+} 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
+
+/apush.right { % [a b] c -> [a b c]
+ exch
+ [ exch aload length 2 add -1 roll ]
+} bind def
+
+/quicksort.cmp {
+ 2 copy
+ lt {
+ pop pop -1
+ } {
+ gt {
+ 1
+ } {
+ 0
+ } ifelse
+ } ifelse
+} bind def
+
+/test.start {
+ print (:) print
+ /test.pass 0 def
+ /test.count 0 def
+} 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 {
+ { quicksort.cmp } quicksort.with_comparator
+} bind def
+
+/test.end {
+ ( ) print
+ test.count 0 gt {
+ (Passed ) print
+ test.pass (...) cvs print
+ (/) print
+ test.count (...) cvs print
+ ( \() print
+ test.pass 100 mul test.count idiv (...) cvs print
+ (%\)) print
+ (\r\n) print
+ } if
+} bind def
+
+/test {
+ /test.count test.count 1 add def
+ {
+ /test.pass test.pass 1 add def
+ } {
+ ( ) print
+ test.count (....) cvs print
+ (-fail) print
+ } ifelse
+} bind def
+
+/quicksort.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
+
+
+% end included library code
+
+/lastelement {
+ 0 dict begin
+ /b exch def
+ {
+ /b b quicksort def
+ /b
+ b apop.right /f exch def
+ apop.right /s exch def
+ def
+ f s gt {
+ /b b f s sub apush.right def
+ } if
+ b length 0 eq {
+ 0
+ exit
+ } if
+ b length 1 eq {
+ b 0 get
+ exit
+ } if
+ } loop
+ end
+} bind def
+
+(lastelement) test.start
+[3 8 5 2 9 2] lastelement 1 eq test
+[3 2 5] lastelement 0 eq test
+test.end
diff --git a/challenge-306/roger-bell-west/python/ch-1.py b/challenge-306/roger-bell-west/python/ch-1.py
new file mode 100755
index 0000000000..77fabf743a
--- /dev/null
+++ b/challenge-306/roger-bell-west/python/ch-1.py
@@ -0,0 +1,36 @@
+#! /usr/bin/python3
+
+import collections
+from itertools import islice
+
+# https://docs.python.org/3/library/itertools.html
+def sliding_window(iterable, n):
+ # sliding_window('ABCDEFG', 4) --> ABCD BCDE CDEF DEFG
+ it = iter(iterable)
+ window = collections.deque(islice(it, n), maxlen=n)
+ if len(window) == n:
+ yield tuple(window)
+ for x in it:
+ window.append(x)
+ yield tuple(window)
+
+def oddsum(a):
+ out = sum(a)
+ l = 3
+ while l <= len(a):
+ for s in sliding_window(a, l):
+ out += sum(s)
+ l += 2
+ return out
+
+import unittest
+
+class TestOddsum(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(oddsum([2, 5, 3, 6, 4]), 77, 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(oddsum([1, 3]), 4, 'example 2')
+
+unittest.main()
diff --git a/challenge-306/roger-bell-west/python/ch-2.py b/challenge-306/roger-bell-west/python/ch-2.py
new file mode 100755
index 0000000000..d16bb157a1
--- /dev/null
+++ b/challenge-306/roger-bell-west/python/ch-2.py
@@ -0,0 +1,26 @@
+#! /usr/bin/python3
+
+def lastelement(a):
+ b = a
+ while True:
+ b.sort()
+ f = b.pop()
+ s = b.pop()
+ if f > s:
+ b.append(f - s)
+ if len(b) == 0:
+ return 0
+ if len(b) == 1:
+ return b[0]
+
+import unittest
+
+class TestLastelement(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(lastelement([3, 8, 5, 2, 9, 2]), 1, 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(lastelement([3, 2, 5]), 0, 'example 2')
+
+unittest.main()
diff --git a/challenge-306/roger-bell-west/raku/ch-1.p6 b/challenge-306/roger-bell-west/raku/ch-1.p6
new file mode 100755
index 0000000000..17125cf99e
--- /dev/null
+++ b/challenge-306/roger-bell-west/raku/ch-1.p6
@@ -0,0 +1,20 @@
+#! /usr/bin/raku
+
+use Test;
+
+plan 2;
+
+is(oddsum([2, 5, 3, 6, 4]), 77, 'example 1');
+is(oddsum([1, 3]), 4, 'example 2');
+
+sub oddsum(@a) {
+ my $out = @a.sum;
+ my $l = 3;
+ while ($l <= @a.elems) {
+ for @a.rotor($l => -($l - 1)) -> @s {
+ $out += @s.sum;
+ }
+ $l += 2;
+ }
+ $out;
+}
diff --git a/challenge-306/roger-bell-west/raku/ch-2.p6 b/challenge-306/roger-bell-west/raku/ch-2.p6
new file mode 100755
index 0000000000..220cce8e2d
--- /dev/null
+++ b/challenge-306/roger-bell-west/raku/ch-2.p6
@@ -0,0 +1,26 @@
+#! /usr/bin/raku
+
+use Test;
+
+plan 2;
+
+is(lastelement([3, 8, 5, 2, 9, 2]), 1, 'example 1');
+is(lastelement([3, 2, 5]), 0, 'example 2');
+
+sub lastelement(@a) {
+ my @b = @a;
+ loop {
+ @b = @b.sort({$^a <=> $^b});
+ my $f = @b.pop;
+ my $s = @b.pop;
+ if ($f > $s) {
+ @b.push($f - $s);
+ }
+ if (@b.elems == 0) {
+ return 0;
+ }
+ if (@b.elems == 1) {
+ return @b[0];
+ }
+ }
+}
diff --git a/challenge-306/roger-bell-west/ruby/ch-1.rb b/challenge-306/roger-bell-west/ruby/ch-1.rb
new file mode 100755
index 0000000000..6fd8414f3f
--- /dev/null
+++ b/challenge-306/roger-bell-west/ruby/ch-1.rb
@@ -0,0 +1,27 @@
+#! /usr/bin/ruby
+
+def oddsum(a)
+ out = a.sum
+ l = 3
+ while l <= a.length do
+ a.each_cons(l) do |s|
+ out += s.sum
+ end
+ l += 2
+ end
+ out
+end
+
+require 'test/unit'
+
+class TestOddsum < Test::Unit::TestCase
+
+ def test_ex1
+ assert_equal(77, oddsum([2, 5, 3, 6, 4]))
+ end
+
+ def test_ex2
+ assert_equal(4, oddsum([1, 3]))
+ end
+
+end
diff --git a/challenge-306/roger-bell-west/ruby/ch-2.rb b/challenge-306/roger-bell-west/ruby/ch-2.rb
new file mode 100755
index 0000000000..be6acca74d
--- /dev/null
+++ b/challenge-306/roger-bell-west/ruby/ch-2.rb
@@ -0,0 +1,33 @@
+#! /usr/bin/ruby
+
+def lastelement(a)
+ b = a
+ while true do
+ b.sort!
+ f = b.pop
+ s = b.pop
+ if f > s
+ b.push(f - s)
+ end
+ if b.length == 0
+ return 0
+ end
+ if b.length == 1
+ return b[0]
+ end
+ end
+end
+
+require 'test/unit'
+
+class TestLastelement < Test::Unit::TestCase
+
+ def test_ex1
+ assert_equal(1, lastelement([3, 8, 5, 2, 9, 2]))
+ end
+
+ def test_ex2
+ assert_equal(0, lastelement([3, 2, 5]))
+ end
+
+end
diff --git a/challenge-306/roger-bell-west/rust/ch-1.rs b/challenge-306/roger-bell-west/rust/ch-1.rs
new file mode 100755
index 0000000000..b5de3195de
--- /dev/null
+++ b/challenge-306/roger-bell-west/rust/ch-1.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!(oddsum(vec![2, 5, 3, 6, 4]), 77);
+}
+
+#[test]
+fn test_ex2() {
+ assert_eq!(oddsum(vec![1, 3]), 4);
+}
+
+fn oddsum(a: Vec<u32>) -> u32 {
+ let mut out: u32 = a.iter().sum();
+ let mut l = 3;
+ while l <= a.len() {
+ for s in a.windows(l) {
+ out += s.into_iter().sum::<u32>();
+ }
+ l += 2;
+ }
+ out
+}
+
diff --git a/challenge-306/roger-bell-west/rust/ch-2.rs b/challenge-306/roger-bell-west/rust/ch-2.rs
new file mode 100755
index 0000000000..db6bf31275
--- /dev/null
+++ b/challenge-306/roger-bell-west/rust/ch-2.rs
@@ -0,0 +1,30 @@
+#! /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!(lastelement(vec![3, 8, 5, 2, 9, 2]), 1);
+}
+
+#[test]
+fn test_ex2() {
+ assert_eq!(lastelement(vec![3, 2, 5]), 0);
+}
+
+fn lastelement(a: Vec<u32>) -> u32 {
+ let mut b = a.clone();
+ loop {
+ b.sort();
+ let f = b.pop().unwrap();
+ let s = b.pop().unwrap();
+ if f > s {
+ b.push(f - s);
+ }
+ if b.len() == 0 {
+ return 0;
+ }
+ if b.len() == 1 {
+ return b[0];
+ }
+ }
+}
diff --git a/challenge-306/roger-bell-west/scala/ch-1.scala b/challenge-306/roger-bell-west/scala/ch-1.scala
new file mode 100644
index 0000000000..ac444ca47b
--- /dev/null
+++ b/challenge-306/roger-bell-west/scala/ch-1.scala
@@ -0,0 +1,29 @@
+
+object Oddsum {
+ def oddsum(a: List[Int]): Int = {
+ var out = a.sum
+ var l = 3
+ while (l <= a.size) {
+ for (s <- a.sliding(l)) {
+ out += s.sum
+ }
+ l += 2
+ }
+ out
+ }
+ def main(args: Array[String]) {
+ if (oddsum(List(2, 5, 3, 6, 4)) == 77) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ print(" ")
+ if (oddsum(List(1, 3)) == 4) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ println("")
+
+ }
+}
diff --git a/challenge-306/roger-bell-west/scala/ch-2.scala b/challenge-306/roger-bell-west/scala/ch-2.scala
new file mode 100644
index 0000000000..674d87facb
--- /dev/null
+++ b/challenge-306/roger-bell-west/scala/ch-2.scala
@@ -0,0 +1,42 @@
+import scala.collection.mutable.ListBuffer
+
+object Lastelement {
+ def lastelement(a: List[Int]): Int = {
+ var b = a.to[ListBuffer]
+ var ct = true
+ var ex = -1
+ while (ct) {
+ b = b.sortWith(_ < _)
+ val f = b.last
+ b = b.dropRight(1)
+ val s = b.last
+ b = b.dropRight(1)
+ if (f > s) {
+ b += (f - s)
+ }
+ if (b.size == 0) {
+ ct = false
+ ex = 0
+ }
+ if (b.size == 1) {
+ ct = false
+ ex = b(0)
+ }
+ }
+ ex
+ }
+ def main(args: Array[String]) {
+ if (lastelement(List(3, 8, 5, 2, 9, 2)) == 1) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ print(" ")
+ if (lastelement(List(3, 2, 5)) == 0) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ println("")
+ }
+}
diff --git a/challenge-306/roger-bell-west/tests.json b/challenge-306/roger-bell-west/tests.json
new file mode 100644
index 0000000000..65f1dfc651
--- /dev/null
+++ b/challenge-306/roger-bell-west/tests.json
@@ -0,0 +1,24 @@
+{
+ "ch-1" : [
+ {
+ "function" : "oddsum",
+ "arguments" : [ 2, 5, 3, 6, 4 ],
+ "result" : 77
+ },
+ {
+ "arguments" : [ 1, 3 ],
+ "result" : 4
+ }
+ ],
+ "ch-2" : [
+ {
+ "function" : "lastelement",
+ "arguments" : [ 3, 8, 5, 2, 9, 2 ],
+ "result" : 1
+ },
+ {
+ "arguments" : [ 3, 2, 5 ],
+ "result" : 0
+ }
+ ]
+}