From c5d7bbdf41bd9632858245f3afd93319186a14d9 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 14 Nov 2022 09:33:51 +0000 Subject: Solutions for challenge #191 --- challenge-191/roger-bell-west/javascript/ch-1.js | 36 +++++++ challenge-191/roger-bell-west/javascript/ch-2.js | 62 ++++++++++++ challenge-191/roger-bell-west/kotlin/ch-1.kt | 32 ++++++ challenge-191/roger-bell-west/kotlin/ch-2.kt | 65 ++++++++++++ challenge-191/roger-bell-west/lua/ch-1.lua | 35 +++++++ challenge-191/roger-bell-west/lua/ch-2.lua | 73 ++++++++++++++ challenge-191/roger-bell-west/perl/ch-1.pl | 17 ++++ challenge-191/roger-bell-west/perl/ch-2.pl | 44 +++++++++ challenge-191/roger-bell-west/postscript/ch-1.ps | 121 +++++++++++++++++++++++ challenge-191/roger-bell-west/postscript/ch-2.ps | 116 ++++++++++++++++++++++ challenge-191/roger-bell-west/python/ch-1.py | 25 +++++ challenge-191/roger-bell-west/python/ch-2.py | 43 ++++++++ challenge-191/roger-bell-west/raku/ch-1.p6 | 15 +++ challenge-191/roger-bell-west/raku/ch-2.p6 | 45 +++++++++ challenge-191/roger-bell-west/ruby/ch-1.rb | 27 +++++ challenge-191/roger-bell-west/ruby/ch-2.rb | 56 +++++++++++ challenge-191/roger-bell-west/rust/ch-1.rs | 29 ++++++ challenge-191/roger-bell-west/rust/ch-2.rs | 60 +++++++++++ 18 files changed, 901 insertions(+) create mode 100755 challenge-191/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-191/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-191/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-191/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-191/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-191/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-191/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-191/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-191/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-191/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-191/roger-bell-west/python/ch-1.py create mode 100755 challenge-191/roger-bell-west/python/ch-2.py create mode 100755 challenge-191/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-191/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-191/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-191/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-191/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-191/roger-bell-west/rust/ch-2.rs (limited to 'challenge-191') diff --git a/challenge-191/roger-bell-west/javascript/ch-1.js b/challenge-191/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..7daf99853d --- /dev/null +++ b/challenge-191/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,36 @@ +#! /usr/bin/node + +"use strict" + +function twicelargest(l0) { + let l = [...l0]; + l.sort(function(a,b) { + return b-a; + }); + return l[0] >= l[1] * 2; +} + +if (!twicelargest([1, 2, 3, 4])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (twicelargest([1, 2, 0, 5])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (twicelargest([2, 6, 3, 1])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!twicelargest([4, 5, 2, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-191/roger-bell-west/javascript/ch-2.js b/challenge-191/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..424b4641d5 --- /dev/null +++ b/challenge-191/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,62 @@ +#! /usr/bin/node + +"use strict" + +function cutelist(n) { + let tab = [[false]]; + let t = []; + for (let x = 1; x <= n; x++) { + tab.push(new Array(n+1).fill(false)); + t.push(x); + } + for (let x = 1; x <= n; x++) { + for (let y = 1; y <= x; y++) { + if (x % y != 0 && y % x != 0) { + tab[x][y] = true; + tab[y][x] = true; + } + } + } + let count = 0; + let stackl = [[]]; + let stackc = [t]; + while (stackl.length != 0) { + let l = stackl.pop(); + let c = stackc.pop(); + if (c.length == 0 && l.length == n) { + count++; + } else { + let place = l.length + 1; + for (let candidate of c) { + if (!tab[place][candidate]) { + let q = Array.from(l); + q.push(candidate); + stackl.push(q); + stackc.push(Array.from(c.filter(i => i != candidate))); + } + } + } + } + return count; +} + +if (cutelist(2) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (cutelist(10) == 700) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (cutelist(15) == 24679) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-191/roger-bell-west/kotlin/ch-1.kt b/challenge-191/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..a5b535b011 --- /dev/null +++ b/challenge-191/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,32 @@ +fun twicelargest(l0: List): Boolean { + var l = ArrayList(l0) + l.sort() + return l.last() >= 2*l[l.size - 2] +} + +fun main() { + if (!twicelargest(listOf(1, 2, 3, 4))) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (twicelargest(listOf(1, 2, 0, 5))) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (twicelargest(listOf(2, 6, 3, 1))) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (!twicelargest(listOf(4, 5, 2, 3))) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-191/roger-bell-west/kotlin/ch-2.kt b/challenge-191/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..9c445a2027 --- /dev/null +++ b/challenge-191/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,65 @@ +fun cutelist(n: Int): Int { + var tab = ArrayList>() + tab.add(arrayListOf(false)) + for (x in 1..n) { + var row = ArrayList() + for (y in 1..n+1) { + row.add(false) + } + tab.add(row) + } + for (x in 1..n) { + for (y in 1..x) { + if (x % y != 0 && y % x != 0) { + tab[x][y] = true + tab[y][x] = true + } + } + } + var count = 0 + var stackl = ArrayList>() + stackl.add(arrayListOf()) + var stackc = ArrayList>() + stackc.add(ArrayList((1..n).toList())) + while (stackl.size > 0) { + val l = stackl.last() + stackl = ArrayList(stackl.dropLast(1)) + val c = stackc.last() + stackc = ArrayList(stackc.dropLast(1)) + if (c.size == 0 && l.size == n) { + count += 1 + } else { + val place = l.size + 1 + for (candidate in c) { + if (!tab[place][candidate]) { + var q = ArrayList(l) + q.add(candidate) + stackl.add(q) + stackc.add(ArrayList(c.filter{it != candidate})) + } + } + } + } + return count +} + +fun main() { + if (cutelist(2) == 2) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (cutelist(10) == 700) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (cutelist(15) == 24679) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-191/roger-bell-west/lua/ch-1.lua b/challenge-191/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..67a03fccb7 --- /dev/null +++ b/challenge-191/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,35 @@ +#! /usr/bin/lua + +function twicelargest(l0) + local l = l0 + table.sort(l) + return l[#l] >= 2 * l[#l-1] +end + +if not twicelargest({1, 2, 3, 4}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if twicelargest({1, 2, 0, 5}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if twicelargest({2, 6, 3, 1}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not twicelargest({4, 5, 2, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-191/roger-bell-west/lua/ch-2.lua b/challenge-191/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..f05338a714 --- /dev/null +++ b/challenge-191/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,73 @@ +#! /usr/bin/lua + +function cutelist(n) + local tab = {} + local tmp = {} + for x = 1,n do + local row = {} + for y = 1,n do + table.insert(row, false) + end + table.insert(tab, row) + table.insert(tmp, x) + end + for x = 1,n do + for y = 1,x do + if x % y ~= 0 and y % x ~= 0 then + tab[x][y] = true + tab[y][x] = true + end + end + end + local count = 0 + local stackl = {{}} + local stackc = {tmp} + while #stackl > 0 do + local l = table.remove(stackl) + local c = table.remove(stackc) + if #c == 0 and #l == n then + count = count + 1 + else + local place = #l + 1 + for i, candidate in ipairs(c) do + if not tab[place][candidate] then + local ql = {} + for j, qx in ipairs(l) do + table.insert(ql, qx) + end + table.insert(ql, candidate) + table.insert(stackl, ql) + local qc = {} + for j, qx in ipairs(c) do + if qx ~= candidate then + table.insert(qc, qx) + end + end + table.insert(stackc, qc) + end + end + end + end + return count +end + +if cutelist(2) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if cutelist(10) == 700 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if cutelist(15) == 24679 then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-191/roger-bell-west/perl/ch-1.pl b/challenge-191/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..5a46a22aac --- /dev/null +++ b/challenge-191/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,17 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 4; + +is(twicelargest(1, 2, 3, 4), 0, 'example 1'); +is(twicelargest(1, 2, 0, 5), 1, 'example 2'); +is(twicelargest(2, 6, 3, 1), 1, 'example 3'); +is(twicelargest(4, 5, 2, 3), 0, 'example 4'); + +sub twicelargest(@l0) { + my @l = sort @l0; + return ($l[-1] >= 2*$l[-2])?1:0; +} diff --git a/challenge-191/roger-bell-west/perl/ch-2.pl b/challenge-191/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..e2cf99800a --- /dev/null +++ b/challenge-191/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,44 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(cutelist(2), 2, 'example 1'); +is(cutelist(10), 700, 'example 2'); +is(cutelist(15), 24679, 'example 3'); + +sub cutelist($n) { + my @tab = ([]); + foreach (1..$n) { + push @tab,[(0) x ($n+1)]; + } + foreach my $x (1..$n) { + foreach my $y (1..$x) { + if ($x % $y != 0 && $y % $x != 0) { + $tab[$x][$y] = $tab[$y][$x] = 1; + } + } + } + my $count = 0; + my @stackl = ([]); + my @stackc = ([1..$n]); + while (@stackl) { + my $l = pop @stackl; + my $c = pop @stackc; + if (scalar @{$c} == 0 && scalar @{$l} == $n) { + $count++; + } else { + my $place = scalar @{$l} + 1; + foreach my $candidate (@{$c}) { + unless ($tab[$place][$candidate]) { + push @stackl,[@{$l},$candidate]; + push @stackc,[grep {$_ != $candidate} @{$c}]; + } + } + } + } + return $count; +} diff --git a/challenge-191/roger-bell-west/postscript/ch-1.ps b/challenge-191/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..fd0cebc81c --- /dev/null +++ b/challenge-191/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,121 @@ +%!PS + +% begin included library code +% see https://github.com/Firedrake/postscript-libraries/ +/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.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 { + /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 + +/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 + +/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 { % [ 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 + + +% end included library code + +/twicelargest { + 1 dict begin + quicksort /l exch def + l l length 1 sub get l l length 2 sub get 2 mul ge + end +} bind def + +(twicelargest) test.start +[ 1 2 3 4 ] twicelargest not test +[ 1 2 0 5 ] twicelargest test +[ 2 6 3 1 ] twicelargest test +[ 4 5 2 3 ] twicelargest not test +test.end diff --git a/challenge-191/roger-bell-west/postscript/ch-2.ps b/challenge-191/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..3f7bc021c2 --- /dev/null +++ b/challenge-191/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,116 @@ +%!PS + +% begin included library code +% see https://github.com/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 + +/apop.right { % [a b c] -> [a b] c + [ exch aload length 1 add 1 roll ] exch +} 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 + +/filter { % array proc(bool) -> array + 1 dict begin + /p exch def + [ exch + { + dup p not + { + pop + } if + } forall + ] + end +} bind def + +/apush.right { % [a b] c -> [a b c] + exch + [ exch aload length 2 add -1 roll ] +} bind def + + +% end included library code + +/cutelist { + 11 dict begin + /n exch def + /tab [ + [ ] + n { + [ + false + n { + dup + } repeat + ] + } repeat + ] def + 1 1 n { + /x exch def + 1 1 x { + /y exch def + x y mod 0 ne y x mod 0 ne and { + tab x get y true put + tab y get x true put + } if + } for + } for + /ct 0 def + /stackl [ [ ] ] def + /stackc [ [ 1 1 n {} for ] ] def + { + stackl length 0 eq { + exit + } if + stackl apop.right /l exch def /stackl exch def + stackc apop.right /c exch def /stackc exch def + c length 0 eq l length n eq and { + /ct ct 1 add def + } { + /place l length 1 add def + c { + /candidate exch def + tab place get candidate get not { + /stackl stackl [ l aload pop candidate ] apush.right def + /stackc stackc [ c { candidate ne } filter aload pop ] apush.right def + } if + } forall + } ifelse + } loop + ct + end +} bind def + +(cutelist) test.start +2 cutelist 2 eq test +10 cutelist 700 eq test +15 cutelist 24679 eq test +test.end diff --git a/challenge-191/roger-bell-west/python/ch-1.py b/challenge-191/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..0b0c0c6ba1 --- /dev/null +++ b/challenge-191/roger-bell-west/python/ch-1.py @@ -0,0 +1,25 @@ +#! /usr/bin/python3 + +import unittest +import re + +def twicelargest(l0): + l = l0 + l.sort() + return l[-1] >= (2 * l[-2]) + +class TestTwicelargest(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(twicelargest([1, 2, 3, 4]), False, "example 1") + + def test_ex2(self): + self.assertEqual(twicelargest([1, 2, 0, 5]), True, "example 2") + + def test_ex3(self): + self.assertEqual(twicelargest([2, 6, 3, 1]), True, "example 3") + + def test_ex4(self): + self.assertEqual(twicelargest([4, 5, 2, 3]), False, "example 4") + +unittest.main() diff --git a/challenge-191/roger-bell-west/python/ch-2.py b/challenge-191/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..a06335557f --- /dev/null +++ b/challenge-191/roger-bell-west/python/ch-2.py @@ -0,0 +1,43 @@ +#! /usr/bin/python3 + +import unittest + +def cutelist(n): + tab = [[False]] + for x in range(1, n+1): + tab.append([False] * (n+1)) + for x in range(1, n+1): + for y in range(1, x+1): + if x % y != 0 and y % x != 0: + tab[x][y] = True + tab[y][x] = True + count = 0 + stackl = [[]] + stackc = [[x for x in range(1,n+1)]] + while len(stackl) > 0: + l = stackl.pop() + c = stackc.pop() + if len(c) == 0 and len(l) == n: + count += 1 + else: + place = len(l) + 1 + for candidate in c: + if not tab[place][candidate]: + q = l.copy() + q.append(candidate) + stackl.append(q) + stackc.append([i for i in c if i != candidate]) + return count + +class TestCutelist(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(cutelist(2), 2, 'example 1') + + def test_ex2(self): + self.assertEqual(cutelist(10), 700, 'example 2') + + def test_ex3(self): + self.assertEqual(cutelist(15), 24679, 'example 3') + +unittest.main() diff --git a/challenge-191/roger-bell-west/raku/ch-1.p6 b/challenge-191/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..cd79a8e17d --- /dev/null +++ b/challenge-191/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,15 @@ +#! /usr/bin/perl6 + +use Test; + +plan 4; + +is(twicelargest([1, 2, 3, 4]), False, 'example 1'); +is(twicelargest([1, 2, 0, 5]), True, 'example 2'); +is(twicelargest([2, 6, 3, 1]), True, 'example 3'); +is(twicelargest([4, 5, 2, 3]), False, 'example 4'); + +sub twicelargest(@l0) { + my @l = @l0.sort; + return @l[*-1] >= 2*@l[*-2]; +} diff --git a/challenge-191/roger-bell-west/raku/ch-2.p6 b/challenge-191/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..c8588cf1f8 --- /dev/null +++ b/challenge-191/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,45 @@ +#! /usr/bin/perl6 + +use Test; + +plan 3; + +is(cutelist(2), 2, 'example 1'); +is(cutelist(10), 700, 'example 2'); +is(cutelist(15), 24679, 'example 3'); + +sub cutelist($n) { + my @tab = ([]); + for (1..$n) { + @tab.push([False xx ($n+1)]); + } + for (1..$n) -> $x { + for (1..$x) -> $y { + if ($x % $y != 0 && $y % $x != 0) { + @tab[$x][$y] = @tab[$y][$x] = True; + } + } + } + my $count = 0; + my @stackl = [[],]; + my @stackc = [[1..$n],]; + while (@stackl.elems > 0) { + my @l = @stackl.pop.flat; + my @c = @stackc.pop.flat; + if (@c.elems == 0 && @l.elems == $n) { + $count++; + } else { + my $place = @l.elems + 1; + for @c -> $candidate { + unless (@tab[$place][$candidate]) { + my @q = @l.clone; + @q.push($candidate); + @stackl.push(@q); + my @qc = @c.grep({$_ != $candidate}); + @stackc.push(@qc); + } + } + } + } + return $count; +} diff --git a/challenge-191/roger-bell-west/ruby/ch-1.rb b/challenge-191/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..9aabe23c24 --- /dev/null +++ b/challenge-191/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,27 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def twicelargest(l0) + l = l0.sort + return l[-1] >= (2 * l[-2]) +end + +class TestDivisiblepairs < Test::Unit::TestCase + + def test_ex1 + assert_equal(false, twicelargest([1, 2, 3, 4])) + end + + def test_ex2 + assert_equal(true, twicelargest([1, 2, 0, 5])) + end + + def test_ex3 + assert_equal(true, twicelargest([2, 6, 3, 1])) + end + + def test_ex4 + assert_equal(false, twicelargest([4, 5, 2, 3])) + end +end diff --git a/challenge-191/roger-bell-west/ruby/ch-2.rb b/challenge-191/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..735ac64d30 --- /dev/null +++ b/challenge-191/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,56 @@ +#! /usr/bin/ruby + +require 'test/unit' + +require 'set' + +def cutelist(n) + tab = [[false]] + 1.upto(n) do + tab.push([false] * (n+1)) + end + 1.upto(n) do |x| + 1.upto(x) do |y| + if x % y != 0 && y % x != 0 then + tab[x][y] = true + tab[y][x] = true + end + end + end + count = 0 + stackl = [[]] + stackc = [(1..n).to_a] + while stackl.length > 0 do + l = stackl.pop + c = stackc.pop + if c.length == 0 && l.length == n then + count += 1 + else + place = l.length + 1 + c.each do |candidate| + if !tab[place][candidate] then + q = l[0..-1] + q.push(candidate) + stackl.push(q) + stackc.push(c.reject {|i| i == candidate}) + end + end + end + end + return count +end + +class TestCutelist < Test::Unit::TestCase + + def test_ex1 + assert_equal(2, cutelist(2)); + end + + def test_ex2 + assert_equal(700, cutelist(10)); + end + + def test_ex3 + assert_equal(24679, cutelist(15)); + end +end diff --git a/challenge-191/roger-bell-west/rust/ch-1.rs b/challenge-191/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..12cd0c5e4d --- /dev/null +++ b/challenge-191/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!(twicelargest(vec![1, 2, 3, 4]), false); +} + +#[test] +fn test_ex2() { + assert_eq!(twicelargest(vec![1, 2, 0, 5]), true); +} + +#[test] +fn test_ex3() { + assert_eq!(twicelargest(vec![2, 6, 3, 1]), true); +} + +#[test] +fn test_ex4() { + assert_eq!(twicelargest(vec![4, 5, 2, 3]), false); +} + +fn twicelargest(l0: Vec) -> bool { + let mut l = l0.clone(); + l.sort(); + l.reverse(); + l[0] >= 2 * l[1] +} diff --git a/challenge-191/roger-bell-west/rust/ch-2.rs b/challenge-191/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..25875b2184 --- /dev/null +++ b/challenge-191/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,60 @@ +#! /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!(cutelist(2), 2); +} + +#[test] +fn test_ex2() { + assert_eq!(cutelist(10), 700); +} + +#[test] +fn test_ex3() { + assert_eq!(cutelist(15), 24679); +} + +fn cutelist(n: usize) -> usize { + let mut tab: Vec> = Vec::new(); + tab.push(Vec::new()); + for _ in 1..=n { + tab.push(vec![false; n + 1]); + } + for x in 1..=n { + for y in 1..=x { + if x % y != 0 && y % x != 0 { + tab[x][y] = true; + tab[y][x] = true; + } + } + } + let mut count = 0; + let mut stackl: Vec> = vec![Vec::new()]; + let mut stackc: Vec> = Vec::new(); + stackc.push((1..=n).collect::>()); + while stackl.len() > 0 { + let l = stackl.pop().unwrap(); + let c = stackc.pop().unwrap(); + if c.len() == 0 && l.len() == n { + count += 1; + } else { + let place = l.len() + 1; + for &candidate in &c { + if !tab[place][candidate] { + let mut q = l.clone(); + q.push(candidate); + stackl.push(q); + stackc.push( + c.iter() + .filter(|i| **i != candidate) + .map(|i| *i) + .collect::>(), + ); + } + } + } + } + count +} -- cgit From 576cd01de7fe9418f8e0b85d7d7f9e5fd3b837a9 Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Mon, 14 Nov 2022 18:30:55 +0800 Subject: challenge 191, raku solutions --- challenge-191/feng-chang/raku/ch-1.raku | 6 ++++++ challenge-191/feng-chang/raku/ch-2.raku | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100755 challenge-191/feng-chang/raku/ch-1.raku create mode 100755 challenge-191/feng-chang/raku/ch-2.raku (limited to 'challenge-191') diff --git a/challenge-191/feng-chang/raku/ch-1.raku b/challenge-191/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..cfab573b2e --- /dev/null +++ b/challenge-191/feng-chang/raku/ch-1.raku @@ -0,0 +1,6 @@ +#!/bin/env raku + +unit sub MAIN(*@N); + +my \m = @N.max; +put @N.grep(!(* == m)).any * 2 > m ?? -1 !! 1; diff --git a/challenge-191/feng-chang/raku/ch-2.raku b/challenge-191/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..7d6b0ae4b8 --- /dev/null +++ b/challenge-191/feng-chang/raku/ch-2.raku @@ -0,0 +1,28 @@ +#!/bin/env raku + +unit sub MAIN(UInt:D \n where * > 0); + +sub solve(@S is copy, @s is copy) { + return 0 if +@S == 0; + return 0 if +@S.any == 0; + + #return +@S[0] if +@S == 1; + if +@S == 1 { + put ' ', (|@s, |@S[0]).join(' '); + return +@S[0]; + } + + my \n = @S[0].shift; + my \cnt = solve(@S, @s); + + @S.shift; + @S .= map({ $_.grep(!(* == n)).Array }); + @s.push(n); + + cnt + solve(@S, @s) +} + +my @S = (1..n).map(-> \m { (1..n).grep({ m %% $_ or $_ %% m }).Array }); +put "candidates: {@S.gist}"; + +put solve(@S, []); -- cgit From 3376e1d194b09465ad98302fb838f725ce260926 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 14 Nov 2022 11:42:47 +0100 Subject: Task 1 done --- challenge-191/luca-ferrari/raku/ch-1.p6 | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 challenge-191/luca-ferrari/raku/ch-1.p6 (limited to 'challenge-191') diff --git a/challenge-191/luca-ferrari/raku/ch-1.p6 b/challenge-191/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..7552e06864 --- /dev/null +++ b/challenge-191/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,10 @@ +#!raku + +# Perl Weekly Challenge 191 + +sub MAIN( *@l where { @l.grep( * ~~ Int ).elems == @l.elems } ) { + my $max = @l.max; + my @ll = @l.grep: { $_ == $max || $_ * 2 <= $max }; + '1'.say and exit if @ll.elems == @l.elems; + '-1'.say; +} -- cgit From 36dd7f7ec23a79c31937377af3071eb3e9f358c0 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 14 Nov 2022 11:48:48 +0100 Subject: Task 2 done --- challenge-191/luca-ferrari/raku/ch-2.p6 | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 challenge-191/luca-ferrari/raku/ch-2.p6 (limited to 'challenge-191') diff --git a/challenge-191/luca-ferrari/raku/ch-2.p6 b/challenge-191/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..5090ffa7e6 --- /dev/null +++ b/challenge-191/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,17 @@ +#!raku + +# Perl Weekly Challenge 191 + +sub MAIN( Int $n where { 0 < $n <= 15 } ) { + + my $cute-counter = 0; + for ( 1 .. $n ).List.permutations -> $current-list { + my $is-cute = True; + for 0 ..^ $current-list.elems -> $i { + $is-cute = False and last if $current-list[ $i ] !%% ( $i + 1 ); + } + + $cute-counter++ if $is-cute; + } + $cute-counter.say; +} -- cgit From 6e220c1639c9101b0968622e2ec9e0d4e0babc08 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 14 Nov 2022 11:52:49 +0100 Subject: Task 1 PL/Perl done --- challenge-191/luca-ferrari/postgresql/ch-1.plperl | 36 +++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 challenge-191/luca-ferrari/postgresql/ch-1.plperl (limited to 'challenge-191') diff --git a/challenge-191/luca-ferrari/postgresql/ch-1.plperl b/challenge-191/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..321ae4b3a8 --- /dev/null +++ b/challenge-191/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,36 @@ +-- Perl Weekly Challenge 191 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc191; + + +/* +testdb=> select * from pwc191.task1_plperl( ARRAY[1,2,3,6]::int[] ); + task1_plperl +-------------- + 1 + +*/ +CREATE OR REPLACE FUNCTION +pwc191.task1_plperl( int[] ) +RETURNS int +AS $CODE$ + my ($l) = @_; + + my $max = 0; + + # compute the max element + for ( $l->@* ) { + $max = $_ if ( $max < $_ ); + } + + # iterate on all elements and see + # if one of the is doubly greater than the max + for ( $l->@* ) { + next if $_ == $max; + return -1 if $_ * 2 > $max; + } + + return 1; +$CODE$ +LANGUAGE plperl; -- cgit From 5849b4f33be47bb72e5b705426c5a002b84b41b3 Mon Sep 17 00:00:00 2001 From: Timofey Potapov Date: Mon, 14 Nov 2022 12:04:17 +0100 Subject: Init --- challenge-191/tim-potapov/perl/ch-1.pl | 67 ++++++++++++++++++++++++++++++++++ challenge-191/tim-potapov/perl/ch-2.pl | 48 ++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100755 challenge-191/tim-potapov/perl/ch-1.pl create mode 100755 challenge-191/tim-potapov/perl/ch-2.pl (limited to 'challenge-191') diff --git a/challenge-191/tim-potapov/perl/ch-1.pl b/challenge-191/tim-potapov/perl/ch-1.pl new file mode 100755 index 0000000000..5f1595310b --- /dev/null +++ b/challenge-191/tim-potapov/perl/ch-1.pl @@ -0,0 +1,67 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; + +=pod + +Task 1: Twice Largest +Submitted by: Mohammad S Anwar +You are given list of integers, @list. + +Write a script to find out whether the largest item in the list is at least twice as large as each of the other items. + +Example 1 +Input: @list = (1,2,3,4) +Output: -1 + +The largest in the given list is 4. However 4 is not greater than twice of every remaining elements. +1 x 2 < 4 +2 x 2 > 4 +2 x 3 > 4 +Example 2 +Input: @list = (1,2,0,5) +Output: 1 + +The largest in the given list is 5. Also 5 is greater than twice of every remaining elements. +1 x 2 < 5 +2 x 2 < 5 +0 x 2 < 5 +Example 3 +Input: @list = (2,6,3,1) +Output: 1 + +The largest in the given list is 6. Also 6 is greater than twice of every remaining elements. +2 x 2 < 6 +3 x 2 < 6 +1 x 2 < 6 +Example 4 +Input: @list = (4,5,2,3) +Output: -1 + +The largest in the given list is 5. Also 5 is not greater than twice of every remaining elements. +4 x 2 > 5 +2 x 2 < 5 +3 x 2 > 5 + +=cut + +sub function { + my ( $input ) = @_; + +} + +my @cases = ( + { + Name => 'Example1', + Input => 1, + Output => 1, + }, +); + +for ( @cases ) { + is function($_->{Input} ), $_->{Output}, "$_->{Name} - $_->{Input}"; +} + +done_testing(); diff --git a/challenge-191/tim-potapov/perl/ch-2.pl b/challenge-191/tim-potapov/perl/ch-2.pl new file mode 100755 index 0000000000..74b9d29981 --- /dev/null +++ b/challenge-191/tim-potapov/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; + +=pod + +Task 2: Cute List +Submitted by: Mohammad S Anwar +You are given an integer, 0 < $n <= 15. + +Write a script to find the number of orderings of numbers that form a cute list. + +With an input @list = (1, 2, 3, .. $n) for positive integer $n, an ordering of @list is cute if for every entry, indexed with a base of 1, either + +1) $list[$i] is evenly divisible by $i +or +2) $i is evenly divisible by $list[$i] +Example +Input: $n = 2 +Ouput: 2 + +Since $n = 2, the list can be made up of two integers only i.e. 1 and 2. +Therefore we can have two list i.e. (1,2) and (2,1). + +@list = (1,2) is cute since $list[1] = 1 is divisible by 1 and $list[2] = 2 is divisible by 2. + +=cut + +sub function { + my ( $input ) = @_; + +} + +my @cases = ( + { + Name => 'Example1', + Input => 1, + Output => 1, + }, +); + +for ( @cases ) { + is function($_->{Input} ), $_->{Output}, "$_->{Name} - $_->{Input}"; +} + +done_testing(); -- cgit From 42bcc1fc7889d357b840ec362615a6691fe3828f Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 14 Nov 2022 12:24:00 +0100 Subject: Task 2 PL/Perl done --- challenge-191/luca-ferrari/postgresql/ch-2.plperl | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 challenge-191/luca-ferrari/postgresql/ch-2.plperl (limited to 'challenge-191') diff --git a/challenge-191/luca-ferrari/postgresql/ch-2.plperl b/challenge-191/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..65186d9c40 --- /dev/null +++ b/challenge-191/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,33 @@ +-- Perl Weekly Challenge 191 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc191; + +CREATE OR REPLACE FUNCTION +pwc191.task2_plperl( int ) +RETURNS int +AS $CODE$ + use List::Permute::Limit qw(permute_iter permute); + + my ($n) = @_; + my $cute_counter = 0; + my @l = ( 1 .. $n ); + + my @permutations = permute( items => [ @l ], nitems => $n ); + for my $current_list ( @permutations ) { + + my $is_cute = 1; + for my $i ( 0 .. $current_list->@* ) { + + if ( $current_list->[ $i ] % ( $i + 1 ) != 0 ) { + $is_cute = 0; + last; + } + } + + $cute_counter++ if ( $is_cute ); + } + + return $cute_counter; +$CODE$ +LANGUAGE plperlu; -- cgit From 10c2c4cd953d4f37cd614a0e876f411e13d34ed0 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 14 Nov 2022 13:07:43 +0100 Subject: Task 1 PLPGSQL done --- challenge-191/luca-ferrari/postgresql/ch-1.sql | 33 ++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 challenge-191/luca-ferrari/postgresql/ch-1.sql (limited to 'challenge-191') diff --git a/challenge-191/luca-ferrari/postgresql/ch-1.sql b/challenge-191/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..a1378c4676 --- /dev/null +++ b/challenge-191/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,33 @@ +-- Perl Weekly Challenge 191 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc191; + +CREATE OR REPLACE FUNCTION +pwc191.task1_plpgsql( l int[] ) +RETURNS int +AS $CODE$ +DECLARE + current_max int; + wrong int := 0; +BEGIN + -- compute the max + SELECT max( v ) + INTO current_max + FROM unnest( l ) v; + + SELECT count(*) + INTO wrong + FROM unnest( l ) v + WHERE ( v * 2 ) > current_max + AND v <> current_max; + + IF wrong > 0 THEN + RETURN -1; + ELSE + RETURN 1; + END IF; + +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 8c54539653446bb5cfda62610026588b84997a85 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 14 Nov 2022 13:17:19 +0100 Subject: Task 2 added another missing requirement --- challenge-191/luca-ferrari/postgresql/ch-2.plperl | 11 +++++++++++ challenge-191/luca-ferrari/raku/ch-2.p6 | 8 ++++++++ 2 files changed, 19 insertions(+) (limited to 'challenge-191') diff --git a/challenge-191/luca-ferrari/postgresql/ch-2.plperl b/challenge-191/luca-ferrari/postgresql/ch-2.plperl index 65186d9c40..98cfcd741b 100644 --- a/challenge-191/luca-ferrari/postgresql/ch-2.plperl +++ b/challenge-191/luca-ferrari/postgresql/ch-2.plperl @@ -26,6 +26,17 @@ AS $CODE$ } $cute_counter++ if ( $is_cute ); + + $is_cute = 1; + for my $i ( 0 .. $current_list->@* ) { + + if ( ( $i + 1 ) % $current_list->[ $i ] != 0 ) { + $is_cute = 0; + last; + } + } + + $cute_counter++ if ( $is_cute ); } return $cute_counter; diff --git a/challenge-191/luca-ferrari/raku/ch-2.p6 b/challenge-191/luca-ferrari/raku/ch-2.p6 index 5090ffa7e6..4d5ad6e09a 100644 --- a/challenge-191/luca-ferrari/raku/ch-2.p6 +++ b/challenge-191/luca-ferrari/raku/ch-2.p6 @@ -12,6 +12,14 @@ sub MAIN( Int $n where { 0 < $n <= 15 } ) { } $cute-counter++ if $is-cute; + + $is-cute = True; + for 0 ..^ $current-list.elems -> $i { + $is-cute = False and last if ( $i + 1 ) !%% $current-list[ $i ] ; + } + + $cute-counter++ if $is-cute; + } $cute-counter.say; } -- cgit From d92590b77e0658b591bc539a1ce61e7f03b9d13f Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 14 Nov 2022 13:32:46 +0100 Subject: Task 2 done --- challenge-191/luca-ferrari/postgresql/ch-2.sql | 71 ++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 challenge-191/luca-ferrari/postgresql/ch-2.sql (limited to 'challenge-191') diff --git a/challenge-191/luca-ferrari/postgresql/ch-2.sql b/challenge-191/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..7b4755b34c --- /dev/null +++ b/challenge-191/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,71 @@ +-- Perl Weekly Challenge 191 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc191; + +CREATE OR REPLACE FUNCTION +pwc191.task2_plpgsql( n int ) +RETURNS int +AS $CODE$ +DECLARE + cute_counter int := 0; + i int; + src int[]; + permutation int[]; + is_cute bool; +BEGIN + + FOR i IN 1 .. n LOOP + src = src || i; + END LOOP; + + + + FOR permutation IN + with recursive + data as (select src as arr), + keys as (select generate_subscripts(d.arr, 1) as rn from data d), + cte as ( + select d.arr initial_arr, array[d.arr[k.rn]] new_arr, array[k.rn] used_rn + from data d + cross join keys k + union all + select initial_arr, c.new_arr || c.initial_arr[k.rn], used_rn || k.rn + from cte c + inner join keys k on not (k.rn = any(c.used_rn)) + ) + select new_arr + from cte + WHERE array_length( new_arr, 1 ) = n + LOOP + + is_cute := 1; + FOR i IN 1 .. array_length( permutation, 1 ) LOOP + IF permutation[i] % i <> 0 THEN + is_cute = false; + EXIT; + END IF; + END LOOP; + + IF is_cute THEN + cute_counter := cute_counter + 1; + END IF; + + is_cute := 1; + FOR i IN 1 .. array_length( permutation, 1 ) LOOP + IF i % permutation[i] <> 0 THEN + is_cute = false; + EXIT; + END IF; + END LOOP; + IF is_cute THEN + cute_counter := cute_counter + 1; + END IF; + + END LOOP; + + +RETURN cute_counter; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From e112d5dafc41c51588a90e47e13e887e0dbbf707 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 14 Nov 2022 13:52:23 +0100 Subject: Blog references --- challenge-191/luca-ferrari/blog-1.txt | 1 + challenge-191/luca-ferrari/blog-2.txt | 1 + challenge-191/luca-ferrari/blog-3.txt | 1 + challenge-191/luca-ferrari/blog-4.txt | 1 + challenge-191/luca-ferrari/blog-5.txt | 1 + challenge-191/luca-ferrari/blog-6.txt | 1 + 6 files changed, 6 insertions(+) create mode 100644 challenge-191/luca-ferrari/blog-1.txt create mode 100644 challenge-191/luca-ferrari/blog-2.txt create mode 100644 challenge-191/luca-ferrari/blog-3.txt create mode 100644 challenge-191/luca-ferrari/blog-4.txt create mode 100644 challenge-191/luca-ferrari/blog-5.txt create mode 100644 challenge-191/luca-ferrari/blog-6.txt (limited to 'challenge-191') diff --git a/challenge-191/luca-ferrari/blog-1.txt b/challenge-191/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..d27c64a96b --- /dev/null +++ b/challenge-191/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/11/14/PerlWeeklyChallenge191.html#task1 diff --git a/challenge-191/luca-ferrari/blog-2.txt b/challenge-191/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..27b203fea7 --- /dev/null +++ b/challenge-191/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/11/14/PerlWeeklyChallenge191.html#task2 diff --git a/challenge-191/luca-ferrari/blog-3.txt b/challenge-191/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..d19edb19f3 --- /dev/null +++ b/challenge-191/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/11/14/PerlWeeklyChallenge191.html#task1plperl diff --git a/challenge-191/luca-ferrari/blog-4.txt b/challenge-191/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..d207697f2f --- /dev/null +++ b/challenge-191/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/11/14/PerlWeeklyChallenge191.html#task2plperl diff --git a/challenge-191/luca-ferrari/blog-5.txt b/challenge-191/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..acc191506f --- /dev/null +++ b/challenge-191/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/11/14/PerlWeeklyChallenge191.html#task1plpgsql diff --git a/challenge-191/luca-ferrari/blog-6.txt b/challenge-191/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..957815b1d1 --- /dev/null +++ b/challenge-191/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/11/14/PerlWeeklyChallenge191.html#task2plpgsql -- cgit From 56caf40f5f1d8587e12c2b7757b2b14f0a653643 Mon Sep 17 00:00:00 2001 From: Timofey Potapov Date: Mon, 14 Nov 2022 13:59:37 +0100 Subject: Did 191 --- challenge-191/tim-potapov/perl/ch-1.pl | 88 ++++++++++++++++++--------------- challenge-191/tim-potapov/perl/ch-2.pl | 65 +++++++++++++++++------- challenge-191/tim-potapov/perl/cpanfile | 1 + 3 files changed, 95 insertions(+), 59 deletions(-) create mode 100644 challenge-191/tim-potapov/perl/cpanfile (limited to 'challenge-191') diff --git a/challenge-191/tim-potapov/perl/ch-1.pl b/challenge-191/tim-potapov/perl/ch-1.pl index 5f1595310b..70835fc62d 100755 --- a/challenge-191/tim-potapov/perl/ch-1.pl +++ b/challenge-191/tim-potapov/perl/ch-1.pl @@ -7,61 +7,69 @@ use Test::More; =pod Task 1: Twice Largest -Submitted by: Mohammad S Anwar You are given list of integers, @list. -Write a script to find out whether the largest item in the list is at least twice as large as each of the other items. +Write a script to find out whether the largest item in the list is at +least twice as large as each of the other items. -Example 1 -Input: @list = (1,2,3,4) -Output: -1 - -The largest in the given list is 4. However 4 is not greater than twice of every remaining elements. -1 x 2 < 4 -2 x 2 > 4 -2 x 3 > 4 -Example 2 -Input: @list = (1,2,0,5) -Output: 1 +=cut -The largest in the given list is 5. Also 5 is greater than twice of every remaining elements. -1 x 2 < 5 -2 x 2 < 5 -0 x 2 < 5 -Example 3 -Input: @list = (2,6,3,1) -Output: 1 +sub twice_as_big { + my ( $biggest, $next_biggest ) = sort { $b <=> $a } @_; + $biggest >= 2 * $next_biggest ? 1 : -1; +} -The largest in the given list is 6. Also 6 is greater than twice of every remaining elements. -2 x 2 < 6 -3 x 2 < 6 -1 x 2 < 6 -Example 4 -Input: @list = (4,5,2,3) -Output: -1 +my @cases = ( + { + Name => 'Example 1', + Input => [ 1, 2, 3, 4 ], + Output => -1, -The largest in the given list is 5. Also 5 is not greater than twice of every remaining elements. -4 x 2 > 5 -2 x 2 < 5 -3 x 2 > 5 + # The largest in the given list is 4. However 4 is not greater than + # twice of every remaining elements. + # 1 x 2 < 4 + # 2 x 2 > 4 + # 2 x 3 > 4 + }, + { + Name => 'Example 2', + Input => [ 1, 2, 0, 5 ], + Output => 1, -=cut + # The largest in the given list is 5. Also 5 is greater than twice of + # every remaining elements. + # 1 x 2 < 5 + # 2 x 2 < 5 + # 0 x 2 < 5 -sub function { - my ( $input ) = @_; + }, + { + Name => 'Example 3', + Input => [ 2, 6, 3, 1 ], + Output => 1, -} + # The largest in the given list is 6. Also 6 is greater than twice of + # every remaining elements. + # 2 x 2 < 6 + # 3 x 2 < 6 + # 1 x 2 < 6 -my @cases = ( + }, { - Name => 'Example1', - Input => 1, - Output => 1, + Name => 'Example 4', + Input => [ 4, 5, 2, 3 ], + Output => -1, + + # The largest in the given list is 5. Also 5 is not greater than twice + # of every remaining elements. + # 4 x 2 > 5 + # 2 x 2 < 5 + # 3 x 2 > 5 }, ); for ( @cases ) { - is function($_->{Input} ), $_->{Output}, "$_->{Name} - $_->{Input}"; + is twice_as_big( $_->{Input}->@* ), $_->{Output}, $_->{Name}; } done_testing(); diff --git a/challenge-191/tim-potapov/perl/ch-2.pl b/challenge-191/tim-potapov/perl/ch-2.pl index 74b9d29981..fa0d59b6b3 100755 --- a/challenge-191/tim-potapov/perl/ch-2.pl +++ b/challenge-191/tim-potapov/perl/ch-2.pl @@ -3,46 +3,73 @@ use strict; use warnings; use Test::More; +use Math::Combinatorics qw( permute ); + +#TODO: Remove this debug code !!! +use feature qw(say); +use Mojo::Util qw(dumper); =pod Task 2: Cute List -Submitted by: Mohammad S Anwar You are given an integer, 0 < $n <= 15. Write a script to find the number of orderings of numbers that form a cute list. -With an input @list = (1, 2, 3, .. $n) for positive integer $n, an ordering of @list is cute if for every entry, indexed with a base of 1, either - -1) $list[$i] is evenly divisible by $i -or -2) $i is evenly divisible by $list[$i] -Example -Input: $n = 2 -Ouput: 2 - -Since $n = 2, the list can be made up of two integers only i.e. 1 and 2. -Therefore we can have two list i.e. (1,2) and (2,1). +With an input @list = (1, 2, 3, .. $n) for positive integer $n, +an ordering of @list is cute if for every entry, indexed with a base of 1, either -@list = (1,2) is cute since $list[1] = 1 is divisible by 1 and $list[2] = 2 is divisible by 2. + 1) $list[$i] is evenly divisible by $i + or + 2) $i is evenly divisible by $list[$i] =cut -sub function { - my ( $input ) = @_; +sub number_of_orderings { + my ( $n ) = @_; + my @lists = permute( 1 .. $n ); + my %orderings; + LIST: + for my $list ( @lists ) { + while ( my ( $i, $entry ) = each @$list ) { + my $check1 = $list->[$i] % ( $i + 1 ) == 0; + my $check2 = ( $i + 1 ) % $list->[$i] == 0; + next LIST unless $check1 or $check2; + } + $orderings{"@$list"}++; + } + + %orderings; } my @cases = ( { - Name => 'Example1', - Input => 1, - Output => 1, + Name => 'Example 1', + Input => 2, + Output => 2, + + # Since $n = 2, the list can be made up of two integers only i.e. 1 and 2. + # Therefore we can have two list i.e. (1,2) and (2,1). + # @list = (1,2) is cute since + # $list[1] = 1 is divisible by 1 and $list[2] = 2 is divisible by 2. + + }, + { + Name => 'Example 2', + Input => 3, + Output => 3, + }, + { + Name => 'Example 2', + Input => 4, + Output => 8, }, ); for ( @cases ) { - is function($_->{Input} ), $_->{Output}, "$_->{Name} - $_->{Input}"; + is number_of_orderings( $_->{Input} ), $_->{Output}, + "$_->{Name} - $_->{Input}"; } done_testing(); diff --git a/challenge-191/tim-potapov/perl/cpanfile b/challenge-191/tim-potapov/perl/cpanfile new file mode 100644 index 0000000000..752340d56f --- /dev/null +++ b/challenge-191/tim-potapov/perl/cpanfile @@ -0,0 +1 @@ +require 'Math::Combinatorics'; -- cgit From de8d3b8c378329a52048c48ac1d0ccefc8ce6572 Mon Sep 17 00:00:00 2001 From: Scimon Date: Mon, 14 Nov 2022 13:12:02 +0000 Subject: Challenges 1 and 2 --- challenge-191/simon-proctor/raku/ch-1.raku | 28 ++++++++++++++ challenge-191/simon-proctor/raku/ch-2.raku | 60 ++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 challenge-191/simon-proctor/raku/ch-1.raku create mode 100644 challenge-191/simon-proctor/raku/ch-2.raku (limited to 'challenge-191') diff --git a/challenge-191/simon-proctor/raku/ch-1.raku b/challenge-191/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..69bdfa6e97 --- /dev/null +++ b/challenge-191/simon-proctor/raku/ch-1.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku + +multi sub MAIN ( "TEST" ) is hidden-from-USAGE { + use Test; + ok ! passes( ( 1,2,3,4 ) ); + ok passes( ( 1,2,0,5 ) ); + ok passes( ( 2,6,3,1 ) ); + ok ! passes( ( 4,5,2,3 ) ); + ok ! passes( ( |(1..1000000) ) ); + ok passes( ( |(1..1000000), 2000000 ) ); + ok passes( ( |(1..500000), 2000000, |(510000..1000000) ) ); + + done-testing; +} + +multi sub passes ( @list ) { + my @sorted = @list.sort; + return @sorted[*-1] >= @sorted[*-2] * 2; +} + +#|( Given a list of ints returns true if the largest element in the list + Is at least twice the size of the next largest element + Returns True or False) +multi sub MAIN( + *@list where { *.all ~~ IntStr } #= List of ints to check +) { + +} diff --git a/challenge-191/simon-proctor/raku/ch-2.raku b/challenge-191/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..d6dfe05e9a --- /dev/null +++ b/challenge-191/simon-proctor/raku/ch-2.raku @@ -0,0 +1,60 @@ +#!/usr/bin/env raku + +subset ValidRange of Int where 0 < * <= 15; +subset LargeRange of Int where 9 < * <= 15; +subset EmptyList of List where *.elems == 0; +subset NonEmptyList of List where *.elems > 0; + +#|( Given an int between 1 and 15 return the number of cute lists can be + made from the range 1..$n ) +multi sub MAIN( ValidRange() $n ) { + say cute-count( $n ); +} + +#|( Prints the list of cute counts for all values between 1 and 15) +multi sub MAIN() { + for ( 1..15 ) { + say "$_ : {cute-count($_)}"; + } +} + +multi sub MAIN( "TEST" ) is hidden-from-USAGE { + use Test; + is cute-count(2), 2; + is cute-count(3), 3; + is cute-count(4), 8; + is cute-count(5), 10; + is cute-count(10), 700; + done-testing; +} + +multi sub cute-count( ValidRange $n ) { + my @list = 1..$n; + if ( $n >= 10 ) { + return [+] ( ^@list ).hyper.map( { + possible-cute( 1, @list[$_], (|@list[0..$_-1], |@list[$_+1..*]) ); + }); + } else { + return [+] ( ^@list ).map( { + possible-cute( 1, @list[$_], (|@list[0..$_-1], |@list[$_+1..*]) ); + }); + } +} + +multi sub possible-cute( $idx is copy, $val is copy, @rest ) { + return 0 unless $idx %% $val || $val %% $idx; + my $next = $idx+1; + if ( @rest.elems > 10 ) { + return [+] ( ^(@rest.elems) ).hyper.map( { + possible-cute( $next, @rest[$_], (|@rest[0..$_-1], |@rest[$_+1..*]) ) + }); + } elsif ( @rest.elems ) { + return [+] ( ^(@rest.elems) ).map( { + possible-cute( $next, @rest[$_], (|@rest[0..$_-1], |@rest[$_+1..*]) ) + }); + } + return 1; +} + + + -- cgit From 2d35fafadd17050147947061940098f238e5e6dd Mon Sep 17 00:00:00 2001 From: Scimon Date: Mon, 14 Nov 2022 13:13:06 +0000 Subject: Fix --- challenge-191/simon-proctor/raku/ch-1.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-191') diff --git a/challenge-191/simon-proctor/raku/ch-1.raku b/challenge-191/simon-proctor/raku/ch-1.raku index 69bdfa6e97..7fdeb0c10e 100644 --- a/challenge-191/simon-proctor/raku/ch-1.raku +++ b/challenge-191/simon-proctor/raku/ch-1.raku @@ -24,5 +24,5 @@ multi sub passes ( @list ) { multi sub MAIN( *@list where { *.all ~~ IntStr } #= List of ints to check ) { - + return passes( @list ); } -- cgit From a3a37f596f2e306493de2ecdf3d7311dfb53d93e Mon Sep 17 00:00:00 2001 From: Scimon Date: Mon, 14 Nov 2022 13:26:05 +0000 Subject: Calculate 1-15 in the time it takes to do 15 --- challenge-191/simon-proctor/raku/ch-2.raku | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'challenge-191') diff --git a/challenge-191/simon-proctor/raku/ch-2.raku b/challenge-191/simon-proctor/raku/ch-2.raku index d6dfe05e9a..e790d9e2fb 100644 --- a/challenge-191/simon-proctor/raku/ch-2.raku +++ b/challenge-191/simon-proctor/raku/ch-2.raku @@ -2,8 +2,6 @@ subset ValidRange of Int where 0 < * <= 15; subset LargeRange of Int where 9 < * <= 15; -subset EmptyList of List where *.elems == 0; -subset NonEmptyList of List where *.elems > 0; #|( Given an int between 1 and 15 return the number of cute lists can be made from the range 1..$n ) @@ -13,9 +11,13 @@ multi sub MAIN( ValidRange() $n ) { #|( Prints the list of cute counts for all values between 1 and 15) multi sub MAIN() { - for ( 1..15 ) { - say "$_ : {cute-count($_)}"; + my @checks; + for (1..15) { + my $a = $_; + @checks.push( start { "$a : {cute-count($_)}" } ); } + await @checks; + say $_.result for @checks; } multi sub MAIN( "TEST" ) is hidden-from-USAGE { -- cgit From 1946d28ba075c5ba45fc0cd36bf3ae0ee702fc59 Mon Sep 17 00:00:00 2001 From: Humberto Massa Date: Mon, 14 Nov 2022 14:17:24 -0300 Subject: my solutions to challenge 191 --- challenge-191/massa/raku/ch-1.raku | 34 ++++++++++++++++++++++++++++++ challenge-191/massa/raku/ch-2.raku | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 challenge-191/massa/raku/ch-1.raku create mode 100644 challenge-191/massa/raku/ch-2.raku (limited to 'challenge-191') diff --git a/challenge-191/massa/raku/ch-1.raku b/challenge-191/massa/raku/ch-1.raku new file mode 100644 index 0000000000..feed9b8b02 --- /dev/null +++ b/challenge-191/massa/raku/ch-1.raku @@ -0,0 +1,34 @@ + + +#!/usr/bin/env raku + +=begin pod + +Week 191: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-191 + +Task #1: Twice Largest + + You are given list of integers, @list. + + Write a script to find out whether the largest item in the list is at least + twice as large as each of the other items. + +=end pod + +use Test; + +is False, twice-largest <1 2 3 4>; +is True, twice-largest <1 2 0 5>; +is True, twice-largest <2 6 3 1>; +is False, twice-largest <4 5 2 3>; + +done-testing; + +sub twice-largest(@list) { + my @li = @list».Int.sort(-*); + [&&] @li[0] X≥ @li[1..*].map(2 × *) +} + + diff --git a/challenge-191/massa/raku/ch-2.raku b/challenge-191/massa/raku/ch-2.raku new file mode 100644 index 0000000000..9bfe7b51a8 --- /dev/null +++ b/challenge-191/massa/raku/ch-2.raku @@ -0,0 +1,43 @@ + + +#!/usr/bin/env raku + +=begin pod + +Week 191: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-191 + +Task #2: Cute List + + You are given an integer, 0 < $n <= 15. + + Write a script to find the number of orderings of numbers that form a cute list. + + With an input @list = (1, 2, 3, .. $n) for positive integer $n, an ordering of @list is cute if for every entry, indexed with a base of 1, either + +=begin code + + 1) $list[$i] is evenly divisible by $i + or + 2) $i is evenly divisible by $list[$i] + +=end code + +=end pod + +use Test; + +is 2, cute-list 2; +is 3, cute-list 3; +is 8, cute-list 4; + +done-testing; + +subset Valid of Int where 0 < * ≤ 15; + +sub cute-list(Valid \n) { + + (1 .. n).permutations.grep: -> @list { + @list.pairs.map({ (.key + 1) %% .value || .value %% (.key + 1) }).all.so + } +} -- cgit From 141af548ffbc41d9aa27c4263b93ba87bf1a151b Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 14 Nov 2022 17:38:48 +0000 Subject: w191 - Task 1 & 2 --- challenge-191/perlboy1967/perl/ch-1.pl | 44 ++++++++++++++++++++++ challenge-191/perlboy1967/perl/ch-2.pl | 67 ++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100755 challenge-191/perlboy1967/perl/ch-1.pl create mode 100755 challenge-191/perlboy1967/perl/ch-2.pl (limited to 'challenge-191') diff --git a/challenge-191/perlboy1967/perl/ch-1.pl b/challenge-191/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..96b2fc993b --- /dev/null +++ b/challenge-191/perlboy1967/perl/ch-1.pl @@ -0,0 +1,44 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 191 + - https://theweeklychallenge.org/blog/perl-weekly-challenge-191/#TASK1 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Twice Largest +Submitted by: Mohammad S Anwar + +You are given list of integers, @list. + +Write a script to find out whether the largest item in the list is at least twice +as large as each of the other items. + +=cut + +use v5.16; +use warnings; + +use Test::More; +use List::Util qw(max); +use List::MoreUtils qw(all none firstidx); + + +sub twiceLargest { + return -1 if @_ < 2; + + my $max = max(@_); + + return ((firstidx { $max < ($_ << 1) } grep { $_ != $max } @_) == -1 ? 1 : -1); +} + + +is(twiceLargest(1,2,3,4),-1); +is(twiceLargest(1,2,0,5),1); +is(twiceLargest(2,6,3,1),1); +is(twiceLargest(1),-1); +is(twiceLargest(1,2),1); +is(twiceLargest(),-1); + +done_testing; diff --git a/challenge-191/perlboy1967/perl/ch-2.pl b/challenge-191/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..47d9a9fbfc --- /dev/null +++ b/challenge-191/perlboy1967/perl/ch-2.pl @@ -0,0 +1,67 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 191 + - https://theweeklychallenge.org/blog/perl-weekly-challenge-191/#TASK2 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Cute List +Submitted by: Mohammad S Anwar + +You are given an integer, 0 < $n <= 15. + +Write a script to find the number of orderings of numbers that form a cute list. + +With an input @list = (1, 2, 3, .. $n) for positive integer $n, an ordering of @list +is cute if for every entry, indexed with a base of 1, either + +1) $list[$i] is evenly divisible by $i +or +2) $i is evenly divisible by $list[$i] + +=cut + +use v5.16; +use warnings; + +use Time::HiRes qw(gettimeofday tv_interval); +use Algorithm::Permute; +use List::MoreUtils qw(firstidx); +use Memoize; + +memoize('isCuteEntry'); + +use Data::Printer output => 'stdout'; + +sub isCuteEntry ($$) { + state $c; + my $idx = $_[0].'|'.$_[1]; + + return $c->{$idx} if defined $c->{$idx}; + + $c->{$idx} = (($_[0] % $_[1]) != 0 and ($_[1] % $_[0]) != 0) ? 1 : 0; + + return $c->{$idx}; +} + +sub nCuteLists { + my $n = 0; + my $m = 0; + my $p = Algorithm::Permute->new([1 .. $_[0]]); + while (my @l = $p->next) { + $m++; + my $i = 1; + $n++ if ((firstidx { isCuteEntry($l[$i-1],$i++) } @l) == -1); + } + + return "$n / $m"; +} + +# Note, testing up to 12 because of time lengthy + +for (1..12) { + my $t0 = [gettimeofday]; + printf "nCuteLists($_) = %s (in %f seconds)\n", nCuteLists($_), tv_interval ($t0); +} -- cgit From 75cb8f3e380542635f86f5607c8680df788e7f5d Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 14 Nov 2022 17:46:19 +0000 Subject: Task 2 - Remove 'memoize' (already done by state variable) --- challenge-191/perlboy1967/perl/ch-2.pl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'challenge-191') diff --git a/challenge-191/perlboy1967/perl/ch-2.pl b/challenge-191/perlboy1967/perl/ch-2.pl index 47d9a9fbfc..ab62c2b6ec 100755 --- a/challenge-191/perlboy1967/perl/ch-2.pl +++ b/challenge-191/perlboy1967/perl/ch-2.pl @@ -29,11 +29,7 @@ use warnings; use Time::HiRes qw(gettimeofday tv_interval); use Algorithm::Permute; use List::MoreUtils qw(firstidx); -use Memoize; -memoize('isCuteEntry'); - -use Data::Printer output => 'stdout'; sub isCuteEntry ($$) { state $c; @@ -46,6 +42,7 @@ sub isCuteEntry ($$) { return $c->{$idx}; } + sub nCuteLists { my $n = 0; my $m = 0; @@ -59,6 +56,7 @@ sub nCuteLists { return "$n / $m"; } + # Note, testing up to 12 because of time lengthy for (1..12) { -- cgit From b167d386695f640eef4e85271195a90601ae19a9 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 14 Nov 2022 17:49:14 +0000 Subject: Cleanup --- challenge-191/perlboy1967/perl/ch-2.pl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'challenge-191') diff --git a/challenge-191/perlboy1967/perl/ch-2.pl b/challenge-191/perlboy1967/perl/ch-2.pl index ab62c2b6ec..43a76f2a7b 100755 --- a/challenge-191/perlboy1967/perl/ch-2.pl +++ b/challenge-191/perlboy1967/perl/ch-2.pl @@ -33,6 +33,7 @@ use List::MoreUtils qw(firstidx); sub isCuteEntry ($$) { state $c; + my $idx = $_[0].'|'.$_[1]; return $c->{$idx} if defined $c->{$idx}; @@ -44,8 +45,8 @@ sub isCuteEntry ($$) { sub nCuteLists { - my $n = 0; - my $m = 0; + my ($n,$m) = (0,0); + my $p = Algorithm::Permute->new([1 .. $_[0]]); while (my @l = $p->next) { $m++; -- cgit From fe96a7540a9afbcea6fc0c69a9469b568ca1c431 Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Mon, 14 Nov 2022 18:30:06 +0000 Subject: Week 191 stuff --- challenge-191/peter-campbell-smith/blog.txt | 1 + challenge-191/peter-campbell-smith/perl/ch-1.pl | 35 ++++++++++++++ challenge-191/peter-campbell-smith/perl/ch-2.pl | 62 +++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 challenge-191/peter-campbell-smith/blog.txt create mode 100755 challenge-191/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-191/peter-campbell-smith/perl/ch-2.pl (limited to 'challenge-191') diff --git a/challenge-191/peter-campbell-smith/blog.txt b/challenge-191/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..4721d679a0 --- /dev/null +++ b/challenge-191/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +https://pjcs-pwc.blogspot.com/2022/11/the-twice-largest-and-number-of-cuties.html diff --git a/challenge-191/peter-campbell-smith/perl/ch-1.pl b/challenge-191/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..8ce627323f --- /dev/null +++ b/challenge-191/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2022-11-14 +# PWC 191 task 1 + +use v5.28; +use utf8; +use warnings; + +# You are given list of integers, @list. Write a script to find out whether the largest item in the list +# is at least twice as large as each of the other items. + +# Blog: https://pjcs-pwc.blogspot.com/2022/11/the-twice-largest-and-number-of-cuties.html + +my (@tests, $test, @sorted, $largest, $second, $this); + +@tests = ([1, 2, 3, 4], [1, 2, 0, 5], [2, 6, 3, 1], [4, 5, 2, 3], [1, 5, 16, 28, 35, 44, 50, 61, 78, 83, 99, 200]); + +# loop over tests +while ($test = shift @tests) { + + # method A + @sorted = reverse sort {$a <=> $b} @$test; + say qq[\nInput: \@list = (] . join(', ', @$test) . qq[)\nOu