aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrir <rirans@comcast.net>2022-05-05 22:38:48 -0400
committerrir <rirans@comcast.net>2022-05-05 22:38:48 -0400
commit9e940a7e621fb6bb96cf5644dc6f7e023cd649c5 (patch)
treec03fe2f8cc3a4ed3eaac92b3851884d26676b836
parentb13444bb5307ad7c7051ace6031dd063bac3cc32 (diff)
downloadperlweeklychallenge-club-9e940a7e621fb6bb96cf5644dc6f7e023cd649c5.tar.gz
perlweeklychallenge-club-9e940a7e621fb6bb96cf5644dc6f7e023cd649c5.tar.bz2
perlweeklychallenge-club-9e940a7e621fb6bb96cf5644dc6f7e023cd649c5.zip
163
-rw-r--r--challenge-163/0rir/raku/ch-1.raku59
-rw-r--r--challenge-163/0rir/raku/ch-2.raku63
2 files changed, 122 insertions, 0 deletions
diff --git a/challenge-163/0rir/raku/ch-1.raku b/challenge-163/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..974b847fcf
--- /dev/null
+++ b/challenge-163/0rir/raku/ch-1.raku
@@ -0,0 +1,59 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab
+use v6.d;
+
+=begin comment
+Task 1: Sum Bitwise Operator
+ Given a list positive numbers, calculate the sum of bitwise-and
+ operations on all unique pairs. NOTE: Presuming position is not
+ part of identity.
+=end comment
+
+my @n = ( 1, 2, 3, 8+2 );
+
+say present-sumbit( @n );
+
+exit;
+
+# sumbit -- compute the sum per comment above.
+# Operators with names this long are a bad idea, but the task's name
+# implies an operator so:
+sub prefix:<sumbit>( @a --> Int ) { # The sumbit!
+ my @dyad = cross( @a);
+ @dyad = @dyad.flat[*;*];
+ my $return = 0;
+ for @dyad -> $d {
+ $return += @$d[0] +& @$d[1];
+ }
+ $return
+}
+
+# generate all pairs of @a's elements and the empty pair.
+sub cross( @a is copy --> Array ) { # Lib-able?
+ (my @dyad).append: gather while @a {
+ my $e = @a.shift;
+ my @l = $e X @a;
+ take @l;
+ }
+}
+
+sub present-sumbit( @a is copy --> Str) {
+ die 'Arg @array must equal @array.unique' if @a !~~ @a.unique;
+ die 'Arg @array must contain all positive integers'
+ unless @a.all ~~ (Int) and @a.all > 0;
+
+ my (@just, @ify );
+ my $result = sumbit @a;
+ my $return = 'Input: @n = ' ~ "( " ~ @a.List().join(', ')
+ ~ ")\nOutput: " ~ $result ~ "\n\n";
+
+ @just = cross( @a);
+ @just = @just.flat[*;*];
+
+ for @just -> $e is rw {
+ @ify.push: @$e[0] +& @$e[1];
+ $e = '(' ~ @$e[0] ~ ' +& ' ~ @$e[1] ~ ')';
+ }
+ $return ~= 'Since ' ~ @just.join( ' + ')
+ ~ ' => ' ~ @ify.join( ' + ') ~ " => $result.";
+}
diff --git a/challenge-163/0rir/raku/ch-2.raku b/challenge-163/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..03656d9d5a
--- /dev/null
+++ b/challenge-163/0rir/raku/ch-2.raku
@@ -0,0 +1,63 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab
+use v6.d;
+
+=begin comment
+
+2: Summations
+ Given a list of numbers. Make a tempory list by snipping off the first
+ item. Create the next list from the temporary by having each number be
+ the sum of the temporary's items which are before or at the position of
+ the item being calculated. Dump the temporary list. Repeat with the
+ list just created until you have a single number. An example leaving
+ out the temporaries: (2,3,1) --> ( 3,4) --> ( 7).
+
+ My terminology of 'final summation' or 'summation arrays' may
+ be idiocyncratic.
+
+=end comment
+
+constant COL_WIDTH = 4;
+
+# (),(1,),(1,2),(1,2,3),(1,2,3,4),(1,2,3,4,5),(1,2,3,4,5,6);
+
+my @n = (1,5,4,3,2,6,7);
+my @summation = create-summation-arrays( @n );
+my $result = final-summation( @summation );
+
+say 'Input: @n = ' ~ "( " ~ @n.List().join(', ')
+ ~ ")\nOutput: " ~ $result ~ "\n";
+display-summation( @summation );
+
+exit;
+
+sub display-summation( @ary --> Nil ) {
+ my $ct =0;
+ my $fld-width = COL_WIDTH;
+ for @ary -> @a {
+ print ' ' x ($ct * $fld-width);
+ for @a -> $fld {
+ printf "%$($fld-width)s", $fld;
+ }
+ ++ $ct;
+ print "\n";
+ }
+}
+
+sub create-summation-arrays( @ary-pos-int --> Array ) {
+ my @current = @ary-pos-int;
+ (my @return).push: @ary-pos-int ;
+
+ while @current.end > 0 {
+ my @tmp;
+ for 1 .. @current.end -> $i {
+ @tmp.push: [+] @current[1 ... $i];
+ }
+ @return.push: @tmp;
+ @current = @tmp;
+ }
+ @return;
+}
+
+sub final-summation( @AoSummation ) { return @AoSummation[*-1][0]; }
+