diff options
| author | Roger Bell_West <roger@firedrake.org> | 2023-03-29 13:38:05 +0100 |
|---|---|---|
| committer | Roger Bell_West <roger@firedrake.org> | 2023-03-29 13:38:05 +0100 |
| commit | 305fbc36a56aab2531befe6a3a2cb33de51a56ae (patch) | |
| tree | acf6334983d7b91dcc1c2d33340fe6485b51fe9d | |
| parent | 8915a66de2cb2a724aee5e55ddfc15580cfdf1d5 (diff) | |
| download | perlweeklychallenge-club-305fbc36a56aab2531befe6a3a2cb33de51a56ae.tar.gz perlweeklychallenge-club-305fbc36a56aab2531befe6a3a2cb33de51a56ae.tar.bz2 perlweeklychallenge-club-305fbc36a56aab2531befe6a3a2cb33de51a56ae.zip | |
RogerBW solutions for challenge no. 210
19 files changed, 780 insertions, 0 deletions
diff --git a/challenge-210/roger-bell-west/javascript/ch-1.js b/challenge-210/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..8f9557d885 --- /dev/null +++ b/challenge-210/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,20 @@ +#! /usr/bin/node + +"use strict" + +function killandwin(a) { + return a.reduce((x, y) => x + y, 0); +} + +if (killandwin([2, 3, 1]) == 6) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (killandwin([1, 1, 2, 2, 2, 3]) == 11) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-210/roger-bell-west/javascript/ch-2.js b/challenge-210/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..d0b4df18a1 --- /dev/null +++ b/challenge-210/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,76 @@ +#! /usr/bin/node + +"use strict" + +// 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; + } +} + +function numbercollision(aa) { + let a = aa; + let dirty = true; + while (dirty) { + let b = []; + dirty = false; + let i = 0; + while (i + 1 <= a.length) { + if (i + 1 < a.length && a[i] > 0 && a[i + 1] < 0) { + let ab = Math.abs(a[i + 1]); + if (a[i] > ab) { + b.push(a[i]); + } else if (a[i] < ab) { + b.push(a[i + 1]); + } + i += 2; + dirty = true; + } else { + b.push(a[i]); + i += 1; + } + } + a = b; + } + return a; +} + +if (deepEqual(numbercollision([2, 3, -1]), [2, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(numbercollision([3, 2, -4]), [-4])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(numbercollision([1, -1]), [])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-210/roger-bell-west/kotlin/ch-1.kt b/challenge-210/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..e917eb0824 --- /dev/null +++ b/challenge-210/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,20 @@ +fun killandwin(a: List<Int>): Int { + return a.sum() +} + +fun main() { + + if (killandwin(listOf(2, 3, 1)) == 6) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (killandwin(listOf(1, 1, 2, 2, 2, 3)) == 11) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-210/roger-bell-west/kotlin/ch-2.kt b/challenge-210/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..627ce7d3f2 --- /dev/null +++ b/challenge-210/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,51 @@ +fun numbercollision(aa: List<Int>): List<Int> { + var a = aa + var dirty = true + while (dirty) { + var b = ArrayList<Int>() + dirty = false + var i = 0 + while (i + 1 <= a.size) { + if (i + 1 < a.size && + a[i] > 0 && + a[i + 1] < 0) { + val ab = Math.abs(a[i + 1]) + if (a[i] > ab) { + b.add(a[i]) + } else if (a[i] < ab) { + b.add(a[i + 1]) + } + i += 2 + dirty = true + } else { + b.add(a[i]) + i += 1 + } + } + a = b.toList() + } + return a +} + +fun main() { + + if (numbercollision(listOf(2, 3, -1)) == listOf(2, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (numbercollision(listOf(3, 2, -4)) == listOf(-4)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (numbercollision(listOf(1, -1)) == emptyList<Int>()) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-210/roger-bell-west/lua/ch-1.lua b/challenge-210/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..dc4a74cd50 --- /dev/null +++ b/challenge-210/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,24 @@ +#! /usr/bin/lua + +function killandwin(a) + local t = 0 + for k, v in ipairs(a) do + t = t + v + end + return t +end + +if killandwin({2, 3, 1}) == 6 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if killandwin({1, 1, 2, 2, 2, 3}) == 11 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-210/roger-bell-west/lua/ch-2.lua b/challenge-210/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..635f55d792 --- /dev/null +++ b/challenge-210/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,69 @@ +#! /usr/bin/lua + +-- by Michael Anderson at +-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua +function recursive_compare(t1,t2) + if t1==t2 then return true end + if (type(t1)~="table") then return false end + local mt1 = getmetatable(t1) + local mt2 = getmetatable(t2) + if( not recursive_compare(mt1,mt2) ) then return false end + for k1,v1 in pairs(t1) do + local v2 = t2[k1] + if( not recursive_compare(v1,v2) ) then return false end + end + for k2,v2 in pairs(t2) do + local v1 = t1[k2] + if( not recursive_compare(v1,v2) ) then return false end + end + return true +end + +function numbercollision(aa) + local a = aa + local dirty = true + while dirty do + local b = {} + dirty = false + local i = 1 + while i <= #a do + if i < #a and a[i] > 0 and a[i + 1] < 0 then + local ab = math.abs(a[i + 1]) + if a[i] > ab then + table.insert(b, a[i]) + elseif a[i] < ab then + table.insert(b, a[i + 1]) + end + i = i + 2 + dirty = true + else + table.insert(b, a[i]) + i = i + 1 + end + end + a = b + end + return a +end + +if recursive_compare(numbercollision({2, 3, -1}), {2, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(numbercollision({3, 2, -4}), {-4}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(numbercollision({1, -1}), {}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-210/roger-bell-west/perl/ch-1.pl b/challenge-210/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..ff532e0d2b --- /dev/null +++ b/challenge-210/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,16 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(killandwin([2, 3, 1]), 6, 'example 1'); +is(killandwin([1, 1, 2, 2, 2, 3]), 11, 'example 2'); + +use List::Util qw(sum); + +sub killandwin($a) { + return sum(@{$a}); +} diff --git a/challenge-210/roger-bell-west/perl/ch-2.pl b/challenge-210/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..2767077752 --- /dev/null +++ b/challenge-210/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,38 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is_deeply(numbercollision([2, 3, -1]), [2, 3], 'example 1'); +is_deeply(numbercollision([3, 2, -4]), [-4], 'example 2'); +is_deeply(numbercollision([1, -1]), [], 'example 3'); + +sub numbercollision($aa) { + my @a = @{$aa}; + my $dirty = 1; + while ($dirty) { + my @b; + $dirty = 0; + my $i = 0; + while ($i <= $#a) { + if ($i < $#a && $a[$i] > 0 && $a[$i + 1] < 0 ) { + my $ab = abs($a[$i+1]); + if ($a[$i] > $ab) { + push @b, $a[$i]; + } elsif ($a[$i] < $ab) { + push @b, $a[$i + 1]; + } + $i+=2; + $dirty = 1; + } else { + push @b, $a[$i]; + $i++; + } + } + @a = @b; + } + return \@a; +} diff --git a/challenge-210/roger-bell-west/postscript/ch-1.ps b/challenge-210/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..f87ad030cc --- /dev/null +++ b/challenge-210/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,58 @@ +%!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 + +/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 + +/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 + +/killandwin { + { add } reduce +} bind def + +(killandwin) test.start +[2 3 1] killandwin 6 eq test +[1 1 2 2 2 3] killandwin 11 eq test +test.end diff --git a/challenge-210/roger-bell-west/postscript/ch-2.ps b/challenge-210/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..cd522c8c84 --- /dev/null +++ b/challenge-210/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,143 @@ +%!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 + +/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.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 + + +% end included library code + +/numbercollision { + 5 dict begin + /a exch def + /dirty true def + { + dirty not { + exit + } if + [ + /dirty false def + /i 0 def + { + i 1 add a length gt { + exit + } if + /std true def + i 1 add a length lt { + a i get 0 gt a i 1 add get 0 lt and { + /std false def + /ab a i 1 add get abs def + a i get ab gt { + a i get + } { + a i get ab lt { + a i 1 add get + } if + } ifelse + /i i 2 add def + /dirty true def + } if + } if + std { + a i get + /i i 1 add def + } if + } loop + ] /a exch def + } loop + a + end +} bind def + +(numbercollision) test.start +[2 3 -1] numbercollision [2 3] deepeq test +[3 2 -4] numbercollision [-4] deepeq test +[1 -1] numbercollision [] deepeq test +test.end diff --git a/challenge-210/roger-bell-west/python/ch-1.py b/challenge-210/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..50a7cc7f10 --- /dev/null +++ b/challenge-210/roger-bell-west/python/ch-1.py @@ -0,0 +1,16 @@ +#! /usr/bin/python3 + +import unittest + +def killandwin(a): + return sum(a) + +class TestKillandwin(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(killandwin([2, 3, 1]), 6, 'example 1') + + def test_ex2(self): + self.assertEqual(killandwin([1, 1, 2, 2, 2, 3]), 11, 'example 2') + +unittest.main() diff --git a/challenge-210/roger-bell-west/python/ch-2.py b/challenge-210/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..95a98520ee --- /dev/null +++ b/challenge-210/roger-bell-west/python/ch-2.py @@ -0,0 +1,38 @@ +#! /usr/bin/python3 + +import unittest + +def numbercollision(aa): + a = aa + dirty = True + while dirty: + b = [] + dirty = False + i = 0 + while i + 1 <= len(a): + if i + 1 < len(a) and a[i] > 0 and a[i + 1] < 0: + ab = abs(a[i + 1]) + if a[i] > ab: + b.append(a[i]) + elif a[i] < ab: + b.append(a[i + 1]) + i += 2 + dirty = True + else: + b.append(a[i]) + i += 1 + a = b + return a + +class TestNumbercollision(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(numbercollision([2, 3, -1]), [2, 3], 'example 1') + + def test_ex2(self): + self.assertEqual(numbercollision([3, 2, -4]), [-4], 'example 2') + + def test_ex3(self): + self.assertEqual(numbercollision([1, -1]), [], 'example 3') + +unittest.main() diff --git a/challenge-210/roger-bell-west/raku/ch-1.p6 b/challenge-210/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..08ccbb12ac --- /dev/null +++ b/challenge-210/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,12 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(killandwin([2, 3, 1]), 6, 'example 1'); +is(killandwin([1, 1, 2, 2, 2, 3]), 11, 'example 2'); + +sub killandwin(@a) { + return @a.sum; +} diff --git a/challenge-210/roger-bell-west/raku/ch-2.p6 b/challenge-210/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..7d90f420b5 --- /dev/null +++ b/challenge-210/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,36 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is-deeply(numbercollision([2, 3, -1]), [2, 3], 'example 1'); +is-deeply(numbercollision([3, 2, -4]), [-4], 'example 2'); +is-deeply(numbercollision([1, -1]), [], 'example 3'); + +sub numbercollision(@aa) { + my @a = @aa; + my $dirty = True; + while ($dirty) { + my @b; + $dirty = False; + my $i = 0; + while ($i <= @a.end) { + if ($i < @a.end && @a[$i] > 0 && @a[$i + 1] < 0 ) { + my $ab = abs(@a[$i+1]); + if (@a[$i] > $ab) { + @b.push(@a[$i]); + } elsif (@a[$i] < $ab) { + @b.push(@a[$i + 1]); + } + $i+=2; + $dirty = True; + } else { + @b.push(@a[$i]); + $i++; + } + } + @a = @b; + } + return @a; +} diff --git a/challenge-210/roger-bell-west/ruby/ch-1.rb b/challenge-210/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..31011a35fe --- /dev/null +++ b/challenge-210/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,19 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def killandwin(a) + return a.sum() +end + +class TestKillandwin < Test::Unit::TestCase + + def test_ex1 + assert_equal(6, killandwin([2, 3, 1])) + end + + def test_ex2 + assert_equal(11, killandwin([1, 1, 2, 2, 2, 3])) + end + +end diff --git a/challenge-210/roger-bell-west/ruby/ch-2.rb b/challenge-210/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..afd96a2a68 --- /dev/null +++ b/challenge-210/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,46 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def numbercollision(aa) + a = aa + dirty = true + while dirty do + b = [] + dirty = false + i = 0 + while i + 1 <= a.length do + if i + 1 < a.length && a[i] > 0 && a[i + 1] < 0 then + ab = a[i + 1].abs + if a[i] > ab then + b.push(a[i]) + elsif a[i] < ab then + b.push(a[i + 1]) + end + i += 2 + dirty = true + else + b.push(a[i]) + i += 1 + end + end + a = b; + end + return a +end + +class TestNumbercollision < Test::Unit::TestCase + + def test_ex1 + assert_equal([2, 3], numbercollision([2, 3, -1])) + end + + def test_ex2 + assert_equal([-4], numbercollision([3, 2, -4])) + end + + def test_ex3 + assert_equal([], numbercollision([1, -1])) + end + +end diff --git a/challenge-210/roger-bell-west/rust/ch-1.rs b/challenge-210/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..bc435fe505 --- /dev/null +++ b/challenge-210/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,16 @@ +#! /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!(killandwin(vec![2, 3, 1]), 6); +} + +#[test] +fn test_ex2() { + assert_eq!(killandwin(vec![1, 1, 2, 2, 2, 3]), 11); +} + +fn killandwin(a: Vec<u32>) -> u32 { + a.iter().sum::<u32>() +} diff --git a/challenge-210/roger-bell-west/rust/ch-2.rs b/challenge-210/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..d5a579b0f6 --- /dev/null +++ b/challenge-210/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,44 @@ +#! /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!(numbercollision(vec![2, 3, -1]), vec![2, 3]); +} + +#[test] +fn test_ex2() { + assert_eq!(numbercollision(vec![3, 2, -4]), vec![-4]); +} + +#[test] +fn test_ex3() { + assert_eq!(numbercollision(vec![1, -1]), Vec::<i32>::new()); +} + +fn numbercollision(aa: Vec<i32>) -> Vec<i32> { + let mut a = aa.clone(); + let mut dirty: bool = true; + while dirty { + let mut b = Vec::new(); + dirty = false; + let mut i = 0; + while i + 1 <= a.len() { + if i + 1 < a.len() && a[i] > 0 && a[i + 1] < 0 { + let ab = a[i + 1].abs(); + if a[i] > ab { + b.push(a[i]); + } else if a[i] < ab { + b.push(a[i + 1]); + } + i += 2; + dirty = true; + } else { + b.push(a[i]); + i += 1; + } + } + a = b; + } + a +} diff --git a/challenge-210/roger-bell-west/tests.yaml b/challenge-210/roger-bell-west/tests.yaml new file mode 100644 index 0000000000..a0f4c639f9 --- /dev/null +++ b/challenge-210/roger-bell-west/tests.yaml @@ -0,0 +1,38 @@ +--- +ch-1: + - function: killandwin + arguments: + - 2 + - 3 + - 1 + result: 6 + - function: killandwin + arguments: + - 1 + - 1 + - 2 + - 2 + - 2 + - 3 + result: 11 +ch-2: + - function: numbercollision + arguments: + - 2 + - 3 + - -1 + result: + - 2 + - 3 + - function: numbercollision + arguments: + - 3 + - 2 + - -4 + result: + - -4 + - function: numbercollision + arguments: + - 1 + - -1 + result: [] |
