aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-04-12 11:51:09 +0100
committerGitHub <noreply@github.com>2022-04-12 11:51:09 +0100
commite6c745fac4b016290eb7d4fd700d5b3ba7fe0c61 (patch)
treecc4c53e498b932a3343c9ee759222c2cef94ed16
parent5ed0e322f97c9882291decf59fafb0338203fce5 (diff)
parent34eccba312a64939178bbaa9c5ab1cc7526d4256 (diff)
downloadperlweeklychallenge-club-e6c745fac4b016290eb7d4fd700d5b3ba7fe0c61.tar.gz
perlweeklychallenge-club-e6c745fac4b016290eb7d4fd700d5b3ba7fe0c61.tar.bz2
perlweeklychallenge-club-e6c745fac4b016290eb7d4fd700d5b3ba7fe0c61.zip
Merge pull request #5925 from 0rir/160
160
-rw-r--r--challenge-160/0rir/raku/ch-1.raku39
-rw-r--r--challenge-160/0rir/raku/ch-2.raku56
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-160/0rir/raku/ch-1.raku b/challenge-160/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..0b249c254b
--- /dev/null
+++ b/challenge-160/0rir/raku/ch-1.raku
@@ -0,0 +1,39 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab
+use v6.d;
+
+# TASK #1 › Four Is Magic.
+
+my \TEST = False;
+
+my %a =
+ 1 => %( <name one sz 3 magic three> ),
+ 2 => %( <name two sz 3 magic three> ),
+ 3 => %( <name three sz 5 magic five > ),
+ 4 => %( <name four sz 4 magic magic> ),
+ 5 => %( <name five sz 4 magic magic> ),
+ 6 => %( <name six sz 3 magic three> ),
+ 7 => %( <name seven sz 5 magic five > ),
+ 8 => %( <name eight sz 5 magic five > ),
+ 9 => %( <name nine sz 4 magic four > ),
+;
+
+if TEST {
+ for 1..9 { m4gic($_); }
+ exit;
+}
+
+sub MAIN( Int $i = 6 ) {
+ m4gic($i);
+}
+
+sub m4gic ( Int $i where (0 < $i and $i < 10) ) {
+ my $n = $i;
+ my Str $out;
+ while %a{$n}<magic> ne 'magic' {
+ $out ~= "%a{$n}<name> is %a{$n}<magic>, ";
+ $n = %a{$n}<sz>;
+ }
+ $out ~= "%a{$n}<name> is %a{$n}<magic>.";
+ say "Input: \$n = $i\nOutput: ", $out.tc;
+}
diff --git a/challenge-160/0rir/raku/ch-2.raku b/challenge-160/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..647a62e45b
--- /dev/null
+++ b/challenge-160/0rir/raku/ch-2.raku
@@ -0,0 +1,56 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab
+use v6.d;
+
+use Test;
+constant TEST = True;
+
+#2 Find the Equilibrium Index which indexes the element between the two
+# sub arrays that sum to equal values, return -1 if none such.
+
+if TEST {
+ my @t =
+ 3 => [1, 3, 5, 7, 9 ],
+ -1 => [1, 2, 3, 4, 5 ],
+ 1 => [ 2, 4, 2 ],
+ -1 => [,],
+ -1 => [ 1 ],
+ -1 => [ 1, 1],
+ -1 => [ 1, 1, 1, 1 ],
+ -1 => [ 1, 7, 10, 40 ],
+ 2 => [ 1, 1, 1, 1, 1 ],
+ 1 => [ 4, 1, 1, 1, 2 ],
+ 3 => [ 1, 1, 2, 1, 4 ],
+ 2 => [ -1, 10, 1, 4, 5 ],
+ 1 => [ 10, -1, 1, 5, 4 ], # dual solution
+ 2 => [ 4, 5, 1, -1, 10 ], # dual solution
+ 2 => [ 5, 4, 1, 10, -1 ],
+ ;
+
+ plan @t.elems;
+
+ for @t -> (:$key, :$value) {
+ my $got = find-equilibrium-index ( $value.Array);
+ is $key, $got, "f-e-i: [ $value ] s/b $key";
+ }
+ exit;
+}
+
+sub find-equilibrium-index ( @ary --> Int ) {
+ return -1 if @ary.end < 2;
+ for 1..@ary.end-1 -> $i {
+ if ([+] @ary[0..($i - 1)]) == ([+] @ary[ ($i + 1) .. @ary.end ]) {
+ return $i;
+ }
+ }
+ -1
+}
+
+sub MAIN( @a = [ 1, 1, 1, 1, 7, 10, 40, 105, 59, 2] ) {
+ print 'Input: @n = (', @a, ")\nOutput: ";
+ my $flag = find-equilibrium-index ( @a );
+ print "$flag ", $flag == -1 ?? 'as no Equilibrium Index found.' !! '';
+}
+
+done-testing;
+