aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoger Bell_West <roger@firedrake.org>2023-02-08 13:02:28 +0000
committerRoger Bell_West <roger@firedrake.org>2023-02-08 13:02:28 +0000
commitffe0e0ab4bb1ee3507798bd774b805a4d088d3e1 (patch)
treee5234a6460accab39b92535a16887405087e59ec
parentf92e84261b474f81c014f4982268d6e2797b66d9 (diff)
downloadperlweeklychallenge-club-ffe0e0ab4bb1ee3507798bd774b805a4d088d3e1.tar.gz
perlweeklychallenge-club-ffe0e0ab4bb1ee3507798bd774b805a4d088d3e1.tar.bz2
perlweeklychallenge-club-ffe0e0ab4bb1ee3507798bd774b805a4d088d3e1.zip
RogerBW solutions for challenge no. 203
-rwxr-xr-xchallenge-203/roger-bell-west/javascript/ch-1.js38
-rw-r--r--challenge-203/roger-bell-west/kotlin/ch-1.kt38
-rwxr-xr-xchallenge-203/roger-bell-west/lua/ch-1.lua39
-rwxr-xr-xchallenge-203/roger-bell-west/perl/ch-1.pl24
-rwxr-xr-xchallenge-203/roger-bell-west/perl/ch-2.pl24
-rw-r--r--challenge-203/roger-bell-west/postscript/ch-1.ps65
-rwxr-xr-xchallenge-203/roger-bell-west/python/ch-1.py25
-rwxr-xr-xchallenge-203/roger-bell-west/raku/ch-1.p619
-rwxr-xr-xchallenge-203/roger-bell-west/raku/ch-2.p621
-rwxr-xr-xchallenge-203/roger-bell-west/ruby/ch-1.rb29
-rwxr-xr-xchallenge-203/roger-bell-west/rust/ch-1.rs29
-rw-r--r--challenge-203/roger-bell-west/tests.yaml25
12 files changed, 376 insertions, 0 deletions
diff --git a/challenge-203/roger-bell-west/javascript/ch-1.js b/challenge-203/roger-bell-west/javascript/ch-1.js
new file mode 100755
index 0000000000..03a1ece461
--- /dev/null
+++ b/challenge-203/roger-bell-west/javascript/ch-1.js
@@ -0,0 +1,38 @@
+#! /usr/bin/node
+
+"use strict"
+
+function specialquads(l) {
+ let ct = 0;
+ for (let ai = 0; ai < l.length-3; ai++) {
+ for (let bi = ai + 1; bi < l.length-2; bi++) {
+ for (let ci = bi + 1; ci < l.length-1; ci++) {
+ for (let di = ci + 1; di < l.length; di++) {
+ if (l[ai] + l[bi] + l[ci] == l[di]) {
+ ct++;
+ }
+ }
+ }
+ }
+ }
+ return ct;
+}
+
+if (specialquads([1, 2, 3, 6]) == 1) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (specialquads([1, 1, 1, 3, 5]) == 4) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (specialquads([3, 3, 6, 4, 5]) == 0) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write("\n");
diff --git a/challenge-203/roger-bell-west/kotlin/ch-1.kt b/challenge-203/roger-bell-west/kotlin/ch-1.kt
new file mode 100644
index 0000000000..49e28c1b16
--- /dev/null
+++ b/challenge-203/roger-bell-west/kotlin/ch-1.kt
@@ -0,0 +1,38 @@
+fun specialquads(l: List<Int>): Int {
+ var ct = 0
+ for (ai in 0..l.size-4) {
+ for (bi in ai+1..l.size-3) {
+ for (ci in bi+1..l.size-2) {
+ for (di in ci+1..l.size-1) {
+ if (l[ai] + l[bi] + l[ci] == l[di]) {
+ ct += 1
+ }
+ }
+ }
+ }
+ }
+ return ct
+}
+
+fun main() {
+
+ if (specialquads(listOf(1, 2, 3, 6)) == 1) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ print(" ")
+ if (specialquads(listOf(1, 1, 1, 3, 5)) == 4) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ print(" ")
+ if (specialquads(listOf(3, 3, 6, 4, 5)) == 0) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ println("")
+
+}
diff --git a/challenge-203/roger-bell-west/lua/ch-1.lua b/challenge-203/roger-bell-west/lua/ch-1.lua
new file mode 100755
index 0000000000..6e103fe067
--- /dev/null
+++ b/challenge-203/roger-bell-west/lua/ch-1.lua
@@ -0,0 +1,39 @@
+#! /usr/bin/lua
+
+function specialquads(l)
+ local ct = 0
+ for ai = 1,#l - 3 do
+ for bi = ai + 1,#l - 2 do
+ for ci = bi + 1,#l - 1 do
+ for di = ci + 1,#l do
+ if l[ai] + l[bi] + l[ci] == l[di] then
+ ct = ct + 1
+ end
+ end
+ end
+ end
+ end
+ return ct
+end
+
+if specialquads({1, 2, 3, 6}) == 1 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if specialquads({1, 1, 1, 3, 5}) == 4 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if specialquads({3, 3, 6, 4, 5}) == 0 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")
+
diff --git a/challenge-203/roger-bell-west/perl/ch-1.pl b/challenge-203/roger-bell-west/perl/ch-1.pl
new file mode 100755
index 0000000000..9662b83930
--- /dev/null
+++ b/challenge-203/roger-bell-west/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+use experimental 'signatures';
+
+use Test::More tests => 3;
+
+is(specialquads([1, 2, 3, 6]), 1, 'example 1');
+is(specialquads([1, 1, 1, 3, 5]), 4, 'example 2');
+is(specialquads([3, 3, 6, 4, 5]), 0, 'example 3');
+
+use Algorithm::Combinatorics qw(combinations);
+
+sub specialquads($l) {
+ my $ct = 0;
+ my $i = combinations($l, 4);
+ while (my $c = $i->next) {
+ if ($c->[0] + $c->[1] + $c->[2] == $c->[3]) {
+ $ct++;
+ }
+ }
+ return $ct;
+}
diff --git a/challenge-203/roger-bell-west/perl/ch-2.pl b/challenge-203/roger-bell-west/perl/ch-2.pl
new file mode 100755
index 0000000000..b256c28207
--- /dev/null
+++ b/challenge-203/roger-bell-west/perl/ch-2.pl
@@ -0,0 +1,24 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+use experimental 'signatures';
+
+use File::Find;
+
+sub copydir($src, $dst) {
+ my @ds;
+ my $l = 1 + length($src);
+ find(sub{
+ if (-d $_) {
+ push @ds,substr($File::Find::name, $l);
+ }
+ }, $src);
+ foreach my $d (@ds) {
+ if (defined $d && length($d) > 0) {
+ mkdir "$dst/$d";
+ }
+ }
+}
+
+copydir('a/b/c', 'x/y');
diff --git a/challenge-203/roger-bell-west/postscript/ch-1.ps b/challenge-203/roger-bell-west/postscript/ch-1.ps
new file mode 100644
index 0000000000..1a506e3e92
--- /dev/null
+++ b/challenge-203/roger-bell-west/postscript/ch-1.ps
@@ -0,0 +1,65 @@
+%!PS
+
+% begin included library code
+% see https://codeberg.org/Firedrake/postscript-libraries/
+/test.end {
+ ( ) print
+ test.count 0 gt {
+ (Passed ) print
+ test.pass (...) cvs print
+ (/) print
+ test.count (...) cvs print
+ ( \() print
+ test.pass 100 mul test.count idiv (...) cvs print
+ (%\)) print
+ (\r\n) print
+ } if
+} bind def
+
+/test.start {
+ print (:) print
+ /test.pass 0 def
+ /test.count 0 def
+} bind def
+
+/test {
+ /test.count test.count 1 add def
+ {
+ /test.pass test.pass 1 add def
+ } {
+ ( ) print
+ test.count (....) cvs print
+ (-fail) print
+ } ifelse
+} bind def
+
+
+% end included library code
+
+/specialquads {
+ 5 dict begin
+ /l exch def
+ 0
+ 0 1 l length 4 sub {
+ /ai exch def
+ ai 1 add 1 l length 3 sub {
+ /bi exch def
+ bi 1 add 1 l length 2 sub {
+ /ci exch def
+ ci 1 add 1 l length 1 sub {
+ /di exch def
+ l ai get l bi get add l ci get add l di get eq {
+ 1 add
+ } if
+ } for
+ } for
+ } for
+ } for
+ end
+} bind def
+
+(specialquads) test.start
+[1 2 3 6] specialquads 1 eq test
+[1 1 1 3 5] specialquads 4 eq test
+[3 3 6 4 5] specialquads 0 eq test
+test.end
diff --git a/challenge-203/roger-bell-west/python/ch-1.py b/challenge-203/roger-bell-west/python/ch-1.py
new file mode 100755
index 0000000000..e2682c49a9
--- /dev/null
+++ b/challenge-203/roger-bell-west/python/ch-1.py
@@ -0,0 +1,25 @@
+#! /usr/bin/python3
+
+import unittest
+
+from itertools import combinations
+
+def specialquads(l):
+ ct = 0
+ for c in combinations(l, 4):
+ if c[0] + c[1] + c[2] == c[3]:
+ ct += 1
+ return ct
+
+class TestSpecialquads(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(specialquads([1, 2, 3, 6]), 1, 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(specialquads([1, 1, 1, 3, 5]), 4, 'example 2')
+
+ def test_ex3(self):
+ self.assertEqual(specialquads([3, 3, 6, 4, 5]), 0, 'example 3')
+
+unittest.main()
diff --git a/challenge-203/roger-bell-west/raku/ch-1.p6 b/challenge-203/roger-bell-west/raku/ch-1.p6
new file mode 100755
index 0000000000..9b0e6d1135
--- /dev/null
+++ b/challenge-203/roger-bell-west/raku/ch-1.p6
@@ -0,0 +1,19 @@
+#! /usr/bin/raku
+
+use Test;
+
+plan 3;
+
+is(specialquads([1, 2, 3, 6]), 1, 'example 1');
+is(specialquads([1, 1, 1, 3, 5]), 4, 'example 2');
+is(specialquads([3, 3, 6, 4, 5]), 0, 'example 3');
+
+sub specialquads(@l) {
+ my $ct = 0;
+ for @l.combinations(4) -> @c {
+ if (@c[0] + @c[1] + @c[2] == @c[3]) {
+ $ct++;
+ }
+ }
+ return $ct;
+}
diff --git a/challenge-203/roger-bell-west/raku/ch-2.p6 b/challenge-203/roger-bell-west/raku/ch-2.p6
new file mode 100755
index 0000000000..45ce8e95c6
--- /dev/null
+++ b/challenge-203/roger-bell-west/raku/ch-2.p6
@@ -0,0 +1,21 @@
+#! /usr/bin/raku
+
+sub getdirs($src) {
+ my @a;
+ for dir($src) -> $e {
+ if ($e.d) {
+ @a.push($e);
+ @a.append(getdirs($e))
+ }
+ }
+ return @a;
+}
+
+sub copydir($src, $dst) {
+ my $l = 1 + $src.chars;
+ for map {$_.substr($l)}, getdirs($src) -> $o {
+ mkdir "$dst/$o";
+ }
+}
+
+copydir('a/b/c', 'x/y');
diff --git a/challenge-203/roger-bell-west/ruby/ch-1.rb b/challenge-203/roger-bell-west/ruby/ch-1.rb
new file mode 100755
index 0000000000..e0f2f6a0e0
--- /dev/null
+++ b/challenge-203/roger-bell-west/ruby/ch-1.rb
@@ -0,0 +1,29 @@
+#! /usr/bin/ruby
+
+require 'test/unit'
+
+def specialquads(l)
+ ct = 0
+ l.combination(4) do |c|
+ if c[0] + c[1] + c[2] == c[3] then
+ ct += 1
+ end
+ end
+ return ct
+end
+
+class TestSpecialquads < Test::Unit::TestCase
+
+ def test_ex1
+ assert_equal(1, specialquads([1, 2, 3, 6]))
+ end
+
+ def test_ex2
+ assert_equal(4, specialquads([1, 1, 1, 3, 5]))
+ end
+
+ def test_ex3
+ assert_equal(0, specialquads([3, 3, 6, 4, 5]))
+ end
+
+end
diff --git a/challenge-203/roger-bell-west/rust/ch-1.rs b/challenge-203/roger-bell-west/rust/ch-1.rs
new file mode 100755
index 0000000000..6bb9ce871f
--- /dev/null
+++ b/challenge-203/roger-bell-west/rust/ch-1.rs
@@ -0,0 +1,29 @@
+// [dependencies]
+// itertools = "0.10.5"
+
+use itertools::Itertools;
+
+#[test]
+fn test_ex1() {
+ assert_eq!(specialquads(vec![1, 2, 3, 6]), 1);
+}
+
+#[test]
+fn test_ex2() {
+ assert_eq!(specialquads(vec![1, 1, 1, 3, 5]), 4);
+}
+
+#[test]
+fn test_ex3() {
+ assert_eq!(specialquads(vec![3, 3, 6, 4, 5]), 0);
+}
+
+fn specialquads(a: Vec<usize>) -> usize {
+ let mut ct = 0;
+ for b in a.iter().combinations(4) {
+ if b[0] + b[1] + b[2] == *b[3] {
+ ct += 1;
+ }
+ }
+ ct
+}
diff --git a/challenge-203/roger-bell-west/tests.yaml b/challenge-203/roger-bell-west/tests.yaml
new file mode 100644
index 0000000000..29612a8573
--- /dev/null
+++ b/challenge-203/roger-bell-west/tests.yaml
@@ -0,0 +1,25 @@
+---
+ch-1:
+ - function: specialquads
+ arguments:
+ - 1
+ - 2
+ - 3
+ - 6
+ result: 1
+ - function: specialquads
+ arguments:
+ - 1
+ - 1
+ - 1
+ - 3
+ - 5
+ result: 4
+ - function: specialquads
+ arguments:
+ - 3
+ - 3
+ - 6
+ - 4
+ - 5
+ result: 0