diff options
21 files changed, 654 insertions, 0 deletions
diff --git a/challenge-262/roger-bell-west/javascript/ch-1.js b/challenge-262/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..13f6c38d2b --- /dev/null +++ b/challenge-262/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,29 @@ +#! /usr/bin/node + +"use strict" + +function maxpositivenumber(a) { + return Math.max( + a.filter ( c => c > 0 ).length, + a.filter ( c => c < 0 ).length + ); +} + +if (maxpositivenumber([-3, 1, 2, -1, 3, -2, 4]) == 4) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maxpositivenumber([-1, -2, -3, 1]) == 3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maxpositivenumber([1, 2]) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-262/roger-bell-west/javascript/ch-2.js b/challenge-262/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..90ee04344f --- /dev/null +++ b/challenge-262/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,28 @@ +#! /usr/bin/node + +"use strict" + +function countequaldivisible(a, k) { + let s = 0; + for (let i = 0; i < a.length - 1; i++) { + for (let j = i + 1; j < a.length; j++) { + if (a[i] == a[j] && i * j % k == 0) { + s++; + } + } + } + return s; +} + +if (countequaldivisible([3, 1, 2, 2, 2, 1, 3], 2) == 4) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (countequaldivisible([1, 2, 3], 1) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-262/roger-bell-west/kotlin/ch-1.kt b/challenge-262/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..fcaccdf0b3 --- /dev/null +++ b/challenge-262/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,29 @@ +fun maxpositivenumber(a: List<Int>): Int { + return listOf( + a.filter { c -> c > 0 }.size, + a.filter { c -> c < 0 }.size + ).maxOrNull()!! +} + +fun main() { + + if (maxpositivenumber(listOf(-3, 1, 2, -1, 3, -2, 4)) == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxpositivenumber(listOf(-1, -2, -3, 1)) == 3) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxpositivenumber(listOf(1, 2)) == 2) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-262/roger-bell-west/kotlin/ch-2.kt b/challenge-262/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..2aee859707 --- /dev/null +++ b/challenge-262/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,29 @@ + +fun countequaldivisible(a: List<Int>, k: Int): Int { + var s = 0 + for (i in 0 .. a.size - 2) { + for (j in i + 1 .. a.size - 1) { + if (a[i] == a[j] && i * j % k == 0) { + s += 1 + } + } + } + return s +} + +fun main() { + + if (countequaldivisible(listOf(3, 1, 2, 2, 2, 1, 3), 2) == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (countequaldivisible(listOf(1, 2, 3), 1) == 0) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-262/roger-bell-west/lua/ch-1.lua b/challenge-262/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..121ae0baaf --- /dev/null +++ b/challenge-262/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,36 @@ +#! /usr/bin/lua + +function maxpositivenumber(a) + local pos = 0 + local neg = 0 + for _, c in ipairs(a) do + if c > 0 then + pos = pos + 1 + elseif c < 0 then + neg = neg + 1 + end + end + return math.max(pos, neg) +end + +if maxpositivenumber({-3, 1, 2, -1, 3, -2, 4}) == 4 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maxpositivenumber({-1, -2, -3, 1}) == 3 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maxpositivenumber({1, 2}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-262/roger-bell-west/lua/ch-2.lua b/challenge-262/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..2ec0243d43 --- /dev/null +++ b/challenge-262/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,28 @@ +#! /usr/bin/lua + +function countequaldivisible(a, k) + local s = 0 + for i = 1, #a-1 do + for j = i + 1, #a do + if a[i] == a[j] and (i-1) * (j-1) % k == 0 then + s = s + 1 + end + end + end + return s +end + +if countequaldivisible({3, 1, 2, 2, 2, 1, 3}, 2) == 4 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if countequaldivisible({1, 2, 3}, 1) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-262/roger-bell-west/perl/ch-1.pl b/challenge-262/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..09d8f39621 --- /dev/null +++ b/challenge-262/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,20 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(maxpositivenumber([-3, 1, 2, -1, 3, -2, 4]), 4, 'example 1'); +is(maxpositivenumber([-1, -2, -3, 1]), 3, 'example 2'); +is(maxpositivenumber([1, 2]), 2, 'example 3'); + +use List::Util qw(max); + +sub maxpositivenumber($a) { + return max( + (scalar grep {$_ > 0} @{$a}), + (scalar grep {$_ < 0} @{$a}), + ); +} diff --git a/challenge-262/roger-bell-west/perl/ch-2.pl b/challenge-262/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..c88649baf6 --- /dev/null +++ b/challenge-262/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,22 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(countequaldivisible([3, 1, 2, 2, 2, 1, 3], 2), 4, 'example 1'); +is(countequaldivisible([1, 2, 3], 1), 0, 'example 2'); + +use Algorithm::Combinatorics qw(combinations); + +sub countequaldivisible($a, $k) { + my $s = 0; + foreach my $c (combinations([0 .. $#{$a}], 2)) { + if ($a->[$c->[0]] == $a->[$c->[1]] && $c->[0] * $c->[1] % $k == 0) { + $s++; + } + } + return $s; +} diff --git a/challenge-262/roger-bell-west/postscript/ch-1.ps b/challenge-262/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..94619bfb38 --- /dev/null +++ b/challenge-262/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,64 @@ +%!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 + +/filter { % array proc(bool) -> array + 1 dict begin + /p exch def + [ exch + { + dup p not + { + pop + } if + } forall + ] + end +} 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 + +/maxpositivenumber { + dup + { 0 gt } filter length exch + { 0 lt } filter length + max +} bind def + +(maxpositivenumber) test.start +[-3 1 2 -1 3 -2 4] maxpositivenumber 4 eq test +[-1 -2 -3 1] maxpositivenumber 3 eq test +[1 2] maxpositivenumber 2 eq test +test.end diff --git a/challenge-262/roger-bell-west/postscript/ch-2.ps b/challenge-262/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..45418b2013 --- /dev/null +++ b/challenge-262/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,93 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 + +/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 + +/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 + +/countequaldivisible { + 0 dict begin + /k exch def + /a exch def + 0 + [ 0 1 a length 1 sub { } for ] 2 combinations { + aload pop + /j exch def + /i exch def + a i get a j get eq { + i j mul k mod 0 eq { + 1 add + } if + } if + } forall + end +} bind def + +(countequaldivisible) test.start +[3 1 2 2 2 1 3] 2 countequaldivisible 4 eq test +[1 2 3] 1 countequaldivisible 0 eq test +test.end diff --git a/challenge-262/roger-bell-west/python/ch-1.py b/challenge-262/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..d3f2a54d9b --- /dev/null +++ b/challenge-262/roger-bell-west/python/ch-1.py @@ -0,0 +1,23 @@ +#! /usr/bin/python3 + +def maxpositivenumber(a): + return max( + len([c for c in a if c > 0]), + len([c for c in a if c < 0]) + ) + + +import unittest + +class TestMaxpositivenumber(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(maxpositivenumber([-3, 1, 2, -1, 3, -2, 4]), 4, 'example 1') + + def test_ex2(self): + self.assertEqual(maxpositivenumber([-1, -2, -3, 1]), 3, 'example 2') + + def test_ex3(self): + self.assertEqual(maxpositivenumber([1, 2]), 2, 'example 3') + +unittest.main() diff --git a/challenge-262/roger-bell-west/python/ch-2.py b/challenge-262/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..bfa28b3532 --- /dev/null +++ b/challenge-262/roger-bell-west/python/ch-2.py @@ -0,0 +1,22 @@ +#! /usr/bin/python3 + +from itertools import combinations + +def countequaldivisible(a, k): + s = 0 + for c in combinations(range(len(a)), 2): + if a[c[0]] == a[c[1]] and c[0] * c[1] % k == 0: + s += 1 + return s + +import unittest + +class TestCountequaldivisible(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(countequaldivisible([3, 1, 2, 2, 2, 1, 3], 2), 4, 'example 1') + + def test_ex2(self): + self.assertEqual(countequaldivisible([1, 2, 3], 1), 0, 'example 2') + +unittest.main() diff --git a/challenge-262/roger-bell-west/raku/ch-1.p6 b/challenge-262/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..39afdb8eeb --- /dev/null +++ b/challenge-262/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,16 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(maxpositivenumber([-3, 1, 2, -1, 3, -2, 4]), 4, 'example 1'); +is(maxpositivenumber([-1, -2, -3, 1]), 3, 'example 2'); +is(maxpositivenumber([1, 2]), 2, 'example 3'); + +sub maxpositivenumber(@a) { + return max( + @a.grep({$_ > 0}).elems, + @a.grep({$_ < 0}).elems, + ); +} diff --git a/challenge-262/roger-bell-west/raku/ch-2.p6 b/challenge-262/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..c9707cd909 --- /dev/null +++ b/challenge-262/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,18 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(countequaldivisible([3, 1, 2, 2, 2, 1, 3], 2), 4, 'example 1'); +is(countequaldivisible([1, 2, 3], 1), 0, 'example 2'); + +sub countequaldivisible(@a, $k) { + my $s = 0; + for combinations([0 .. @a.end], 2) -> @c { + if (@a[@c[0]] == @a[@c[1]] && @c[0] * @c[1] % $k == 0) { + $s++; + } + } + return $s; +} diff --git a/challenge-262/roger-bell-west/ruby/ch-1.rb b/challenge-262/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..16a1c0fb8b --- /dev/null +++ b/challenge-262/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,24 @@ +#! /usr/bin/ruby + +def maxpositivenumber(a) + return [a.find_all{|c| c > 0}.length, + a.find_all{|c| c < 0}.length].max +end + +require 'test/unit' + +class TestMaxpositivenumber < Test::Unit::TestCase + + def test_ex1 + assert_equal(4, maxpositivenumber([-3, 1, 2, -1, 3, -2, 4])) + end + + def test_ex2 + assert_equal(3, maxpositivenumber([-1, -2, -3, 1])) + end + + def test_ex3 + assert_equal(2, maxpositivenumber([1, 2])) + end + +end diff --git a/challenge-262/roger-bell-west/ruby/ch-2.rb b/challenge-262/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..a2e3a33d95 --- /dev/null +++ b/challenge-262/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,25 @@ +#! /usr/bin/ruby + +def countequaldivisible(a, k) + s = 0 + 0.upto(a.length - 1).to_a.combination(2) do |c| + if a[c[0]] == a[c[1]] && c[0] * c[1] % k == 0 then + s += 1 + end + end + return s +end + +require 'test/unit' + +class TestCountequaldivisible < Test::Unit::TestCase + + def test_ex1 + assert_equal(4, countequaldivisible([3, 1, 2, 2, 2, 1, 3], 2)) + end + + def test_ex2 + assert_equal(0, countequaldivisible([1, 2, 3], 1)) + end + +end diff --git a/challenge-262/roger-bell-west/rust/ch-1.rs b/challenge-262/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..f4700a824e --- /dev/null +++ b/challenge-262/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,26 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +use std::cmp::max; + +#[test] +fn test_ex1() { + assert_eq!(maxpositivenumber(vec![-3, 1, 2, -1, 3, -2, 4]), 4); +} + +#[test] +fn test_ex2() { + assert_eq!(maxpositivenumber(vec![-1, -2, -3, 1]), 3); +} + +#[test] +fn test_ex3() { + assert_eq!(maxpositivenumber(vec![1, 2]), 2); +} + +fn maxpositivenumber(a: Vec<i32>) -> u32 { + max( + a.iter().filter(|c| **c > 0).collect::<Vec<_>>().len(), + a.iter().filter(|c| **c < 0).collect::<Vec<_>>().len(), + ) as u32 +} diff --git a/challenge-262/roger-bell-west/rust/ch-2.rs b/challenge-262/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..d4d090ad90 --- /dev/null +++ b/challenge-262/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,21 @@ +use itertools::Itertools; + +#[test] +fn test_ex1() { + assert_eq!(countequaldivisible(vec![3, 1, 2, 2, 2, 1, 3], 2), 4); +} + +#[test] +fn test_ex2() { + assert_eq!(countequaldivisible(vec![1, 2, 3], 1), 0); +} + +fn countequaldivisible(a: Vec<u32>, k: usize) -> u32 { + let mut s = 0; + for c in a.iter().enumerate().combinations(2) { + if c[0].1 == c[1].1 && c[0].0 * c[1].0 % k == 0 { + s += 1; + } + } + s +} diff --git a/challenge-262/roger-bell-west/scala/ch-1.scala b/challenge-262/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..303f6f6404 --- /dev/null +++ b/challenge-262/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,30 @@ + +object Maxpositivenumber { + def maxpositivenumber(a: List[Int]): Int = { + List( + a.filter( c => c > 0 ).size, + a.filter( c => c < 0 ).size + ).max + } + def main(args: Array[String]) { + if (maxpositivenumber(List(-3, 1, 2, -1, 3, -2, 4)) == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxpositivenumber(List(-1, -2, -3, 1)) == 3) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maxpositivenumber(List(1, 2)) == 2) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-262/roger-bell-west/scala/ch-2.scala b/challenge-262/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..bce913b39a --- /dev/null +++ b/challenge-262/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,29 @@ + +object Countequaldivisible { + def countequaldivisible(a: List[Int], k: Int): Int = { + var s = 0 + for (i <- 0 to a.size - 2) { + for (j <- i + 1 to a.size - 1) { + if (a(i) == a(j) && i * j % k == 0) { + s += 1 + } + } + } + s + } + def main(args: Array[String]) { + if (countequaldivisible(List(3, 1, 2, 2, 2, 1, 3), 2) == 4) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (countequaldivisible(List(1, 2, 3), 1) == 0) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-262/roger-bell-west/tests.yaml b/challenge-262/roger-bell-west/tests.yaml new file mode 100644 index 0000000000..4b9d9d1650 --- /dev/null +++ b/challenge-262/roger-bell-west/tests.yaml @@ -0,0 +1,42 @@ +--- +ch-1: + - function: maxpositivenumber + arguments: + - -3 + - 1 + - 2 + - -1 + - 3 + - -2 + - 4 + result: 4 + - arguments: + - -1 + - -2 + - -3 + - 1 + result: 3 + - arguments: + - 1 + - 2 + result: 2 +ch-2: + - function: countequaldivisible + multiarg: true + arguments: + - - 3 + - 1 + - 2 + - 2 + - 2 + - 1 + - 3 + - 2 + result: 4 + - multiarg: true + arguments: + - - 1 + - 2 + - 3 + - 1 + result: 0 |
