aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoger Bell_West <roger@firedrake.org>2023-03-22 14:41:30 +0000
committerRoger Bell_West <roger@firedrake.org>2023-03-22 14:41:30 +0000
commitc39c3c7c8fe44628a7824a0f3f676cea2dd97d83 (patch)
treeec298f322cc0873311a7f3009257401397b316d6
parent9c5cd2108a8f6cf8b793c28051fdf8d767a4c8a9 (diff)
downloadperlweeklychallenge-club-c39c3c7c8fe44628a7824a0f3f676cea2dd97d83.tar.gz
perlweeklychallenge-club-c39c3c7c8fe44628a7824a0f3f676cea2dd97d83.tar.bz2
perlweeklychallenge-club-c39c3c7c8fe44628a7824a0f3f676cea2dd97d83.zip
RogerBW solutions for challenge no. 209
-rwxr-xr-xchallenge-209/roger-bell-west/javascript/ch-1.js28
-rw-r--r--challenge-209/roger-bell-west/kotlin/ch-1.kt28
-rwxr-xr-xchallenge-209/roger-bell-west/lua/ch-1.lua28
-rwxr-xr-xchallenge-209/roger-bell-west/perl/ch-1.pl22
-rwxr-xr-xchallenge-209/roger-bell-west/perl/ch-2.pl57
-rw-r--r--challenge-209/roger-bell-west/postscript/ch-1.ps57
-rw-r--r--challenge-209/roger-bell-west/postscript/ch-2.ps308
-rwxr-xr-xchallenge-209/roger-bell-west/python/ch-1.py22
-rwxr-xr-xchallenge-209/roger-bell-west/raku/ch-1.p620
-rwxr-xr-xchallenge-209/roger-bell-west/raku/ch-2.p655
-rwxr-xr-xchallenge-209/roger-bell-west/ruby/ch-1.rb27
-rwxr-xr-xchallenge-209/roger-bell-west/rust/ch-1.rs24
-rw-r--r--challenge-209/roger-bell-west/tests.yaml56
13 files changed, 732 insertions, 0 deletions
diff --git a/challenge-209/roger-bell-west/javascript/ch-1.js b/challenge-209/roger-bell-west/javascript/ch-1.js
new file mode 100755
index 0000000000..0cf77b5f18
--- /dev/null
+++ b/challenge-209/roger-bell-west/javascript/ch-1.js
@@ -0,0 +1,28 @@
+#! /usr/bin/node
+
+"use strict"
+
+function specialbitcharacters(a) {
+ let s = 0
+ for (let v of a) {
+ if (s == 1) {
+ s = 2;
+ } else {
+ s = v;
+ }
+ }
+ return s == 0;
+}
+
+if (specialbitcharacters([1, 0, 0])) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+if (!specialbitcharacters([1, 1, 1, 0])) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write("\n");
diff --git a/challenge-209/roger-bell-west/kotlin/ch-1.kt b/challenge-209/roger-bell-west/kotlin/ch-1.kt
new file mode 100644
index 0000000000..500f50e008
--- /dev/null
+++ b/challenge-209/roger-bell-west/kotlin/ch-1.kt
@@ -0,0 +1,28 @@
+fun specialbitcharacters(a: List<Int>): Boolean {
+ var s = 0
+ for (v in a) {
+ if (s == 1) {
+ s = 2
+ } else {
+ s = v
+ }
+ }
+ return s == 0
+}
+
+fun main() {
+
+ if (specialbitcharacters(listOf(1, 0, 0))) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ print(" ")
+ if (!specialbitcharacters(listOf(1, 1, 1, 0))) {
+ print("Pass")
+ } else {
+ print("Fail")
+ }
+ println("")
+
+}
diff --git a/challenge-209/roger-bell-west/lua/ch-1.lua b/challenge-209/roger-bell-west/lua/ch-1.lua
new file mode 100755
index 0000000000..12e8a3247f
--- /dev/null
+++ b/challenge-209/roger-bell-west/lua/ch-1.lua
@@ -0,0 +1,28 @@
+#! /usr/bin/lua
+
+function specialbitcharacters(a)
+ local s = 0
+ for k, v in ipairs(a) do
+ if s == 1 then
+ s = 2
+ else
+ s = v
+ end
+ end
+ return s == 0
+end
+
+if specialbitcharacters({1, 0, 0}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if not specialbitcharacters({1, 1, 1, 0}) then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")
+
diff --git a/challenge-209/roger-bell-west/perl/ch-1.pl b/challenge-209/roger-bell-west/perl/ch-1.pl
new file mode 100755
index 0000000000..7b30288edc
--- /dev/null
+++ b/challenge-209/roger-bell-west/perl/ch-1.pl
@@ -0,0 +1,22 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+use experimental 'signatures';
+
+use Test::More tests => 2;
+
+is(specialbitcharacters([1, 0, 0]), 1, 'example 1');
+is(specialbitcharacters([1, 1, 1, 0]), 0, 'example 2');
+
+sub specialbitcharacters($a) {
+ my $s = 0;
+ foreach my $v (@{$a}) {
+ if ($s == 1) {
+ $s = 2;
+ } else {
+ $s = $v;
+ }
+ }
+ return ($s == 0)?1:0;
+}
diff --git a/challenge-209/roger-bell-west/perl/ch-2.pl b/challenge-209/roger-bell-west/perl/ch-2.pl
new file mode 100755
index 0000000000..6d41bd0b23
--- /dev/null
+++ b/challenge-209/roger-bell-west/perl/ch-2.pl
@@ -0,0 +1,57 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+use experimental 'signatures';
+
+use Test::More tests => 2;
+
+is_deeply(mergeaccount([['A', 'a1@a.com', 'a2@a.com'], ['B', 'b1@b.com'], ['A', 'a3@a.com', 'a1@a.com']]), [['A', 'a1@a.com', 'a2@a.com', 'a3@a.com'], ['B', 'b1@b.com']], 'example 1');
+is_deeply(mergeaccount([['A', 'a1@a.com', 'a2@a.com'], ['B', 'b1@b.com'], ['A', 'a3@a.com'], ['B', 'b2@b.com', 'b1@b.com']]), [['A', 'a1@a.com', 'a2@a.com'], ['A', 'a3@a.com'], ['B', 'b1@b.com', 'b2@b.com']], 'example 2');
+
+sub mergeaccount($a) {
+ my @aname;
+ my $id = 0;
+ my %r;
+ foreach my $acc (@{$a}) {
+ push @aname, $acc->[0];
+ foreach my $i (1..$#{$acc}) {
+ push @{$r{$acc->[$i]}}, $id;
+ }
+ $id++;
+ }
+ my %m;
+ foreach my $idlist (values %r) {
+ if (scalar @{$idlist} > 1) {
+ my $root = $idlist->[0];
+ while (exists $m{$root}) {
+ $root = $m{$root};
+ }
+ foreach my $i (1..$#{$idlist}) {
+ $m{$idlist->[$i]} = $root;
+ }
+ }
+ }
+ my %staging;
+ my %prefix;
+ foreach my $id (0..$#{$a}) {
+ my $ii = $id;
+ while (exists $m{$ii}) {
+ $ii = $m{$ii};
+ }
+ my $acc = $a->[$id];
+ if (!exists $prefix{$ii}) {
+ $prefix{$ii} = $acc->[0];
+ }
+ foreach my $addr (map {$acc->[$_]} 1..$#{$acc}) {
+ $staging{$ii}{$addr} = 1;
+ }
+ }
+ my @out;
+ foreach my $k (keys %staging) {
+ push @out,[$prefix{$k}, sort keys %{$staging{$k}}];
+ }
+ @out = sort {$::a->[0] cmp $::b->[0] ||
+ $::a->[1] cmp $::b->[1]} @out;
+ return \@out;
+}
diff --git a/challenge-209/roger-bell-west/postscript/ch-1.ps b/challenge-209/roger-bell-west/postscript/ch-1.ps
new file mode 100644
index 0000000000..8155b78086
--- /dev/null
+++ b/challenge-209/roger-bell-west/postscript/ch-1.ps
@@ -0,0 +1,57 @@
+%!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 {
+ /test.count test.count 1 add def
+ {
+ /test.pass test.pass 1 add def
+ } {
+ ( ) print
+ test.count (....) cvs print
+ (-fail) print
+ } ifelse
+} bind def
+
+/test.start {
+ print (:) print
+ /test.pass 0 def
+ /test.count 0 def
+} bind def
+
+
+% end included library code
+
+/specialbitcharacters {
+ 1 dict begin
+ /s 0 def
+ {
+ s 1 eq {
+ pop
+ /s 2 def
+ } {
+ /s exch def
+ } ifelse
+ } forall
+ s 0 eq
+ end
+} bind def
+
+(specialbitcharacters) test.start
+[1 0 0] specialbitcharacters test
+[1 1 1 0] specialbitcharacters not test
+test.end
diff --git a/challenge-209/roger-bell-west/postscript/ch-2.ps b/challenge-209/roger-bell-west/postscript/ch-2.ps
new file mode 100644
index 0000000000..527036de99
--- /dev/null
+++ b/challenge-209/roger-bell-west/postscript/ch-2.ps
@@ -0,0 +1,308 @@
+%!PS
+
+% begin included library code
+% see https://codeberg.org/Firedrake/postscript-libraries/
+/quicksort.main { % lo hi -> (null)
+ 3 dict begin
+ /hi exch def
+ /lo exch def
+ /xit false def
+ lo 0 lt {
+ /xit true def
+ } if
+ hi 0 lt {
+ /xit true def
+ } if
+ lo hi ge {
+ /xit true def
+ } if
+ xit not {
+ /p quicksort.partition def
+ lo p quicksort.main
+ p 1 add hi quicksort.main
+ } if
+ end
+} bind def
+
+/filter { % array proc(bool) -> array
+ 1 dict begin
+ /p exch def
+ [ exch
+ {
+ dup p not
+ {
+ pop
+ } if
+ } forall
+ ]
+ end
+} bind def
+
+/quicksort { % [ a c b ] -> [ a b c ]
+ 1 dict begin
+ /arr exch def
+ arr length 0 gt {
+ 0 arr length 1 sub quicksort.main
+ } if
+ arr
+ end
+} bind def
+
+/map { % array proc -> array
+ 2 dict begin
+ /p exch def
+ [ exch
+ {
+ p
+ } forall
+ ]
+ end
+} bind def
+
+/apush.right { % [a b] c -> [a b c]
+ exch
+ [ exch aload length 2 add -1 roll ]
+} 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
+
+/keys { % dict -> array of dict keys
+ [ exch
+ {
+ pop
+ } forall
+ ]
+} bind def
+
+/toset { % array -> dict of (value, true)
+ << exch
+ {
+ true
+ } forall
+ >>
+} 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.start {
+ print (:) print
+ /test.pass 0 def
+ /test.count 0 def
+} bind def
+
+/values { % dict -> array of dict values
+ [ exch
+ {
+ exch pop
+ } forall
+ ]
+} 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
+
+/deepeq {
+ 2 dict begin
+ /a exch def
+ /b exch def
+ a type b type eq {
+ a type /dicttype eq {
+ a length b length eq {
+ <<
+ a {
+ pop
+ true
+ } forall
+ b {
+ pop
+ true
+ } forall
+ >>
+ true exch
+ {
+ pop
+ dup a exch known {
+ dup b exch known {
+ dup a exch get exch b exch get deepeq not {
+ pop false
+ } if
+ } {
+ false
+ } ifelse
+ } {
+ false
+ } ifelse
+ } forall
+ } {
+ false
+ } ifelse
+ } {
+ a type dup /arraytype eq exch /stringtype eq or {
+ a length b length eq {
+ true
+ 0 1 a length 1 sub {
+ dup a exch get exch b exch get deepeq not {
+ pop false
+ exit
+ } if
+ } for
+ } {
+ false
+ } ifelse
+ } {
+ a b eq
+ } ifelse
+ } ifelse
+ } {
+ false
+ } ifelse
+ 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
+
+
+% end included library code
+
+/mergeaccount {
+ 5 dict begin
+ /a exch def
+ /id 0 def
+ /r 0 dict def
+ /aname [
+ a {
+ /acc exch def
+ acc 0 get
+ 1 1 acc length 1 sub {
+ /i exch def
+ r acc i get known {
+ r acc i get get
+ } {
+ [ ]
+ } ifelse
+ id
+ apush.right
+ r exch acc i get exch put
+ } for
+ /id id 1 add def
+ } forall
+ ] def
+ /m 0 dict def
+ r values {
+ /idlist exch def
+ idlist length 1 gt {
+ /root idlist 0 get def
+ {
+ m root known {
+ /root m root get def
+ } {
+ exit
+ } ifelse
+ } loop
+ 1 1 idlist length 1 sub {
+ /i exch def
+ m idlist i get root put
+ } for
+ } if
+ } forall
+ /staging 0 dict def
+ /prefix 0 dict def
+ 0 1 a length 1 sub {
+ /id exch def
+ /ii id def
+ {
+ m ii known {
+ /ii m ii get def
+ } {
+ exit
+ } ifelse
+ } loop
+ /acc a id get def
+ prefix ii known not {
+ prefix ii acc 0 get put
+ } if
+ 1 1 acc length 1 sub {
+ acc exch get /addr exch def
+ staging ii known not {
+ staging ii 0 dict put
+ } if
+ staging ii get addr true put
+ } for
+ } for
+ /out [
+ staging keys {
+ /k exch def
+ [ prefix k get staging k get keys { dup length string cvs } map quicksort aload pop ]
+ } forall
+ ] def
+ [
+ out { 0 get } map toset keys { dup length string cvs } map quicksort {
+ /k exch def
+ /tmp out { 0 get k deepeq } filter def
+ tmp { 1 get } map toset keys { dup length string cvs } map quicksort {
+ /j exch def
+ tmp { 1 get j deepeq } filter aload pop
+ } forall
+ } forall
+ ]
+ end
+} bind def
+
+(mergeaccount) test.start
+[[(A) (a1@a.com) (a2@a.com)] [(B) (b1@b.com)] [(A) (a3@a.com) (a1@a.com)]] mergeaccount [[(A) (a1@a.com) (a2@a.com) (a3@a.com)] [(B) (b1@b.com)]] deepeq test
+[[(A) (a1@a.com) (a2@a.com)] [(B) (b1@b.com)] [(A) (a3@a.com)] [(B) (b2@b.com) (b1@b.com)]] mergeaccount [[(A) (a1@a.com) (a2@a.com)] [(A) (a3@a.com)] [(B) (b1@b.com) (b2@b.com)]] deepeq test
+test.end
diff --git a/challenge-209/roger-bell-west/python/ch-1.py b/challenge-209/roger-bell-west/python/ch-1.py
new file mode 100755
index 0000000000..4f3632b196
--- /dev/null
+++ b/challenge-209/roger-bell-west/python/ch-1.py
@@ -0,0 +1,22 @@
+#! /usr/bin/python3
+
+import unittest
+
+def specialbitcharacters(a):
+ s = 0
+ for v in a:
+ if s == 1:
+ s = 2
+ else:
+ s = v
+ return s == 0
+
+class TestSpecialbitcharacters(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(specialbitcharacters([1, 0, 0]), True, 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(specialbitcharacters([1, 1, 1, 0]), False, 'example 2')
+
+unittest.main()
diff --git a/challenge-209/roger-bell-west/raku/ch-1.p6 b/challenge-209/roger-bell-west/raku/ch-1.p6
new file mode 100755
index 0000000000..7148d541e9
--- /dev/null
+++ b/challenge-209/roger-bell-west/raku/ch-1.p6
@@ -0,0 +1,20 @@
+#! /usr/bin/raku
+
+use Test;
+
+plan 2;
+
+is(specialbitcharacters([1, 0, 0]), True, 'example 1');
+is(specialbitcharacters([1, 1, 1, 0]), False, 'example 2');
+
+sub specialbitcharacters(@a) {
+ my $s = 0;
+ for @a -> $v {
+ if ($s == 1) {
+ $s = 2;
+ } else {
+ $s = $v;
+ }
+ }
+ return $s == 0;
+}
diff --git a/challenge-209/roger-bell-west/raku/ch-2.p6 b/challenge-209/roger-bell-west/raku/ch-2.p6
new file mode 100755
index 0000000000..d6a55b578c
--- /dev/null
+++ b/challenge-209/roger-bell-west/raku/ch-2.p6
@@ -0,0 +1,55 @@
+#! /usr/bin/raku
+
+use Test;
+
+plan 2;
+
+is-deeply(mergeaccount([['A', 'a1@a.com', 'a2@a.com'], ['B', 'b1@b.com'], ['A', 'a3@a.com', 'a1@a.com']]), [['A', 'a1@a.com', 'a2@a.com', 'a3@a.com'], ['B', 'b1@b.com']], 'example 1');
+is-deeply(mergeaccount([['A', 'a1@a.com', 'a2@a.com'], ['B', 'b1@b.com'], ['A', 'a3@a.com'], ['B', 'b2@b.com', 'b1@b.com']]), [['A', 'a1@a.com', 'a2@a.com'], ['A', 'a3@a.com'], ['B', 'b1@b.com', 'b2@b.com']], 'example 2');
+
+sub mergeaccount(@a) {
+ my @aname;
+ my $id = 0;
+ my %r;
+ for @a -> $acc {
+ @aname.push($acc[0]);
+ for (1..$acc.end) -> $i {
+ %r{$acc[$i]}.push($id);
+ }
+ $id++;
+ }
+ my %m;
+ for (%r.values) -> $idlist {
+ if ($idlist.elems > 1) {
+ my $root = $idlist[0];
+ while (%m{$root}:exists) {
+ $root = %m{$root};
+ }
+ for (1..$idlist.end) -> $i {
+ %m{$idlist[$i]} = $root;
+ }
+ }
+ }
+ my %staging;
+ my %prefix;
+ for (0..@a.end) -> $id {
+ my $ii = $id;
+ while (%m{$ii}:exists) {
+ $ii = %m{$ii};
+ }
+ my $acc = @a[$id];
+ if (%prefix{$ii}:!exists) {
+ %prefix{$ii} = $acc[0];
+ }
+ for (map {$acc[$_]}, 1..$acc.end) -> $addr {
+ %staging{$ii}{$addr} = 1;
+ }
+ }
+ my @out;
+ for (%staging.keys) -> $k {
+ @out.push([%prefix{$k}, sort keys %staging{$k}]ยป.List.flat.Array);
+ }
+ @out = sort {$^a[0] cmp $^b[0] ||
+ $^a[1] cmp $^b[1]}, @out;
+ return @out;
+}
diff --git a/challenge-209/roger-bell-west/ruby/ch-1.rb b/challenge-209/roger-bell-west/ruby/ch-1.rb
new file mode 100755
index 0000000000..42ace6362c
--- /dev/null
+++ b/challenge-209/roger-bell-west/ruby/ch-1.rb
@@ -0,0 +1,27 @@
+#! /usr/bin/ruby
+
+require 'test/unit'
+
+def specialbitcharacters(a)
+ s = 0
+ a.each do |v|
+ if s == 1 then
+ s = 2
+ else
+ s = v
+ end
+ end
+ return s == 0
+end
+
+class TestSpecialbitcharacters < Test::Unit::TestCase
+
+ def test_ex1
+ assert_equal(true, specialbitcharacters([1, 0, 0]))
+ end
+
+ def test_ex2
+ assert_equal(false, specialbitcharacters([1, 1, 1, 0]))
+ end
+
+end
diff --git a/challenge-209/roger-bell-west/rust/ch-1.rs b/challenge-209/roger-bell-west/rust/ch-1.rs
new file mode 100755
index 0000000000..26ea2cc521
--- /dev/null
+++ b/challenge-209/roger-bell-west/rust/ch-1.rs
@@ -0,0 +1,24 @@
+#! /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!(specialbitcharacters(vec![1, 0, 0]), true);
+}
+
+#[test]
+fn test_ex2() {
+ assert_eq!(specialbitcharacters(vec![1, 1, 1, 0]), false);
+}
+
+fn specialbitcharacters(a: Vec<u8>) -> bool {
+ let mut s: u8 = 0;
+ for v in a {
+ if s == 1 {
+ s = 2;
+ } else {
+ s = v;
+ }
+ }
+ s == 0
+}
diff --git a/challenge-209/roger-bell-west/tests.yaml b/challenge-209/roger-bell-west/tests.yaml
new file mode 100644
index 0000000000..f1ab3f7829
--- /dev/null
+++ b/challenge-209/roger-bell-west/tests.yaml
@@ -0,0 +1,56 @@
+---
+ch-1:
+ - function: specialbitcharacters
+ arguments:
+ - 1
+ - 0
+ - 0
+ result:
+ true
+ - function: specialbitcharacters
+ arguments:
+ - 1
+ - 1
+ - 1
+ - 0
+ result:
+ false
+ch-2:
+ - function: mergeaccount
+ arguments:
+ - - A
+ - a1@a.com
+ - a2@a.com
+ - - B
+ - b1@b.com
+ - - A
+ - a3@a.com
+ - a1@a.com
+ result:
+ - - A
+ - a1@a.com
+ - a2@a.com
+ - a3@a.com
+ - - B
+ - b1@b.com
+ - function: mergeaccount
+ arguments:
+ - - A
+ - a1@a.com
+ - a2@a.com
+ - - B
+ - b1@b.com
+ - - A
+ - a3@a.com
+ - - B
+ - b2@b.com
+ - b1@b.com
+ result:
+ - - A
+ - a1@a.com
+ - a2@a.com
+ - - A
+ - a3@a.com
+ - - B
+ - b1@b.com
+ - b2@b.com