aboutsummaryrefslogtreecommitdiff
path: root/challenge-083
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-25 20:51:15 +0000
committerGitHub <noreply@github.com>2020-10-25 20:51:15 +0000
commit611d76265007210aaa52d30d3c7558f60aed4da2 (patch)
tree3192c15099f443a4b47c49e5f91e8507f12b9d93 /challenge-083
parent94c44b7bce27f3dffa616f1a213cde66a958ab0b (diff)
parent5ab6de2c14b0d85e87f275aad3a12ea04bb8effb (diff)
downloadperlweeklychallenge-club-611d76265007210aaa52d30d3c7558f60aed4da2.tar.gz
perlweeklychallenge-club-611d76265007210aaa52d30d3c7558f60aed4da2.tar.bz2
perlweeklychallenge-club-611d76265007210aaa52d30d3c7558f60aed4da2.zip
Merge pull request #2617 from arnesom/branch-for-challenge-083
083
Diffstat (limited to 'challenge-083')
-rw-r--r--challenge-083/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-083/arne-sommer/raku/ch-1.p611
-rwxr-xr-xchallenge-083/arne-sommer/raku/ch-2.p643
-rwxr-xr-xchallenge-083/arne-sommer/raku/flip-array43
-rwxr-xr-xchallenge-083/arne-sommer/raku/words-length11
5 files changed, 109 insertions, 0 deletions
diff --git a/challenge-083/arne-sommer/blog.txt b/challenge-083/arne-sommer/blog.txt
new file mode 100644
index 0000000000..f56945a2da
--- /dev/null
+++ b/challenge-083/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://raku-musings.com/wordly-array.html
diff --git a/challenge-083/arne-sommer/raku/ch-1.p6 b/challenge-083/arne-sommer/raku/ch-1.p6
new file mode 100755
index 0000000000..1f70965a6f
--- /dev/null
+++ b/challenge-083/arne-sommer/raku/ch-1.p6
@@ -0,0 +1,11 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (Str $S where $S.words.elems >= 3,
+ :v($verbose));
+
+my @words = $S.words;
+my $s = @words[1.. @words.end -1].join;
+
+say ": '{ $s }'" if $verbose;
+
+say $s.chars;
diff --git a/challenge-083/arne-sommer/raku/ch-2.p6 b/challenge-083/arne-sommer/raku/ch-2.p6
new file mode 100755
index 0000000000..30e1fd58fb
--- /dev/null
+++ b/challenge-083/arne-sommer/raku/ch-2.p6
@@ -0,0 +1,43 @@
+#! /usr/bin/env raku
+
+subset Positive of Numeric where * > 0;
+
+unit sub MAIN (*@A where @A.elems > 0 && all(@A) ~~ Positive, :v($verbose));
+
+my %sum;
+
+do_it( (), @A);
+
+my $lowest-sum = %sum.keys.grep( * >= 0).min;
+
+say ": Lowest non-negative sum: $lowest-sum" if $verbose;
+
+my @flip;
+
+for @(%sum{$lowest-sum}) -> @candidate
+{
+ state $index = 0;
+ @flip[$index] = @candidate.grep( * < 0).elems;
+ say ": Match $index: [{ @candidate.join(", ") }] with @flip[$index] flip(s)" if $verbose;
+ $index++;
+}
+
+say ": Flips: [{ @flip.join(", ") }]" if $verbose;
+
+say @flip.min;
+
+sub do_it (@left, @right is copy)
+{
+ my $current = @right.shift;
+
+ unless defined $current
+ {
+ say ": Candidate [{ @left.join(", ") }] with sum {@left.sum }" if $verbose;
+
+ %sum{@left.sum}.push: @left;
+ return;
+ }
+
+ do_it((@left, $current).flat, @right);
+ do_it((@left, -$current).flat, @right);
+}
diff --git a/challenge-083/arne-sommer/raku/flip-array b/challenge-083/arne-sommer/raku/flip-array
new file mode 100755
index 0000000000..30e1fd58fb
--- /dev/null
+++ b/challenge-083/arne-sommer/raku/flip-array
@@ -0,0 +1,43 @@
+#! /usr/bin/env raku
+
+subset Positive of Numeric where * > 0;
+
+unit sub MAIN (*@A where @A.elems > 0 && all(@A) ~~ Positive, :v($verbose));
+
+my %sum;
+
+do_it( (), @A);
+
+my $lowest-sum = %sum.keys.grep( * >= 0).min;
+
+say ": Lowest non-negative sum: $lowest-sum" if $verbose;
+
+my @flip;
+
+for @(%sum{$lowest-sum}) -> @candidate
+{
+ state $index = 0;
+ @flip[$index] = @candidate.grep( * < 0).elems;
+ say ": Match $index: [{ @candidate.join(", ") }] with @flip[$index] flip(s)" if $verbose;
+ $index++;
+}
+
+say ": Flips: [{ @flip.join(", ") }]" if $verbose;
+
+say @flip.min;
+
+sub do_it (@left, @right is copy)
+{
+ my $current = @right.shift;
+
+ unless defined $current
+ {
+ say ": Candidate [{ @left.join(", ") }] with sum {@left.sum }" if $verbose;
+
+ %sum{@left.sum}.push: @left;
+ return;
+ }
+
+ do_it((@left, $current).flat, @right);
+ do_it((@left, -$current).flat, @right);
+}
diff --git a/challenge-083/arne-sommer/raku/words-length b/challenge-083/arne-sommer/raku/words-length
new file mode 100755
index 0000000000..1f70965a6f
--- /dev/null
+++ b/challenge-083/arne-sommer/raku/words-length
@@ -0,0 +1,11 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (Str $S where $S.words.elems >= 3,
+ :v($verbose));
+
+my @words = $S.words;
+my $s = @words[1.. @words.end -1].join;
+
+say ": '{ $s }'" if $verbose;
+
+say $s.chars;