From 22437e2fa18fda4713254cc11efdc21f4ece082a Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Thu, 6 Apr 2023 11:36:26 +0100 Subject: RogerBW solutions for challenge no. 211 --- challenge-211/roger-bell-west/javascript/ch-1.js | 44 +++++++++ challenge-211/roger-bell-west/javascript/ch-2.js | 69 ++++++++++++++ challenge-211/roger-bell-west/kotlin/ch-1.kt | 44 +++++++++ challenge-211/roger-bell-west/kotlin/ch-2.kt | 69 ++++++++++++++ challenge-211/roger-bell-west/lua/ch-1.lua | 46 +++++++++ challenge-211/roger-bell-west/lua/ch-2.lua | 78 ++++++++++++++++ challenge-211/roger-bell-west/perl/ch-1.pl | 38 ++++++++ challenge-211/roger-bell-west/perl/ch-2.pl | 35 +++++++ challenge-211/roger-bell-west/postscript/ch-1.ps | 77 +++++++++++++++ challenge-211/roger-bell-west/postscript/ch-2.ps | 113 +++++++++++++++++++++++ challenge-211/roger-bell-west/python/ch-1.py | 34 +++++++ challenge-211/roger-bell-west/python/ch-2.py | 32 +++++++ challenge-211/roger-bell-west/raku/ch-1.p6 | 36 ++++++++ challenge-211/roger-bell-west/raku/ch-2.p6 | 29 ++++++ challenge-211/roger-bell-west/ruby/ch-1.rb | 43 +++++++++ challenge-211/roger-bell-west/ruby/ch-2.rb | 39 ++++++++ challenge-211/roger-bell-west/rust/ch-1.rs | 42 +++++++++ challenge-211/roger-bell-west/rust/ch-2.rs | 39 ++++++++ challenge-211/roger-bell-west/tests.yaml | 49 ++++++++++ 19 files changed, 956 insertions(+) create mode 100755 challenge-211/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-211/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-211/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-211/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-211/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-211/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-211/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-211/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-211/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-211/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-211/roger-bell-west/python/ch-1.py create mode 100755 challenge-211/roger-bell-west/python/ch-2.py create mode 100755 challenge-211/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-211/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-211/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-211/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-211/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-211/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-211/roger-bell-west/tests.yaml (limited to 'challenge-211') diff --git a/challenge-211/roger-bell-west/javascript/ch-1.js b/challenge-211/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..c400a681b4 --- /dev/null +++ b/challenge-211/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,44 @@ +#! /usr/bin/node + +"use strict" + +function toeplitzmatrix(a) { + let ym = a.length - 1; + let xm = a[0].length - 1; + let toeplitz = true; + for (let xb = 1 - xm; xb <= ym - 1; xb++) { + let init = true; + let tv = 0; + for (let x = xb; x <= xb + xm; x++) { + if (x >= 0 && x <= xm) { + let y = x - xb; + if (y >= 0 && y <= ym) { + if (init) { + init = false; + tv = a[y][x]; + } else if (a[y][x] != tv) { + toeplitz = false; + break; + } + } + } + } + if (!toeplitz) { + break; + } + } + return toeplitz +} + +if (toeplitzmatrix([[4, 3, 2, 1], [5, 4, 3, 2], [6, 5, 4, 3]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!toeplitzmatrix([[1, 2, 3], [3, 2, 1]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-211/roger-bell-west/javascript/ch-2.js b/challenge-211/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..75870a79b1 --- /dev/null +++ b/challenge-211/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,69 @@ +#! /usr/bin/node + +"use strict" + +function combinations(arr, k) { + let c = []; + for (let i = 0; i < k; i++) { + c.push(i); + } + c.push(arr.length); + c.push(0); + let out = []; + while (true) { + let inner = []; + for (let i = k-1; i >= 0; i--) { + inner.push(arr[c[i]]); + } + out.push(inner); + let j = 0; + while (c[j] + 1 == c[j + 1]) { + c[j] = j; + j += 1; + } + if (j >= k) { + break; + } + c[j] += 1; + } + return out; +} + +function splitsameaverage(a) { + let ss = a.reduce((x, y) => x + y, 0); + let ml = a.length; + let mx = Math.floor(ml / 2); + let ssa = false; + for (let n = 1; n <= mx; n++) { + for (let c of combinations(a, n)) { + let ca = c.reduce((x, y) => x + y, 0); + if (ca / n == (ss - ca) / (ml - n)) { + ssa = true; + break; + } + } + if (ssa) { + break; + } + } + return ssa; +} + +if (splitsameaverage([1, 2, 3, 4, 5, 6, 7, 8])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!splitsameaverage([1, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (splitsameaverage([1, 2, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-211/roger-bell-west/kotlin/ch-1.kt b/challenge-211/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..96d2c4f559 --- /dev/null +++ b/challenge-211/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,44 @@ +fun toeplitzmatrix(a: List>): Boolean { + val ym = a.size - 1 + val xm = a[0].size - 1 + var toeplitz = true + for (xb in 1 - xm .. ym - 1) { + var init = true + var tv = 0 + for (x in xb .. xb + xm) { + if (x >= 0 && x <= xm) { + val y = x - xb + if (y >= 0 && y <= ym) { + if (init) { + init = false + tv = a[y][x] + } else if (a[y][x] != tv) { + toeplitz = false + break + } + } + } + } + if (!toeplitz) { + break + } + } + return toeplitz +} + +fun main() { + + if (toeplitzmatrix(listOf(listOf(4, 3, 2, 1), listOf(5, 4, 3, 2), listOf(6, 5, 4, 3)))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!toeplitzmatrix(listOf(listOf(1, 2, 3), listOf(3, 2, 1)))) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-211/roger-bell-west/kotlin/ch-2.kt b/challenge-211/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..9145cbe67f --- /dev/null +++ b/challenge-211/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,69 @@ +fun combinations(arr: List, k: Int): List> { + var c = ArrayList() + for (i in 0 .. k-1) { + c.add(i) + } + c.add(arr.size) + c.add(0) + var out = ArrayList>() + while (true) { + var inner = ArrayList() + for (i in k-1 downTo 0) { + inner.add(arr[c[i]]) + } + out.add(inner.toList()) + var j = 0 + while (c[j] + 1 == c[j + 1]) { + c[j] = j + j += 1 + } + if (j >= k) { + break + } + c[j] += 1 + } + return out.toList(); +} + +fun splitsameaverage(a: List): Boolean { + val ss = a.sum() + val ml = a.size + val mx = ml / 2 + var ssa = false + for (n in 1 .. mx) { + for (c in combinations(a, n)) { + var ca = c.sum() + if (ca.toFloat() / n.toFloat() == (ss - ca).toFloat() / (ml - n).toFloat()) { + ssa = true + break + } + } + if (ssa) { + break + } + } + return ssa +} + +fun main() { + + if (splitsameaverage(listOf(1, 2, 3, 4, 5, 6, 7, 8))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!splitsameaverage(listOf(1, 3))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (splitsameaverage(listOf(1, 2, 3))) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-211/roger-bell-west/lua/ch-1.lua b/challenge-211/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..50dfb58b1c --- /dev/null +++ b/challenge-211/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,46 @@ +#! /usr/bin/lua + +function toeplitzmatrix(a) + local ym = #a - 1 + local xm = #(a[1]) - 1 + local toeplitz = true + for xb = (1 - xm), (ym - 1) do + local init = true + local tv = 0 + for xi = xb, xb + xm do + if xi >= 0 and xi <= xm then + local x = xi + 1 + local yi = xi - xb + if yi >= 0 and yi <= ym then + local y = yi + 1 + if init then + init = false + tv = a[y][x] + elseif a[y][x] ~= tv then + toeplitz = false + break + end + end + end + end + if not toeplitz then + break + end + end + return toeplitz +end + +if toeplitzmatrix({{4, 3, 2, 1}, {5, 4, 3, 2}, {6, 5, 4, 3}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not toeplitzmatrix({{1, 2, 3}, {3, 2, 1}}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-211/roger-bell-west/lua/ch-2.lua b/challenge-211/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..c524e76c6d --- /dev/null +++ b/challenge-211/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,78 @@ +#! /usr/bin/lua + +function combinations(arr, k) + local c = {} + for i = 1, k do + table.insert(c, i) + end + table.insert(c, #arr + 1) + table.insert(c, 0) + local out = {} + while true do + local inner = {} + for i = k, 1, -1 do + table.insert(inner, arr[c[i]]) + end + table.insert(out, inner) + local j = 1 + while c[j] + 1 == c[j + 1] do + c[j] = j + j = j + 1 + end + if j > k then + break + end + c[j] = c[j] + 1 + end + return ipairs(out) +end + +function sum(t) + local ss = 0 + for i, k in ipairs(t) do + ss = ss + k + end + return ss +end + +function splitsameaverage(a) + local ss = sum(a) + local ml = #a + local mx = ml // 2 + local ssa = false + for n = 1, mx do + for i, c in combinations(a, n) do + local ca = sum(c) + if (ca / n) == (ss - ca) / (ml - n) then + ssa = true + break + end + end + if ssa then + break + end + end + return ssa +end + +if splitsameaverage({1, 2, 3, 4, 5, 6, 7, 8}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not splitsameaverage({1, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if splitsameaverage({1, 2, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-211/roger-bell-west/perl/ch-1.pl b/challenge-211/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..f8513ae5b0 --- /dev/null +++ b/challenge-211/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,38 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(toeplitzmatrix([[4, 3, 2, 1], [5, 4, 3, 2], [6, 5, 4, 3]]), 1, 'example 1'); +is(toeplitzmatrix([[1, 2, 3], [3, 2, 1]]), 0, 'example 2'); + +sub toeplitzmatrix($a) { + my $ym = $#{$a}; + my $xm = $#{$a->[0]}; + my $toeplitz = 1; + foreach my $xb ((1 - $xm)..($ym - 1)) { + my $init = 1; + my $tv = 0; + foreach my $x ($xb .. $xb + $xm) { + if ($x >= 0 && $x <= $xm) { + my $y = $x - $xb; + if ($y >= 0 && $y <= $ym) { + if ($init) { + $init = 0; + $tv = $a->[$y][$x]; + } elsif ($a->[$y][$x] != $tv) { + $toeplitz = 0; + last; + } + } + } + } + unless ($toeplitz) { + last; + } + } + return $toeplitz; +} diff --git a/challenge-211/roger-bell-west/perl/ch-2.pl b/challenge-211/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..3956df018c --- /dev/null +++ b/challenge-211/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,35 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(splitsameaverage([1, 2, 3, 4, 5, 6, 7, 8]), 1, 'example 1'); +is(splitsameaverage([1, 3]), 0, 'example 2'); +is(splitsameaverage([1, 2, 3]), 1, 'example 3'); + +use Algorithm::Combinatorics qw(combinations); +use List::Util qw(sum); + +sub splitsameaverage($a) { + my $ss = sum(@{$a}); + my $ml = scalar @{$a}; + my $mx = int($ml / 2); + my $ssa = 0; + foreach my $n (1 .. $mx) { + my $ic = combinations($a, $n); + while (my $c = $ic->next) { + my $ca = sum(@{$c}); + if ($ca / $n == ($ss - $ca) / ($ml - $n)) { + $ssa = 1; + last; + } + } + if ($ssa) { + last; + } + } + return $ssa; +} diff --git a/challenge-211/roger-bell-west/postscript/ch-1.ps b/challenge-211/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..19711a69dd --- /dev/null +++ b/challenge-211/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,77 @@ +%!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.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 + + +% end included library code + +/toeplitzmatrix { + 9 dict begin + /a exch def + /ym a length 1 sub def + /xm a 0 get length 1 sub def + /toeplitz true def + 1 xm sub 1 ym 1 sub { + /xb exch def + /init true def + /tv 0 def + xb 1 xb xm add { + /x exch def + x 0 ge x xm le and { + /y x xb sub def + y 0 ge y ym le and { + init { + /init false def + /tv a y get x get def + } { + a y get x get tv ne { + /toeplitz false def + exit + } if + } ifelse + } if + } if + } for + toeplitz not { + exit + } if + } for + toeplitz + end +} bind def + +(toeplitzmatrix) test.start +[[4 3 2 1] [5 4 3 2] [6 5 4 3]] toeplitzmatrix test +[[1 2 3] [3 2 1]] toeplitzmatrix not test +test.end diff --git a/challenge-211/roger-bell-west/postscript/ch-2.ps b/challenge-211/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..731387160e --- /dev/null +++ b/challenge-211/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,113 @@ +%!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 + +/combinations { + 4 dict begin + /k exch def + /arr exch def + /c [ + 0 1 k 1 sub { } for + arr length + 0 + ] def + [ + { + [ + k 1 sub -1 0 { + c exch get arr exch get + } for + ] + /j 0 def + { + c j get 1 add c j 1 add get ne { + exit + } if + c j j put + /j j 1 add def + } loop + j k ge { + exit + } if + c j c j get 1 add put + } loop + ] + end +} bind def + +/reduce { % array proc -> value + 2 dict begin + /p exch def + /a exch def + a 0 get + 1 1 a length 1 sub { + a exch get + p + } for + end +} bind def + + +% end included library code + +/splitsameaverage { + 6 dict begin + /a exch def + /ss a { add } reduce def + /ml a length def + /mx ml 2 idiv def + /ssa false def + 1 1 mx { + /n exch def + a n combinations { + { add } reduce /ca exch def + ca cvr n cvr div + ss ca sub cvr ml n sub cvr div eq { + /ssa true def + exit + } if + } forall + ssa { + exit + } if + } for + ssa + end +} bind def + +(splitsameaverage) test.start +[1 2 3 4 5 6 7 8] splitsameaverage test +[1 3] splitsameaverage not test +[1 2 3] splitsameaverage test +test.end diff --git a/challenge-211/roger-bell-west/python/ch-1.py b/challenge-211/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..a1c6e0e380 --- /dev/null +++ b/challenge-211/roger-bell-west/python/ch-1.py @@ -0,0 +1,34 @@ +#! /usr/bin/python3 + +import unittest + +def toeplitzmatrix(a): + ym = len(a) - 1 + xm = len(a[0]) - 1 + toeplitz = True + for xb in range(1 - xm, ym): + init = True + tv = 0 + for x in range(xb, xb + xm + 1): + if x >= 0 and x <= xm: + y = x - xb + if y >= 0 and y <= ym: + if init: + init = False + tv = a[y][x] + elif a[y][x] != tv: + toeplitz = False + break + if not toeplitz: + break + return toeplitz + +class TestToeplitzmatrix(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(toeplitzmatrix([[4, 3, 2, 1], [5, 4, 3, 2], [6, 5, 4, 3]]), True, 'example 1') + + def test_ex2(self): + self.assertEqual(toeplitzmatrix([[1, 2, 3], [3, 2, 1]]), False, 'example 2') + +unittest.main() diff --git a/challenge-211/roger-bell-west/python/ch-2.py b/challenge-211/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..7a4f8fcc6d --- /dev/null +++ b/challenge-211/roger-bell-west/python/ch-2.py @@ -0,0 +1,32 @@ +#! /usr/bin/python3 + +import unittest +from itertools import combinations + +def splitsameaverage(a): + ss = sum(a) + ml = len(a) + mx = int(ml / 2) + ssa = False + for n in range(1, mx + 1): + for c in combinations(a, n): + ca = sum(c) + if (float(ca) / float(n) == float(ss - ca) / float(ml - n)): + ssa = True + break + if ssa: + break + return ssa + +class TestSplitsameaverage(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(splitsameaverage([1, 2, 3, 4, 5, 6, 7, 8]), True, 'example 1') + + def test_ex2(self): + self.assertEqual(splitsameaverage([1, 3]), False, 'example 2') + + def test_ex3(self): + self.assertEqual(splitsameaverage([1, 2, 3]), True, 'example 3') + +unittest.main() diff --git a/challenge-211/roger-bell-west/raku/ch-1.p6 b/challenge-211/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..27c7581f2b --- /dev/null +++ b/challenge-211/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,36 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(toeplitzmatrix([[4, 3, 2, 1], [5, 4, 3, 2], [6, 5, 4, 3]]), True, 'example 1'); +is(toeplitzmatrix([[1, 2, 3], [3, 2, 1]]), False, 'example 2'); + +sub toeplitzmatrix(@a) { + my $ym = @a.end; + my $xm = @a[0].end; + my $toeplitz = True; + for (1 - $xm)..($ym - 1) -> $xb { + my $init = True; + my $tv = 0; + for $xb .. $xb + $xm -> $x { + if ($x >= 0 && $x <= $xm) { + my $y = $x - $xb; + if ($y >= 0 && $y <= $ym) { + if ($init) { + $init = False; + $tv = @a[$y][$x]; + } elsif (@a[$y][$x] != $tv) { + $toeplitz = False; + last; + } + } + } + } + unless ($toeplitz) { + last; + } + } + return $toeplitz; +} diff --git a/challenge-211/roger-bell-west/raku/ch-2.p6 b/challenge-211/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..9e99ecdf00 --- /dev/null +++ b/challenge-211/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,29 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(splitsameaverage([1, 2, 3, 4, 5, 6, 7, 8]), True, 'example 1'); +is(splitsameaverage([1, 3]), False, 'example 2'); +is(splitsameaverage([1, 2, 3]), True, 'example 3'); + +sub splitsameaverage(@a) { + my $ss = @a.sum; + my $ml = @a.elems; + my $mx = floor($ml / 2); + my $ssa = False; + for 1 .. $mx -> $n { + for @a.combinations($n) -> @c { + my $ca = @c.sum; + if (Rat.new($ca, $n) == Rat.new($ss - $ca, $ml - $n)) { + $ssa = True; + last; + } + } + if ($ssa) { + last; + } + } + return $ssa; +} diff --git a/challenge-211/roger-bell-west/ruby/ch-1.rb b/challenge-211/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..3bc47dae17 --- /dev/null +++ b/challenge-211/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,43 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def toeplitzmatrix(a) + ym = a.length - 1 + xm = a[0].length - 1 + toeplitz = true + (-xm + 1).upto(ym - 1) do |xb| + init = true + tv = 0 + xb.upto(xb + xm) do |x| + if x >= 0 && x <= xm then + y = x - xb + if y >= 0 && y <= ym then + if init then + init = false + tv = a[y][x] + elsif a[y][x] != tv then + toeplitz = false + break + end + end + end + end + if !toeplitz then + break + end + end + return toeplitz +end + +class TestToeplitzmatrix < Test::Unit::TestCase + + def test_ex1 + assert_equal(true, toeplitzmatrix([[4, 3, 2, 1], [5, 4, 3, 2], [6, 5, 4, 3]])) + end + + def test_ex2 + assert_equal(false, toeplitzmatrix([[1, 2, 3], [3, 2, 1]])) + end + +end diff --git a/challenge-211/roger-bell-west/ruby/ch-2.rb b/challenge-211/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..04e730f38d --- /dev/null +++ b/challenge-211/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,39 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def splitsameaverage(a) + ss = a.sum + ml = a.length + mx = ml/2.to_i + ssa = false + 1.upto(mx) do |n| + a.combination(n) do |c| + ca = c.sum + if (ca.to_f / n.to_f == (ss - ca).to_f / (ml - n).to_f) then + ssa = true + break + end + end + if ssa then + break + end + end + return ssa +end + +class TestSplitsameaverage < Test::Unit::TestCase + + def test_ex1 + assert_equal(true, splitsameaverage([1, 2, 3, 4, 5, 6, 7, 8])) + end + + def test_ex2 + assert_equal(false, splitsameaverage([1, 3])) + end + + def test_ex3 + assert_equal(true, splitsameaverage([1, 2, 3])) + end + +end diff --git a/challenge-211/roger-bell-west/rust/ch-1.rs b/challenge-211/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..9a386a9e6a --- /dev/null +++ b/challenge-211/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,42 @@ +#! /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!(toeplitzmatrix(vec![vec![4, 3, 2, 1], vec![5, 4, 3, 2], vec![6, 5, 4, 3]]), true); +} + +#[test] +fn test_ex2() { + assert_eq!(toeplitzmatrix(vec![vec![1, 2, 3], vec![3, 2, 1]]), false); +} + +fn toeplitzmatrix(a: Vec>) -> bool { + let ym = (a.len() - 1) as isize; + let xm = (a[0].len() - 1) as isize; + let mut toeplitz = true; + for xb in (1 - xm) ..= (ym - 1) { + let mut init = true; + let mut tv = 0; + for xi in xb ..= (xb + xm) { + if xi >= 0 && xi <= xm { + let x = xi as usize; + let yi = xi - xb; + if yi >= 0 && yi <= ym { + let y = yi as usize; + if init { + init = false; + tv = a[y][x]; + } else if a[y][x] != tv { + toeplitz = false; + break; + } + } + } + } + if !toeplitz { + break; + } + } + toeplitz +} diff --git a/challenge-211/roger-bell-west/rust/ch-2.rs b/challenge-211/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..5b000689ba --- /dev/null +++ b/challenge-211/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,39 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +use itertools::Itertools; + +#[test] +fn test_ex1() { + assert_eq!(splitsameaverage(vec![1, 2, 3, 4, 5, 6, 7, 8]), true); +} + +#[test] +fn test_ex2() { + assert_eq!(splitsameaverage(vec![1, 3]), false); +} + +#[test] +fn test_ex3() { + assert_eq!(splitsameaverage(vec![1, 2, 3]), true); +} + +fn splitsameaverage(a: Vec) -> bool { + let ss = a.iter().sum::(); + let ml = a.len(); + let mx = ml / 2; + let mut ssa = false; + for n in 1 ..= mx { + for c in a.iter().combinations(n) { + let ca = c.iter().map(|i| *i).sum::(); + if (ca as f64) / (n as f64) == ((ss - ca) as f64) / ((ml - n) as f64) { + ssa = true; + break; + } + } + if ssa { + break; + } + } + ssa +} diff --git a/challenge-211/roger-bell-west/tests.yaml b/challenge-211/roger-bell-west/tests.yaml new file mode 100644 index 0000000000..7370c9a866 --- /dev/null +++ b/challenge-211/roger-bell-west/tests.yaml @@ -0,0 +1,49 @@ +--- +ch-1: + - function: toeplitzmatrix + arguments: + - - 4 + - 3 + - 2 + - 1 + - - 5 + - 4 + - 3 + - 2 + - - 6 + - 5 + - 4 + - 3 + result: true + - function: toeplitzmatrix + arguments: + - - 1 + - 2 + - 3 + - - 3 + - 2 + - 1 + result: false +ch-2: + - function: splitsameaverage + arguments: + - 1 + - 2 + - 3 + - 4 + - 5 + - 6 + - 7 + - 8 + result: true + - function: splitsameaverage + arguments: + - 1 + - 3 + result: false + - function: splitsameaverage + arguments: + - 1 + - 2 + - 3 + result: true -- cgit