diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-15 21:06:34 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-15 21:06:34 +0000 |
| commit | 95cb93418a5409a3a6700676d250ab76ff9c4577 (patch) | |
| tree | f8739d1cc8b88004ebcd406d4aec0c6bcaa354da /challenge-243 | |
| parent | f219f4079bd8a110836b2d16c8f20f158f3f994b (diff) | |
| parent | 8800ae776ec4c9568825319092ec21799b25f281 (diff) | |
| download | perlweeklychallenge-club-95cb93418a5409a3a6700676d250ab76ff9c4577.tar.gz perlweeklychallenge-club-95cb93418a5409a3a6700676d250ab76ff9c4577.tar.bz2 perlweeklychallenge-club-95cb93418a5409a3a6700676d250ab76ff9c4577.zip | |
Merge pull request #9067 from Firedrake/rogerbw-challenge-243
RogerBW solutions for challenge no. 243
Diffstat (limited to 'challenge-243')
21 files changed, 695 insertions, 0 deletions
diff --git a/challenge-243/roger-bell-west/javascript/ch-1.js b/challenge-243/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..a80bc36df8 --- /dev/null +++ b/challenge-243/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,47 @@ +#! /usr/bin/node + +"use strict" + +function combinations(arr, k) { + let c = []; + for (let i = 0; i < k; i++) { + c.push(i); + } + c.push(arr.length); + c.push(0); + let out = []; + while (true) { + let inner = []; + for (let i = k-1; i >= 0; i--) { + inner.push(arr[c[i]]); + } + out.push(inner); + let j = 0; + while (c[j] + 1 == c[j + 1]) { + c[j] = j; + j += 1; + } + if (j >= k) { + break; + } + c[j] += 1; + } + return out; +} + +function reversepairs(a) { + return combinations(a, 2).filter (v => v[1] > 2 * v[0]).length; +} + +if (reversepairs([1, 3, 2, 3, 1]) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (reversepairs([2, 4, 3, 5, 1]) == 3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-243/roger-bell-west/javascript/ch-2.js b/challenge-243/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..2e3e62b832 --- /dev/null +++ b/challenge-243/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,26 @@ +#! /usr/bin/node + +"use strict" + +function floorsum(a) { + let n = 0; + for (let iv of a) { + for (let jv of a) { + n += Math.floor(iv / jv); + } + } + return n; +} + +if (floorsum([2, 5, 9]) == 10) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (floorsum([7, 7, 7, 7, 7, 7, 7]) == 49) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-243/roger-bell-west/kotlin/ch-1.kt b/challenge-243/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..6d520e03e0 --- /dev/null +++ b/challenge-243/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,47 @@ +fun combinations(arr: List<Int>, k: Int): List<List<Int>> { + var c = ArrayList<Int>() + for (i in 0 .. k-1) { + c.add(i) + } + c.add(arr.size) + c.add(0) + var out = ArrayList<List<Int>>() + while (true) { + var inner = ArrayList<Int>() + for (i in k-1 downTo 0) { + inner.add(arr[c[i]]) + } + out.add(inner.toList()) + var j = 0 + while (c[j] + 1 == c[j + 1]) { + c[j] = j + j += 1 + } + if (j >= k) { + break + } + c[j] += 1 + } + return out.toList() +} + +fun reversepairs(a: List<Int>): Int { + return combinations(a, 2).filter {v -> v[1] > 2 * v[0]}.size +} + +fun main() { + + if (reversepairs(listOf(1, 3, 2, 3, 1)) == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (reversepairs(listOf(2, 4, 3, 5, 1)) == 3) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-243/roger-bell-west/kotlin/ch-2.kt b/challenge-243/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..048f1f8ff5 --- /dev/null +++ b/challenge-243/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,26 @@ +fun floorsum(a: List<Int>): Int { + var n = 0 + for (iv in a) { + for (jv in a) { + n += iv / jv + } + } + return n +} + +fun main() { + + if (floorsum(listOf(2, 5, 9)) == 10) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (floorsum(listOf(7, 7, 7, 7, 7, 7, 7)) == 49) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-243/roger-bell-west/lua/ch-1.lua b/challenge-243/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..600295280e --- /dev/null +++ b/challenge-243/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,52 @@ +#! /usr/bin/lua +function combinations(arr, k) + local c = {} + for i = 1, k do + table.insert(c, i) + end + table.insert(c, #arr + 1) + table.insert(c, 0) + local out = {} + while true do + local inner = {} + for i = k, 1, -1 do + table.insert(inner, arr[c[i]]) + end + table.insert(out, inner) + local j = 1 + while c[j] + 1 == c[j + 1] do + c[j] = j + j = j + 1 + end + if j > k then + break + end + c[j] = c[j] + 1 + end + return ipairs(out) +end + +function reversepairs(a) + local n = 0 + for _, x in combinations(a, 2) do + if x[2] > 2 * x[1] then + n = n + 1 + end + end + return n +end + +if reversepairs({1, 3, 2, 3, 1}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if reversepairs({2, 4, 3, 5, 1}) == 3 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-243/roger-bell-west/lua/ch-2.lua b/challenge-243/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..6d6a3ce27b --- /dev/null +++ b/challenge-243/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,26 @@ +#! /usr/bin/lua + +function floorsum(a) + local n = 0 + for _i, iv in ipairs(a) do + for _j, jv in ipairs(a) do + n = n + math.floor(iv / jv) + end + end + return n +end + +if floorsum({2, 5, 9}) == 10 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if floorsum({7, 7, 7, 7, 7, 7, 7}) == 49 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-243/roger-bell-west/perl/ch-1.pl b/challenge-243/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..60474e23f1 --- /dev/null +++ b/challenge-243/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,16 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(reversepairs([1, 3, 2, 3, 1]), 2, 'example 1'); +is(reversepairs([2, 4, 3, 5, 1]), 3, 'example 2'); + +use Algorithm::Combinatorics qw(combinations); + +sub reversepairs($a) { + return scalar grep {$_->[0] > 2 * $_->[1]} combinations($a, 2); +} diff --git a/challenge-243/roger-bell-west/perl/ch-2.pl b/challenge-243/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..ef1fcbb730 --- /dev/null +++ b/challenge-243/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,21 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 2; + +is(floorsum([2, 5, 9]), 10, 'example 1'); +is(floorsum([7, 7, 7, 7, 7, 7, 7]), 49, 'example 2'); + +sub floorsum($a) { + use integer; + my $n = 0; + foreach my $iv (@{$a}) { + foreach my $jv (@{$a}) { + $n += $iv / $jv; + } + } + return $n; +} diff --git a/challenge-243/roger-bell-west/postscript/ch-1.ps b/challenge-243/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..d9033c6dbc --- /dev/null +++ b/challenge-243/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,95 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/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 + +/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 + +/filter { % array proc(bool) -> array + 1 dict begin + /p exch def + [ exch + { + dup p not + { + pop + } if + } forall + ] + end +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + + +% end included library code + +/reversepairs { + 2 combinations + { aload pop exch 2 mul gt } filter + length +} bind def + +(reversepairs) test.start +[1 3 2 3 1] reversepairs 2 eq test +[2 4 3 5 1] reversepairs 3 eq test +test.end diff --git a/challenge-243/roger-bell-west/postscript/ch-2.ps b/challenge-243/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..4f022300e0 --- /dev/null +++ b/challenge-243/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,76 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/reduce { % array proc -> value + 2 dict begin + /p exch def + /a exch def + a 0 get + 1 1 a length 1 sub { + a exch get + p + } for + end +} bind def + +/test.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 + +/map { % array proc -> array + 2 dict begin + /p exch def + [ exch + { + p + } forall + ] + end +} bind def + + +% end included library code + +/floorsum { + 2 dict begin + /a exch def + 0 + a { + /iv exch def + a { iv idiv } map { add } reduce add + } forall + end +} bind def + +(floorsum) test.start +[2 5 9] floorsum 10 eq test +[7 7 7 7 7 7 7] floorsum 49 eq test +test.end diff --git a/challenge-243/roger-bell-west/python/ch-1.py b/challenge-243/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..8e23b80511 --- /dev/null +++ b/challenge-243/roger-bell-west/python/ch-1.py @@ -0,0 +1,18 @@ +#! /usr/bin/python3 + +from itertools import combinations + +def reversepairs(a): + return len([v for v in combinations(a, 2) if v[0] > 2 * v[1]]) + +import unittest + +class TestReversepairs(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(reversepairs([1, 3, 2, 3, 1]), 2, 'example 1') + + def test_ex2(self): + self.assertEqual(reversepairs([2, 4, 3, 5, 1]), 3, 'example 2') + +unittest.main() diff --git a/challenge-243/roger-bell-west/python/ch-2.py b/challenge-243/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..b85c925f4d --- /dev/null +++ b/challenge-243/roger-bell-west/python/ch-2.py @@ -0,0 +1,22 @@ +#! /usr/bin/python3 + +from math import floor + +def floorsum(a: list[int]): + n = int(0) + for iv in a: + for jv in a: + n += floor(iv / jv) + return n + +import unittest + +class TestFloorsum(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(floorsum([2, 5, 9]), 10, 'example 1') + + def test_ex2(self): + self.assertEqual(floorsum([7, 7, 7, 7, 7, 7, 7]), 49, 'example 2') + +unittest.main() diff --git a/challenge-243/roger-bell-west/raku/ch-1.p6 b/challenge-243/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..243ce662d9 --- /dev/null +++ b/challenge-243/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,12 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(reversepairs([1, 3, 2, 3, 1]), 2, 'example 1'); +is(reversepairs([2, 4, 3, 5, 1]), 3, 'example 2'); + +sub reversepairs(@a) { + return @a.combinations(2).grep({ $_[0] > 2 * $_[1]}).elems; +} diff --git a/challenge-243/roger-bell-west/raku/ch-2.p6 b/challenge-243/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..6aa118a5eb --- /dev/null +++ b/challenge-243/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,18 @@ +#! /usr/bin/raku + +use Test; + +plan 2; + +is(floorsum([2, 5, 9]), 10, 'example 1'); +is(floorsum([7, 7, 7, 7, 7, 7, 7]), 49, 'example 2'); + +sub floorsum(@a) { + my $n = 0; + for @a -> $iv { + for @a -> $jv { + $n += floor($iv / $jv); + } + } + return $n; +} diff --git a/challenge-243/roger-bell-west/ruby/ch-1.rb b/challenge-243/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..2d8416edff --- /dev/null +++ b/challenge-243/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,19 @@ +#! /usr/bin/ruby + +def reversepairs(a) + return a.combination(2).find_all{|v| v[0] > 2 * v[1]}.length +end + +require 'test/unit' + +class TestReversepairs < Test::Unit::TestCase + + def test_ex1 + assert_equal(2, reversepairs([1, 3, 2, 3, 1])) + end + + def test_ex2 + assert_equal(3, reversepairs([2, 4, 3, 5, 1])) + end + +end diff --git a/challenge-243/roger-bell-west/ruby/ch-2.rb b/challenge-243/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..e6ab5e5d82 --- /dev/null +++ b/challenge-243/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,25 @@ +#! /usr/bin/ruby + +def floorsum(a) + n = 0 + a.each do |iv| + a.each do |jv| + n += iv.div(jv) + end + end + return n +end + +require 'test/unit' + +class TestFloorsum < Test::Unit::TestCase + + def test_ex1 + assert_equal(10, floorsum([2, 5, 9])) + end + + def test_ex2 + assert_equal(49, floorsum([7, 7, 7, 7, 7, 7, 7])) + end + +end diff --git a/challenge-243/roger-bell-west/rust/ch-1.rs b/challenge-243/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..29693c0961 --- /dev/null +++ b/challenge-243/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,18 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +use itertools::Itertools; + +#[test] +fn test_ex1() { + assert_eq!(reversepairs(vec![1, 3, 2, 3, 1]), 2); +} + +#[test] +fn test_ex2() { + assert_eq!(reversepairs(vec![2, 4, 3, 5, 1]), 3); +} + +fn reversepairs(a: Vec<u32>) -> u32 { + a.iter().combinations(2).filter(|v| *v[0] > 2 * *v[1]).count() as u32 +} diff --git a/challenge-243/roger-bell-west/rust/ch-2.rs b/challenge-243/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..5b985acc8f --- /dev/null +++ b/challenge-243/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,22 @@ +#! /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!(floorsum(vec![2, 5, 9]), 10); +} + +#[test] +fn test_ex2() { + assert_eq!(floorsum(vec![7, 7, 7, 7, 7, 7, 7]), 49); +} + +fn floorsum(a: Vec<u32>) -> u32 { + let mut n = 0; + for iv in &a { + for jv in &a { + n += iv / jv; + } + } + return n; +} diff --git a/challenge-243/roger-bell-west/scala/ch-1.scala b/challenge-243/roger-bell-west/scala/ch-1.scala new file mode 100644 index 0000000000..af9ce27824 --- /dev/null +++ b/challenge-243/roger-bell-west/scala/ch-1.scala @@ -0,0 +1,49 @@ +import scala.collection.mutable.ListBuffer + +object Reversepairs { + def combinations(arr: List[Int], k: Int): List[List[Int]] = { + var c = new ListBuffer[Int]() + for (i <- 0 until k) { + c += i + } + c += arr.length + c += 0 + var out = new ListBuffer[List[Int]]() + var cont = true + while (cont) { + var inner = new ListBuffer[Int]() + for (i <- List.range(k-1, -1, -1)) { + inner += arr(c(i)) + } + out += inner.toList + var j = 0 + while (c(j) + 1 == c(j + 1)) { + c(j) = j + j += 1 + } + if (j >= k) { + cont = false + } else { + c(j) += 1 + } + } + return out.toList + } + def reversepairs(a: List[Int]): Int = { + combinations(a, 2).filter(v => v(1) > 2 * v(0)).length + } + def main(args: Array[String]) { + if (reversepairs(List(1, 3, 2, 3, 1)) == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (reversepairs(List(2, 4, 3, 5, 1)) == 3) { + print("Pass") + } else { + print("Fail") + } + println("") + } +} diff --git a/challenge-243/roger-bell-west/scala/ch-2.scala b/challenge-243/roger-bell-west/scala/ch-2.scala new file mode 100644 index 0000000000..1dca77ad8c --- /dev/null +++ b/challenge-243/roger-bell-west/scala/ch-2.scala @@ -0,0 +1,27 @@ + +object Floorsum { + def floorsum(a: List[Int]): Int = { + var n = 0 + for (iv <- a) { + for (jv <- a) { + n += iv / jv + } + } + return n + } + def main(args: Array[String]) { + if (floorsum(List(2, 5, 9)) == 10) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (floorsum(List(7, 7, 7, 7, 7, 7, 7)) == 49) { + print("Pass") + } else { + print("Fail") + } + println("") + + } +} diff --git a/challenge-243/roger-bell-west/tests.yaml b/challenge-243/roger-bell-west/tests.yaml new file mode 100644 index 0000000000..ba8244d494 --- /dev/null +++ b/challenge-243/roger-bell-west/tests.yaml @@ -0,0 +1,33 @@ +--- +ch-1: + - function: reversepairs + arguments: + - 1 + - 3 + - 2 + - 3 + - 1 + result: 2 + - arguments: + - 2 + - 4 + - 3 + - 5 + - 1 + result: 3 +ch-2: + - function: floorsum + arguments: + - 2 + - 5 + - 9 + result: 10 + - arguments: + - 7 + - 7 + - 7 + - 7 + - 7 + - 7 + - 7 + result: 49 |
