diff options
| author | Roger Bell_West <roger@firedrake.org> | 2023-02-21 14:58:35 +0000 |
|---|---|---|
| committer | Roger Bell_West <roger@firedrake.org> | 2023-02-21 14:58:35 +0000 |
| commit | 478441fbbbef3833f35bcdd269add62e83ca8eae (patch) | |
| tree | f5d776eb070c5b80c3bcfce0209c7d7bffd22c6d | |
| parent | a7c004dd7466bb12a849f30ea7c107574be41c4b (diff) | |
| download | perlweeklychallenge-club-478441fbbbef3833f35bcdd269add62e83ca8eae.tar.gz perlweeklychallenge-club-478441fbbbef3833f35bcdd269add62e83ca8eae.tar.bz2 perlweeklychallenge-club-478441fbbbef3833f35bcdd269add62e83ca8eae.zip | |
RogerBW solutions for challenge no. 205
19 files changed, 780 insertions, 0 deletions
diff --git a/challenge-205/roger-bell-west/javascript/ch-1.js b/challenge-205/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..8c3763df5e --- /dev/null +++ b/challenge-205/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,36 @@ +#! /usr/bin/node + +"use strict" + +function thirdhighest(l) { + let v = [...new Set(l)]; + v.sort(function(a,b) { + return b-a; + }); + if (v.length == 0) { + return 0; + } else if (v.length <= 2) { + return v[0]; + } else { + return v[2]; + } +} + +if (thirdhighest([5, 3, 4]) == 3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (thirdhighest([5, 6]) == 6) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (thirdhighest([5, 4, 4, 3]) == 3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-205/roger-bell-west/javascript/ch-2.js b/challenge-205/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..9a54e19bd8 --- /dev/null +++ b/challenge-205/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,32 @@ +#! /usr/bin/node + +"use strict" + +function maximumxor(l) { + let m = []; + for (let ai = 0; ai < l.length-1; ai++) { + for (let bi = ai + 1; bi < l.length; bi++) { + m.push(l[ai] ^ l[bi]); + } + } + return Math.max(...m); +} + +if (maximumxor([1, 2, 3, 4, 5, 6, 7]) == 7) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maximumxor([2, 4, 1, 3]) == 7) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (maximumxor([10, 5, 7, 12, 8]) == 15) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-205/roger-bell-west/kotlin/ch-1.kt b/challenge-205/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..e89c3490a2 --- /dev/null +++ b/challenge-205/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,35 @@ +fun thirdhighest(l: List<Int>): Int { + var v = ArrayList(l.toSet()) + v.sort() + v.reverse() + if (v.size == 0) { + return 0 + } else if (v.size <= 2) { + return v[0] + } else { + return v[2] + } +} + +fun main() { + + if (thirdhighest(listOf(5, 3, 4)) == 3) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (thirdhighest(listOf(5, 6)) == 6) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (thirdhighest(listOf(5, 4, 4, 3)) == 3) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-205/roger-bell-west/kotlin/ch-2.kt b/challenge-205/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..6fc00f1a7b --- /dev/null +++ b/challenge-205/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,32 @@ +fun maximumxor(l: List<Int>): Int { + var m = ArrayList<Int>() + for (ai in 0..l.size-2) { + for (bi in ai+1..l.size-1) { + m.add(l[ai] xor l[bi]) + } + } + return m.maxOrNull()!! +} + +fun main() { + + if (maximumxor(listOf(1, 2, 3, 4, 5, 6, 7)) == 7) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maximumxor(listOf(2, 4, 1, 3)) == 7) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (maximumxor(listOf(10, 5, 7, 12, 8)) == 15) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-205/roger-bell-west/lua/ch-1.lua b/challenge-205/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..3fdd35519f --- /dev/null +++ b/challenge-205/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,57 @@ +#! /usr/bin/lua + +-- by hookenz at +-- https://stackoverflow.com/questions/9168058/how-to-dump-a-table-to-console +function dump(o) + if type(o) == 'table' then + local s = '{ ' + for k,v in pairs(o) do + if type(k) ~= 'number' then k = '"'..k..'"' end + s = s .. '['..k..'] = ' .. dump(v) .. ',' + end + return s .. '} ' + else + return tostring(o) + end +end + +function thirdhighest(l) + local m = {} + for k, va in ipairs(l) do + m[va] = true + end + local v = {} + for k, va in pairs(m) do + table.insert(v,k) + end + table.sort(v, function(a, b) return b < a end) + if #v == 0 then + return 0 + elseif #v <= 2 then + return v[1] + else + return v[3] + end +end + +if thirdhighest({5, 3, 4}) == 3 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if thirdhighest({5, 6}) == 6 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if thirdhighest({5, 4, 4, 3}) == 3 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-205/roger-bell-west/lua/ch-2.lua b/challenge-205/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..d0c73a3598 --- /dev/null +++ b/challenge-205/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,33 @@ +#! /usr/bin/lua + +function maximumxor(l) + local m = {} + for ai = 1,#l - 1 do + for bi = ai + 1,#l do + table.insert(m, l[ai] ~ l[bi]) + end + end + return math.max(table.unpack(m)) +end + +if maximumxor({1, 2, 3, 4, 5, 6, 7}) == 7 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maximumxor({2, 4, 1, 3}) == 7 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if maximumxor({10, 5, 7, 12, 8}) == 15 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-205/roger-bell-west/perl/ch-1.pl b/challenge-205/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..3d639b12cf --- /dev/null +++ b/challenge-205/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(thirdhighest([5, 3, 4]), 3, 'example 1'); +is(thirdhighest([5, 6]), 6, 'example 2'); +is(thirdhighest([5, 4, 4, 3]), 3, 'example 3'); + +sub thirdhighest($l) { + my @v = reverse sort keys %{{map {$_ => 1} @{$l}}}; + if (scalar @v == 0) { + return 0; + } elsif (scalar @v <= 2) { + return $v[0]; + } else { + return $v[2]; + } +} diff --git a/challenge-205/roger-bell-west/perl/ch-2.pl b/challenge-205/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..6f049e74f0 --- /dev/null +++ b/challenge-205/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,23 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(maximumxor([1, 2, 3, 4, 5, 6, 7]), 7, 'example 1'); +is(maximumxor([2, 4, 1, 3]), 7, 'example 2'); +is(maximumxor([10, 5, 7, 12, 8]), 15, 'example 3'); + +use Algorithm::Combinatorics qw(combinations); +use List::Util qw(max); + +sub maximumxor($l) { + my @m; + my $i = combinations($l, 2); + while (my $c = $i->next) { + push @m, $c->[0] ^ $c->[1]; + } + return max(@m); +} diff --git a/challenge-205/roger-bell-west/postscript/ch-1.ps b/challenge-205/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..332fdf5585 --- /dev/null +++ b/challenge-205/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,159 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} 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 + +/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 { % [ a c b ] -> [ a b c ] + 1 dict begin + /arr exch def + arr length 0 gt { + 0 arr length 1 sub quicksort.main + } if + arr + end +} 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 ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + end +} bind def + +/reverse { + 1 dict begin + dup length /l exch def + [ exch + aload pop + 2 1 l { + -1 roll + } for + ] + 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 + +/toset { % array -> dict of (value, true) + << exch + { + true + } forall + >> +} bind def + + +% end included library code + +/thirdhighest { + 1 dict begin + toset keys quicksort reverse /v exch def + /l v length def + 1 { + l 0 eq { + 0 + exit + } if + l 1 ge l 2 le and { + v 0 get + exit + } if + v 2 get + } repeat + end +} bind def + +(thirdhighest) test.start +[5 3 4] thirdhighest 3 eq test +[5 6] thirdhighest 6 eq test +[5 4 4 3] thirdhighest 3 eq test +test.end diff --git a/challenge-205/roger-bell-west/postscript/ch-2.ps b/challenge-205/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..5aee012bfb --- /dev/null +++ b/challenge-205/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,100 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/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 + +/listmax { + { max } reduce +} 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 + +/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 + +/maximumxor { + [ exch + 2 combinations { + dup 0 get exch 1 get xor + } forall + ] listmax +} bind def + +(maximumxor) test.start +[1 2 3 4 5 6 7] maximumxor 7 eq test +[2 4 1 3] maximumxor 7 eq test +[10 5 7 12 8] maximumxor 15 eq test +test.end diff --git a/challenge-205/roger-bell-west/python/ch-1.py b/challenge-205/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..f98b373229 --- /dev/null +++ b/challenge-205/roger-bell-west/python/ch-1.py @@ -0,0 +1,28 @@ +#! /usr/bin/python3 + +import unittest + +def thirdhighest(l): + v = list(set(l)) + v.sort() + v.reverse() + vl = len(v) + if vl == 0: + return 0 + elif vl <= 2: + return v[0] + else: + return v[2] + +class TestThirdhighest(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(thirdhighest([5, 3, 4]), 3, 'example 1') + + def test_ex2(self): + self.assertEqual(thirdhighest([5, 6]), 6, 'example 2') + + def test_ex3(self): + self.assertEqual(thirdhighest([5, 4, 4, 3]), 3, 'example 3') + +unittest.main() diff --git a/challenge-205/roger-bell-west/python/ch-2.py b/challenge-205/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..31213c2e43 --- /dev/null +++ b/challenge-205/roger-bell-west/python/ch-2.py @@ -0,0 +1,25 @@ +#! /usr/bin/python3 + +import unittest + +from itertools import combinations + +def maximumxor(l): + m = [] + for c in combinations(l, 2): + m.append(c[0] ^ c[1]) + return max(m) + + +class TestMaximumxor(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(maximumxor([1, 2, 3, 4, 5, 6, 7]), 7, 'example 1') + + def test_ex2(self): + self.assertEqual(maximumxor([2, 4, 1, 3]), 7, 'example 2') + + def test_ex3(self): + self.assertEqual(maximumxor([10, 5, 7, 12, 8]), 15, 'example 3') + +unittest.main() diff --git a/challenge-205/roger-bell-west/raku/ch-1.p6 b/challenge-205/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..206cce0d1e --- /dev/null +++ b/challenge-205/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,20 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(thirdhighest([5, 3, 4]), 3, 'example 1'); +is(thirdhighest([5, 6]), 6, 'example 2'); +is(thirdhighest([5, 4, 4, 3]), 3, 'example 3'); + +sub thirdhighest(@l) { + my @v = @l.Set.keys.sort.reverse; + if (@v.elems == 0) { + return 0; + } elsif (@v.elems <= 2) { + return @v[0]; + } else { + return @v[2]; + } +} diff --git a/challenge-205/roger-bell-west/raku/ch-2.p6 b/challenge-205/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..79102549d6 --- /dev/null +++ b/challenge-205/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,17 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(maximumxor([1, 2, 3, 4, 5, 6, 7]), 7, 'example 1'); +is(maximumxor([2, 4, 1, 3]), 7, 'example 2'); +is(maximumxor([10, 5, 7, 12, 8]), 15, 'example 3'); + +sub maximumxor(@l) { + my @ct; + for @l.combinations(2) -> @c { + @ct.push(@c[0] +^ @c[1]); + } + return @ct.max; +} diff --git a/challenge-205/roger-bell-west/ruby/ch-1.rb b/challenge-205/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..7e00f285cc --- /dev/null +++ b/challenge-205/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,32 @@ +#! /usr/bin/ruby + +require 'test/unit' + +require 'set' + +def thirdhighest(l) + v = Set.new(l).to_a.sort.reverse + if v.length == 0 then + return 0 + elsif v.length <= 2 then + return v[0] + else + return v[2] + end +end + +class TestThirdhighest < Test::Unit::TestCase + + def test_ex1 + assert_equal(3, thirdhighest([5, 3, 4])) + end + + def test_ex2 + assert_equal(6, thirdhighest([5, 6])) + end + + def test_ex3 + assert_equal(3, thirdhighest([5, 4, 4, 3])) + end + +end diff --git a/challenge-205/roger-bell-west/ruby/ch-2.rb b/challenge-205/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..41b7891d49 --- /dev/null +++ b/challenge-205/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,27 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def maximumxor(l) + m = [] + l.combination(2) do |c| + m.push(c[0] ^ c[1]) + end + return m.max +end + +class TestMaximumxor < Test::Unit::TestCase + + def test_ex1 + assert_equal(7, maximumxor([1, 2, 3, 4, 5, 6, 7])) + end + + def test_ex2 + assert_equal(7, maximumxor([2, 4, 1, 3])) + end + + def test_ex3 + assert_equal(15, maximumxor([10, 5, 7, 12, 8])) + end + +end diff --git a/challenge-205/roger-bell-west/rust/ch-1.rs b/challenge-205/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..e173e414a4 --- /dev/null +++ b/challenge-205/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,29 @@ +#! /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!(thirdhighest(vec![5, 3, 4]), 3); +} + +#[test] +fn test_ex2() { + assert_eq!(thirdhighest(vec![5, 6]), 6); +} + +#[test] +fn test_ex3() { + assert_eq!(thirdhighest(vec![5, 4, 4, 3]), 3); +} + +fn thirdhighest(l: Vec<i32>) -> i32 { + let mut v = l.clone(); + v.sort(); + v.dedup(); + v.reverse(); + match v.len() { + 0 => 0, + 1..=2 => v[0], + _ => v[2], + } +} diff --git a/challenge-205/roger-bell-west/rust/ch-2.rs b/challenge-205/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..99ba01f2bf --- /dev/null +++ b/challenge-205/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,27 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +// [dependencies] +// itertools = "0.10.5" + +use itertools::Itertools; + +#[test] +fn test_ex1() { + assert_eq!(maximumxor(vec![1, 2, 3, 4, 5, 6, 7]), 7); +} + +#[test] +fn test_ex2() { + assert_eq!(maximumxor(vec![2, 4, 1, 3]), 7); +} + +#[test] +fn test_ex3() { + assert_eq!(maximumxor(vec![10, 5, 7, 12, 8]), 15); +} + + +fn maximumxor(l: Vec<i32>) -> i32 { + l.iter().combinations(2).map(|i| i[1] ^ i[0]).max().unwrap() +} diff --git a/challenge-205/roger-bell-west/tests.yaml b/challenge-205/roger-bell-west/tests.yaml new file mode 100644 index 0000000000..19342eca71 --- /dev/null +++ b/challenge-205/roger-bell-west/tests.yaml @@ -0,0 +1,46 @@ +--- +ch-1: + - function: thirdhighest + arguments: + - 5 + - 3 + - 4 + result: 3 + - function: thirdhighest + arguments: + - 5 + - 6 + result: 6 + - function: thirdhighest + arguments: + - 5 + - 4 + - 4 + - 3 + result: 3 +ch-2: + - function: maximumxor + arguments: + - 1 + - 2 + - 3 + - 4 + - 5 + - 6 + - 7 + result: 7 + - function: maximumxor + arguments: + - 2 + - 4 + - 1 + - 3 + result: 7 + - function: maximumxor + arguments: + - 10 + - 5 + - 7 + - 12 + - 8 + result: 15 |
