diff options
21 files changed, 634 insertions, 0 deletions
diff --git a/challenge-246/roger-bell-west/javascript/ch-1.js b/challenge-246/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..d08d322ce9 --- /dev/null +++ b/challenge-246/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,24 @@ +#! /usr/bin/node + +"use strict" + +function shuffleArray(input) { + let keys = Array(input.length).fill().map((element, index) => Math.random()); + let ix = Array(input.length).fill().map((element, index) => index); + ix.sort(function(a, b) { + return keys[a] - keys[b]; + }); + return ix.map(n => input[n]); +} + +function sixoffortynine() { + let candidates = Array(49).fill().map((element, index) => index + 1); + candidates = shuffleArray(candidates); + candidates.length = 6; + candidates.sort(function(a, b) { + return a - b; + }); + console.log(JSON.stringify(candidates)); +} + +sixoffortynine(); diff --git a/challenge-246/roger-bell-west/javascript/ch-2.js b/challenge-246/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..6eae7e5586 --- /dev/null +++ b/challenge-246/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,31 @@ +#! /usr/bin/node + +"use strict" + +function linearrecurrencesecondorder(seq) { + const a = seq.slice(0, 3); + const b = seq.slice(1, 4); + const c = seq.slice(2, 5); + const q = Math.floor((b[2] * a[0] - b[0] * a[2]) / (b[1] * a[0] - b[0] * a[1])); + const p = Math.floor((a[2] - a[1] * q) / a[0]); + return p * a[0] + q * a[1] == a[2] && p * b[0] + q * b[1] == b[2] && p * c[0] + q * c[1] == c[2] +} + +if (linearrecurrencesecondorder([1, 1, 2, 3, 5])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!linearrecurrencesecondorder([4, 2, 4, 5, 7])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (linearrecurrencesecondorder([4, 1, 2, -3, 8])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-246/roger-bell-west/kotlin/ch-1.kt b/challenge-246/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..7e545a7dd7 --- /dev/null +++ b/challenge-246/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,11 @@ +fun sixoffortynine() { + var candidates = (1..49).toList() + candidates = candidates.shuffled() + candidates = candidates.take(6) + candidates = candidates.sorted() + println(candidates) +} + +fun main() { + sixoffortynine() +} diff --git a/challenge-246/roger-bell-west/kotlin/ch-2.kt b/challenge-246/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..c4fa62fe32 --- /dev/null +++ b/challenge-246/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,31 @@ +fun linearrecurrencesecondorder(seq: List<Int>): Boolean { + val a = seq.take(3) + val b = seq.take(4).takeLast(3) + val c = seq.take(5).takeLast(3) + val q = (b[2] * a[0] - b[0] * a[2]) / (b[1] * a[0] - b[0] * a[1]) + val p = (a[2] - a[1] * q) / a[0] + return p * a[0] + q * a[1] == a[2] && p * b[0] + q * b[1] == b[2] && p * c[0] + q * c[1] == c[2] +} + +fun main() { + + if (linearrecurrencesecondorder(listOf(1, 1, 2, 3, 5))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!linearrecurrencesecondorder(listOf(4, 2, 4, 5, 7))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (linearrecurrencesecondorder(listOf(4, 1, 2, -3, 8))) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-246/roger-bell-west/lua/ch-1.lua b/challenge-246/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..aed1a4805d --- /dev/null +++ b/challenge-246/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,64 @@ +#! /usr/bin/lua + +-- by Michael Anderson at +-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua +-- modified by Roger +function recursive_compare(t1,t2) + -- Use usual comparison first. + if t1==t2 then return true end + -- We only support non-default behavior for tables + if (type(t1)~="table") then return false end + -- They better have the same metatables + local mt1 = getmetatable(t1) + local mt2 = getmetatable(t2) + if( not recursive_compare(mt1,mt2) ) then return false end + -- Build list of all keys + local kk = {} + for k1, _ in pairs(t1) do + kk[k1] = true + end + for k2, _ in pairs(t2) do + kk[k2] = true + end + -- Check each key that exists in at least one table + for _, k in ipairs(kk) do + if (not recursive_compare(t1[k], t2[k])) then + return false + end + end + return true +end + +function shuffle(input) + local ix = {} + local keys = {} + for n = 1, #input do + table.insert(ix, n) + table.insert(keys, math.random()) + end + table.sort(ix, function (i, j) return keys[i] < keys[j] end) + local out = {} + for _, v in ipairs(ix) do + table.insert(out, input[v]) + end + return out +end + +function sixoffortynine() + local candidates = {} + for n = 1, 49 do + table.insert(candidates, n) + end + candidates = shuffle(candidates) + local o = {} + for n = 1, 6 do + table.insert(o, candidates[n]) + end + candidates = o + table.sort(candidates, function (i, j) return i < j end) + for n = 1, 6 do + print(candidates[n]) + end +end + +sixoffortynine() diff --git a/challenge-246/roger-bell-west/lua/ch-2.lua b/challenge-246/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..85281be2d7 --- /dev/null +++ b/challenge-246/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,32 @@ +#! /usr/bin/lua + +function linearrecurrencesecondorder(seq) + local a = {seq[1], seq[2], seq[3]} + local b = {seq[2], seq[3], seq[4]} + local c = {seq[3], seq[4], seq[5]} + local q = math.floor((b[3] * a[1] - b[1] * a[3]) / (b[2] * a[1] - b[1] * a[2])); + local p = math.floor((a[3] - a[2] * q) / a[1]); + return p * a[1] + q * a[2] == a[3] and p * b[1] + q * b[2] == b[3] and p * c[1] + q * c[2] == c[3] +end + +if linearrecurrencesecondorder({1, 1, 2, 3, 5}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not linearrecurrencesecondorder({4, 2, 4, 5, 7}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if linearrecurrencesecondorder({4, 1, 2, -3, 8}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-246/roger-bell-west/perl/ch-1.pl b/challenge-246/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..dee4976b50 --- /dev/null +++ b/challenge-246/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,17 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +sixoffortynine(); + +use List::Util qw(shuffle); + +sub sixoffortynine { + my @candidates = (1..49); + @candidates = shuffle @candidates; + splice @candidates, 6; + @candidates = sort {$a <=> $b} @candidates; + print join(', ', @candidates), "\n"; +} diff --git a/challenge-246/roger-bell-west/perl/ch-2.pl b/challenge-246/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..302df6ddd4 --- /dev/null +++ b/challenge-246/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 => 3; + +is(linearrecurrencesecondorder([1, 1, 2, 3, 5]), 1, 'example 1'); +is(linearrecurrencesecondorder([4, 2, 4, 5, 7]), 0, 'example 2'); +is(linearrecurrencesecondorder([4, 1, 2, -3, 8]), 1, 'example 3'); + +use integer; + +sub linearrecurrencesecondorder($seq) { + my $a = [@{$seq}[0..2]]; + my $b = [@{$seq}[1..3]]; + my $c = [@{$seq}[2..4]]; + my $q = ($b->[2] * $a->[0] - $b->[0] * $a->[2]) / ($b->[1] * $a->[0] - $b->[0] * $a->[1]); + my $p = ($a->[2] - $a->[1] * $q) / $a->[0]; + return ($p * $a->[0] + $q * $a->[1] == $a->[2] && $p * $b->[0] + $q * $b->[1] == $b->[2] && $p * $c->[0] + $q * $c->[1] == $c->[2])?1:0; +} diff --git a/challenge-246/roger-bell-west/postscript/ch-1.ps b/challenge-246/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..dee0e8d905 --- /dev/null +++ b/challenge-246/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,134 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/quicksort.cmp { + 2 copy + lt { + pop pop -1 + } { + gt { + 1 + } { + 0 + } ifelse + } ifelse +} bind def + + +/quicksort.with_keylist { % [ a c b ] [ keylist ] -> [ a b c ] + 4 dict begin + /kg exch def + /arr exch def + /kl << + 0 1 arr length 1 sub { + /i exch def + arr i get + kg i get + } for + >> def + arr { + exch kl exch get exch kl exch get quicksort.cmp + } quicksort.with_comparator +} 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 { + { quicksort.cmp } quicksort.with_comparator +} 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 cmp 0 ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot cmp 0 le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + end +} bind def + +/quicksort.with_comparator { % [ a c b ] { comparator } -> [ a b c ] + 2 dict begin + /cmp exch def + /arr exch def + arr length 0 gt { + 0 arr length 1 sub quicksort.main + } if + arr + end +} bind def + + +/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 + + + +% end included library code + +/shuffle { + (%Calendar%) currentdevparams /Second get srand + dup + [ exch + length { + rand + } repeat + ] + quicksort.with_keylist +} bind def + +/sixoffortynine { + [ 1 1 49 { } for ] + shuffle + 0 6 getinterval + quicksort +} bind def + +sixoffortynine +== diff --git a/challenge-246/roger-bell-west/postscript/ch-2.ps b/challenge-246/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..db96e5e362 --- /dev/null +++ b/challenge-246/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,60 @@ +%!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 + +/linearrecurrencesecondorder { + 0 dict begin + /seq exch def + /a seq 0 3 getinterval def + /b seq 1 3 getinterval def + /c seq 2 3 getinterval def + /q + b 2 get a 0 get mul b 0 get a 2 get mul sub + b 1 get a 0 get mul b 0 get a 1 get mul sub idiv + def + /p a 2 get a 1 get q mul sub a 0 get idiv def + p a 0 get mul q a 1 get mul add a 2 get eq + p b 0 get mul q b 1 get mul add b 2 get eq and + p c 0 get mul q c 1 get mul add c 2 get eq and + end +} bind def + +(linearrecurrencesecondorder) test.start +[1 1 2 3 5] linearrecurrencesecondorder test +[4 2 4 5 7] linearrecurrencesecondorder not test +[4 1 2 -3 8] linearrecurrencesecondorder test +test.end diff --git a/challenge-246/roger-bell-west/python/ch-1.py b/challenge-246/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..c22b2316ef --- /dev/null +++ b/challenge-246/roger-bell-west/python/ch-1.py @@ -0,0 +1,9 @@ +#! /usr/bin/python3 + +import random + +candidates = list(range(1, 50)) +random.shuffle(candidates) +candidates = candidates[0:6] +candidates.sort() +print(candidates) diff --git a/challenge-246/roger-bell-west/python/ch-2.py b/challenge-246/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..aca37cd440 --- /dev/null +++ b/challenge-246/roger-bell-west/python/ch-2.py @@ -0,0 +1,24 @@ +#! /usr/bin/python3 + +def linearrecurrencesecondorder(seq): + a = seq[0:3] + b = seq[1:4] + c = seq[2:5] + q = (b[2] * a[0] - b[0] * a[2]) // (b[1] * a[0] - b[0] * a[1]) + p = (a[2] - a[1] * q) // a[0] + return p * a[0] + q * a[1] == a[2] and p * b[0] + q * b[1] == b[2] and p * c[0] + q * c[1] == c[2] + +import unittest + +class TestLinearrecurrencesecondorder(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(linearrecurrencesecondorder([1, 1, 2, 3, 5]), True, 'example 1') + + def test_ex2(self): + self.assertEqual(linearrecurrencesecondorder([4, 2, 4, 5, 7]), False, 'example 2') + + def test_ex3(self): + self.assertEqual(linearrecurrencesecondorder([4, 1, 2, -3, 8]), True, 'example 3') + +unittest.main() diff --git a/challenge-246/roger-bell-west/raku/ch-1.p6 b/challenge-246/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..7052a67429 --- /dev/null +++ b/challenge-246/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,10 @@ +#! /usr/bin/raku + +sub sixoffortynine { + my @candidates = (1..49); + @candidates = @candidates.pick(6); + @candidates = sort {$^a <=> $^b}, @candidates; + say join(', ', @candidates); +} + +sixoffortynine(); diff --git a/challenge-246/roger-bell-west/raku/ch-2.p6 b/challenge-246/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..27284f867b --- /dev/null +++ b/challenge-246/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,18 @@ +#! /usr/bin/raku + +use Test; + +plan 3; + +is(linearrecurrencesecondorder([1, 1, 2, 3, 5]), True, 'example 1'); +is(linearrecurrencesecondorder([4, 2, 4, 5, 7]), False, 'example 2'); +is(linearrecurrencesecondorder([4, 1, 2, -3, 8]), True, 'example 3'); + +sub linearrecurrencesecondorder(@seq) { + my @a = [@seq[0..2]]; + my @b = [@seq[1..3]]; + my @c = [@seq[2..4]]; + my $q = (@b[2] * @a[0] - @b[0] * @a[2]) div (@b[1] * @a[0] - @b[0] * @a[1]); + my $p = (@a[2] - @a[1] * $q) div @a[0]; + return $p * @a[0] + $q * @a[1] == @a[2] && $p * @b[0] + $q * @b[1] == @b[2] && $p * @c[0] + $q * @c[1] == @c[2]; +} diff --git a/challenge-246/roger-bell-west/ruby/ch-1.rb b/challenge-246/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..0e3af192a2 --- /dev/null +++ b/challenge-246/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,7 @@ +#! /usr/bin/ruby + +candidates = Array.new(49) {|i| i + 1} +candidates.shuffle! +candidates = candidates[0..5] +candidates.sort! +print("#{candidates}\n") diff --git a/challenge-246/roger-bell-west/ruby/ch-2.rb b/challenge-246/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..1e1b31de7a --- /dev/null +++ b/challenge-246/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,31 @@ +#! /usr/bin/ruby + +def linearrecurrencesecondorder(seq) + a = seq[0..2] + b = seq[1..3] + c = seq[2..4] + q = (b[2] * a[0] - b[0] * a[2]) / (b[1] * a[0] - b[0] * a[1]) + p = (a[2] - a[1] * q) / a[0] + return p * a[0] + q * a[1] == a[2] && + p * b[0] + q * b[1] == b[2] && + p * c[0] + q * c[1] == c[2] +end + +require 'test/unit' + +class TestLinearrecurrencesecondorder < Test::Unit::TestCase + + def test_ex1 + assert_equal(true, linearrecurrencesecondorder([1, 1, 2, 3, 5])) + end + + def test_ex2 + assert_equal(false, linearrecurrencesecondorder([4, 2, 4, 5, 7])) + end + + def test_ex3 + assert_equal(true, linearrecurrencesecondorder([4, 1, 2, -3, 8])) + end + +end + diff --git a/challenge-246/roger-bell-west/rust/ch-1.rs b/challenge-246/roger-bell-west/rust/ch-1.rs new file mode 100644 index 0000000000..fbd2de1326 --- /dev/null +++ b/challenge-246/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,11 @@ +use rand::seq::SliceRandom; +use rand::thread_rng; + +fn main() { + let mut candidates = (1..=49).collect::<Vec<u32>>(); + let mut rng = thread_rng(); + candidates.shuffle(&mut rng); + candidates.resize(6, 0); + candidates.sort(); + println!("{:?}", candidates); +} diff --git a/challenge-246/roger-bell-west/rust/ch-2.rs b/challenge-246/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..175f89129c --- /dev/null +++ b/challenge-246/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,28 @@ +#! /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!(linearrecurrencesecondorder(vec![1, 1, 2, 3, 5]), true); +} + +#[test] +fn test_ex2() { + assert_eq!(linearrecurrencesecondorder(vec![4, 2, 4, 5, 7]), false); +} + +#[test] +fn test_ex3() { + assert_eq!(linearrecurrencesecondorder(vec![4, 1, 2, -3, 8]), true); +} + +fn linearrecurrencesecondorder(seq: Vec<i32>) -> bool { + let a = &seq[0..=2]; + let b = &seq[1..=3]; + let c = &seq[2..=4]; + let q = (b[2] * a[0] - b[0] * a[2]) / (b[1] * a[0] - b[0] * a[1]); + let p = (a[2] - a[1] * q) / a[0]; + p * a[0] + q * a[1] == a[2] + && p * b[0] + q * b[1] == b[2] + && p * c[0] + q * c[1] == c[2] +} diff --git a/challenge-246/roger-bell-west/scala/ch-1.scala b/challenge-246/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..35e97f151d --- /dev/null +++ b/challenge-246/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,14 @@ +import scala.util.Random + +object Sixoffortynine { + def sixoffortynine() { + var candidates = List.range(1, 50) + candidates = Random.shuffle(candidates) + candidates = candidates.take(6) + candidates = candidates.sortWith(_ < _) + println(candidates) + } + def main(args: Array[String]) { + sixoffortynine() + } +} diff --git a/challenge-246/roger-bell-west/scala/ch-2.scala b/challenge-246/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..5c7ff10efa --- /dev/null +++ b/challenge-246/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,32 @@ + +object Linearrecurrencesecondorder { + def linearrecurrencesecondorder(seq: List[Int]): Boolean = { + val a = seq.take(3) + val b = seq.take(4).takeRight(3) + val c = seq.take(5).takeRight(3) + val q = (b(2) * a(0) - b(0) * a(2)) / (b(1) * a(0) - b(0) * a(1)) + val p = (a(2) - a(1) * q) / a(0) + return p * a(0) + q * a(1) == a(2) && p * b(0) + q * b(1) == b(2) && p * c(0) + q * c(1) == c(2) + } + def main(args: Array[String]) { + if (linearrecurrencesecondorder(List(1, 1, 2, 3, 5))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (!linearrecurrencesecondorder(List(4, 2, 4, 5, 7))) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (linearrecurrencesecondorder(List(4, 1, 2, -3, 8))) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-246/roger-bell-west/tests.yaml b/challenge-246/roger-bell-west/tests.yaml new file mode 100644 index 0000000000..5922572cf6 --- /dev/null +++ b/challenge-246/roger-bell-west/tests.yaml @@ -0,0 +1,24 @@ +--- +ch-2: + - function: linearrecurrencesecondorder + arguments: + - 1 + - 1 + - 2 + - 3 + - 5 + result: true + - arguments: + - 4 + - 2 + - 4 + - 5 + - 7 + result: false + - arguments: + - 4 + - 1 + - 2 + - -3 + - 8 + result: true |
