diff options
19 files changed, 668 insertions, 0 deletions
diff --git a/challenge-215/roger-bell-west/javascript/ch-1.js b/challenge-215/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..60cddea93c --- /dev/null +++ b/challenge-215/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,34 @@ +#! /usr/bin/node + +"use strict" + +function oddoneout(a) { + let ct = 0; + for (let s of a) { + let p = s.split(""); + p.sort(); + if (p.join("") != s) { + ct += 1; + } + } + return ct; +} + +if (oddoneout(['abc', 'xyz', 'tsu']) == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (oddoneout(['rat', 'cab', 'dad']) == 3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (oddoneout(['x', 'y', 'z']) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-215/roger-bell-west/javascript/ch-2.js b/challenge-215/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..9190bec593 --- /dev/null +++ b/challenge-215/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,38 @@ +#! /usr/bin/node + +"use strict" + +function numberplacement(a0, ct) { + let a = [1]; + a.push(...a0); + a.push(1) + let s = 0; + let tt = 0; + for (let i = 1; i < a.length; i++) { + if (a[i - 1] == 1 && a[i] == 0) { + s = i; + } else if (a[i - 1] == 0 && a[i] == 1) { + tt += Math.floor((i - s) / 2); + } + } + return ct <= tt; +} + +if (numberplacement([1, 0, 0, 0, 1], 1)) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!numberplacement([1, 0, 0, 0, 1], 2)) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (numberplacement([1, 0, 0, 0, 0, 0, 0, 0, 1], 3)) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-215/roger-bell-west/kotlin/ch-1.kt b/challenge-215/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..0e5243dd92 --- /dev/null +++ b/challenge-215/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,34 @@ +fun oddoneout(a: List<String>): Int { + var ct = 0 + for (s in a) { + var p = s.toCharArray() + p.sort() + if (p.joinToString("") != s) { + ct += 1 + } + } + return ct +} + +fun main() { + + if (oddoneout(listOf("abc", "xyz", "tsu")) == 1) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (oddoneout(listOf("rat", "cab", "dad")) == 3) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (oddoneout(listOf("x", "y", "z")) == 0) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-215/roger-bell-west/kotlin/ch-2.kt b/challenge-215/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..7de54fe7aa --- /dev/null +++ b/challenge-215/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,39 @@ +fun numberplacement(a0: List<Int>, ct: Int): Boolean { + var a = ArrayList<Int>() + a.add(1) + a.addAll(a0) + a.add(1) + var s = 0 + var tt = 0 + for (i in 1 .. a.size - 1) { + if (a[i - 1] == 1 && a[i] == 0) { + s = i + } else if (a[i - 1] == 0 && a[i] == 1) { + tt += (i - s) / 2 + } + } + return ct <= tt +} + +fun main() { + + if (numberplacement(listOf(1, 0, 0, 0, 1), 1)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!numberplacement(listOf(1, 0, 0, 0, 1), 2)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (numberplacement(listOf(1, 0, 0, 0, 0, 0, 0, 0, 1), 3)) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-215/roger-bell-west/lua/ch-1.lua b/challenge-215/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..eb0dbb2bdb --- /dev/null +++ b/challenge-215/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,36 @@ +#! /usr/bin/lua + +function oddoneout(a) + local ct = 0 + for _dummy, s in ipairs(a) do + for i = 2, #s do + if string.byte(s, i - 1) > string.byte(s, i) then + ct = ct + 1 + break + end + end + end + return ct +end + +if oddoneout({"abc", "xyz", "tsu"}) == 1 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if oddoneout({"rat", "cab", "dad"}) == 3 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if oddoneout({"x", "y", "z"}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-215/roger-bell-west/lua/ch-2.lua b/challenge-215/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..e8562a0286 --- /dev/null +++ b/challenge-215/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,39 @@ +#! /usr/bin/lua + +function numberplacement(a0, ct) + local a = a0 + table.insert(a, 1, 1) + table.insert(a, 1) + local s = 0 + local tt = 0 + for i = 2, #a do + if a[i - 1] == 1 and a[i] == 0 then + s = i + elseif a[i - 1] == 0 and a[i] == 1 then + tt = tt + (i - s) // 2 + end + end + return ct <= tt +end + +if numberplacement({1, 0, 0, 0, 1}, 1) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not numberplacement({1, 0, 0, 0, 1}, 2) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if numberplacement({1, 0, 0, 0, 0, 0, 0, 0, 1}, 3) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-215/roger-bell-west/perl/ch-1.pl b/challenge-215/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..afd202442a --- /dev/null +++ b/challenge-215/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,22 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(oddoneout(['abc', 'xyz', 'tsu']), 1, 'example 1'); +is(oddoneout(['rat', 'cab', 'dad']), 3, 'example 2'); +is(oddoneout(['x', 'y', 'z']), 0, 'example 3'); + +sub oddoneout($a) { + my $ct = 0; + foreach my $s (@{$a}) { + my $t = join('', sort split '',$s); + if ($s ne $t) { + $ct++; + } + } + return $ct; +} diff --git a/challenge-215/roger-bell-west/perl/ch-2.pl b/challenge-215/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..0400a956fc --- /dev/null +++ b/challenge-215/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,25 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(numberplacement([1, 0, 0, 0, 1], 1), 1, 'example 1'); +is(numberplacement([1, 0, 0, 0, 1], 2), 0, 'example 2'); +is(numberplacement([1, 0, 0, 0, 0, 0, 0, 0, 1], 3), 1, 'example 3'); + +sub numberplacement($a0, $ct) { + my @a = (1, @{$a0}, 1); + my $s = 0; + my $tt = 0; + foreach my $i (1..$#a) { + if ($a[$i - 1] == 1 && $a[$i] == 0) { + $s = $i; + } elsif ($a[$i - 1] == 0 && $a[$i] == 1) { + $tt += int(($i - $s)/2); + } + } + return ($ct <= $tt)?1:0; +} diff --git a/challenge-215/roger-bell-west/postscript/ch-1.ps b/challenge-215/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..7ba323fb7f --- /dev/null +++ b/challenge-215/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,60 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + + +% end included library code + +/oddoneout { + 3 dict begin + /ct 0 def + { + /s exch def + 1 1 s length 1 sub { + /i exch def + s i 1 sub get s i get gt { + /ct ct 1 add def + exit + } if + } for + } forall + ct + end +} bind def + +(oddoneout) test.start +[(abc) (xyz) (tsu)] oddoneout 1 eq test +[(rat) (cab) (dad)] oddoneout 3 eq test +[(x) (y) (z)] oddoneout 0 eq test +test.end diff --git a/challenge-215/roger-bell-west/postscript/ch-2.ps b/challenge-215/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..8b935fa340 --- /dev/null +++ b/challenge-215/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,65 @@ +%!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 + +/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.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + + +% end included library code + +/numberplacement { + 5 dict begin + /ct exch def + /a exch [ exch 1 exch + aload pop + 1 ] def + /s 0 def + /tt 0 def + 1 1 a length 1 sub { + /i exch def + a i 1 sub get 1 eq a i get 0 eq and { + /s i def + } { + a i 1 sub get 0 eq a i get 1 eq and { + /tt tt i s sub 2 idiv add def + } if + } ifelse + } for + ct tt le + end +} bind def + +(numberplacement) test.start +[1 0 0 0 1] 1 numberplacement test +[1 0 0 0 1] 2 numberplacement not test +[1 0 0 0 0 0 0 0 1] 3 numberplacement test +test.end diff --git a/challenge-215/roger-bell-west/python/ch-1.py b/challenge-215/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..e9786ee471 --- /dev/null +++ b/challenge-215/roger-bell-west/python/ch-1.py @@ -0,0 +1,25 @@ +#! /usr/bin/python3 + +def oddoneout(a): + ct = 0 + for s in a: + p = [i for i in s] + p.sort() + if ''.join(p) != s: + ct += 1 + return ct + +import unittest + +class TestOddoneout(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(oddoneout(["abc", "xyz", "tsu"]), 1, 'example 1') + + def test_ex2(self): + self.assertEqual(oddoneout(["rat", "cab", "dad"]), 3, 'example 2') + + def test_ex3(self): + self.assertEqual(oddoneout(["x", "y", "z"]), 0, 'example 3') + +unittest.main() diff --git a/challenge-215/roger-bell-west/python/ch-2.py b/challenge-215/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..0dca0440e5 --- /dev/null +++ b/challenge-215/roger-bell-west/python/ch-2.py @@ -0,0 +1,27 @@ +#! /usr/bin/python3 + +def numberplacement(a0, ct): + a = [1] + a0 + [1] + s = 0 + tt = 0 + for i in range(1, len(a)): + if a[i - 1] == 1 and a[i] == 0: + s = i + elif a[i - 1] == 0 and a[i] == 1: + tt += (i - s) // 2 + return ct <= tt + +import unittest + +class TestNumberplacement(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(numberplacement([1, 0, 0, 0, 1], 1), True, 'example 1') + + def test_ex2(self): + self.assertEqual(numberplacement([1, 0, 0, 0, 1], 2), False, 'example 2') + + def test_ex3(self): + self.assertEqual(numberplacement([1, 0, 0, 0, 0, 0, 0, 0, 1], 3), True, 'example 3') + +unittest.main() diff --git a/challenge-215/roger-bell-west/raku/ch-1.p6 b/challenge-215/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..8bf20f2fa1 --- /dev/null +++ b/challenge-215/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,20 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(oddoneout(['abc', 'xyz', 'tsu']), 1, 'example 1'); +is(oddoneout(['rat', 'cab', 'dad']), 3, 'example 2'); +is(oddoneout(['x', 'y', 'z']), 0, 'example 3'); + +sub oddoneout(@a) { + my $ct = 0; + for @a -> $s { + my $t = $s.comb.sort.join(''); + if ($s ne $t) { + $ct++; + } + } + return $ct; +} diff --git a/challenge-215/roger-bell-west/raku/ch-2.p6 b/challenge-215/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..57484d9814 --- /dev/null +++ b/challenge-215/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,25 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(numberplacement([1, 0, 0, 0, 1], 1), True, 'example 1'); +is(numberplacement([1, 0, 0, 0, 1], 2), False, 'example 2'); +is(numberplacement([1, 0, 0, 0, 0, 0, 0, 0, 1], 3), True, 'example 3'); + +sub numberplacement(@a0, $ct) { + my @a = (1, ); + @a.append(@a0); + @a.push(1); + my $s = 0; + my $tt = 0; + for (1..@a.end) -> $i { + if (@a[$i - 1] == 1 && @a[$i] == 0) { + $s = $i; + } elsif (@a[$i - 1] == 0 && @a[$i] == 1) { + $tt += floor(($i - $s)/2); + } + } + return $ct <= $tt; +} diff --git a/challenge-215/roger-bell-west/ruby/ch-1.rb b/challenge-215/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..e119695546 --- /dev/null +++ b/challenge-215/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,31 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def oddoneout(a) + ct = 0 + a.each do |s| + p = s.chars.to_a + p.sort! + if p.join != s then + ct += 1 + end + end + return ct +end + +class TestOddoneout < Test::Unit::TestCase + + def test_ex1 + assert_equal(1, oddoneout(['abc', 'xyz', 'tsu'])) + end + + def test_ex2 + assert_equal(3, oddoneout(['rat', 'cab', 'dad'])) + end + + def test_ex3 + assert_equal(0, oddoneout(['x', 'y', 'z'])) + end + +end diff --git a/challenge-215/roger-bell-west/ruby/ch-2.rb b/challenge-215/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..37f1ecc66a --- /dev/null +++ b/challenge-215/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,35 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def numberplacement(a0, ct) + a = a0 + a.unshift(1) + a.push(1) + s = 0 + tt = 0 + 1.upto(a.length) do |i| + if a[i - 1] == 1 && a[i] == 0 then + s = i + elsif a[i - 1] == 0 && a[i] == 1 then + tt += ((i - s) / 2).to_i + end + end + return ct <= tt +end + +class TestNumberplacement < Test::Unit::TestCase + + def test_ex1 + assert_equal(true, numberplacement([1, 0, 0, 0, 1], 1)) + end + + def test_ex2 + assert_equal(false, numberplacement([1, 0, 0, 0, 1], 2)) + end + + def test_ex3 + assert_equal(true, numberplacement([1, 0, 0, 0, 0, 0, 0, 0, 1], 3)) + end + +end diff --git a/challenge-215/roger-bell-west/rust/ch-1.rs b/challenge-215/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..db78bb646f --- /dev/null +++ b/challenge-215/roger-bell-west/rust/ch-1.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!(oddoneout(vec!["abc", "xyz", "tsu"]), 1); +} + +#[test] +fn test_ex2() { + assert_eq!(oddoneout(vec!["rat", "cab", "dad"]), 3); +} + +#[test] +fn test_ex3() { + assert_eq!(oddoneout(vec!["x", "y", "z"]), 0); +} + +fn oddoneout(a: Vec<&str>) -> u32 { + let mut ct = 0; + for s in a { + let mut p = s.to_string().chars().collect::<Vec<char>>(); + p.sort(); + let t = p.iter().collect::<String>(); + if s != t { + ct += 1; + } + } + ct +} diff --git a/challenge-215/roger-bell-west/rust/ch-2.rs b/challenge-215/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..4da1a4f7ce --- /dev/null +++ b/challenge-215/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,32 @@ +#! /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!(numberplacement(vec![1, 0, 0, 0, 1], 1), true); +} + +#[test] +fn test_ex2() { + assert_eq!(numberplacement(vec![1, 0, 0, 0, 1], 2), false); +} + +#[test] +fn test_ex3() { + assert_eq!(numberplacement(vec![1, 0, 0, 0, 0, 0, 0, 0, 1], 3), true); +} + +fn numberplacement(a0: Vec<u8>, ct: u32) -> bool { + let mut a: Vec<u8> = vec![1; a0.len() + 2]; + a.splice( 1 ..= a0.len(), a0); + let mut s = 0; + let mut tt = 0; + for i in 1 .. a.len() { + match (a[i - 1], a[i]) { + (1, 0) => { s = i; }, + (0, 1) => { tt += (i - s) / 2; }, + _ => (), + } + } + ct <= tt as u32 +} diff --git a/challenge-215/roger-bell-west/tests.yaml b/challenge-215/roger-bell-west/tests.yaml new file mode 100644 index 0000000000..d4de83cca3 --- /dev/null +++ b/challenge-215/roger-bell-west/tests.yaml @@ -0,0 +1,51 @@ +--- +ch-1: + - function: oddoneout + arguments: + - abc + - xyz + - tsu + result: 1 + - arguments: + - rat + - cab + - dad + result: 3 + - arguments: + - x + - y + - z + result: 0 +ch-2: + - function: numberplacement + multiarg: true + arguments: + - - 1 + - 0 + - 0 + - 0 + - 1 + - 1 + result: true + - multiarg: true + arguments: + - - 1 + - 0 + - 0 + - 0 + - 1 + - 2 + result: false + - multiarg: true + arguments: + - - 1 + - 0 + - 0 + - 0 + - 0 + - 0 + - 0 + - 0 + - 1 + - 3 + result: true |
