aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-07-23 00:10:30 +0100
committerGitHub <noreply@github.com>2023-07-23 00:10:30 +0100
commitc4a90badc5a44fef7f8b8dac9cdf0c77e1312c5b (patch)
tree599f0da1693669cf9e3b785a2d703aa90913f6ed
parent398fb646c24946306b9d3a9a77f7f592ad0fd86f (diff)
parent190ae8f0096fea69e3cf1d89eb26234224fedc62 (diff)
downloadperlweeklychallenge-club-c4a90badc5a44fef7f8b8dac9cdf0c77e1312c5b.tar.gz
perlweeklychallenge-club-c4a90badc5a44fef7f8b8dac9cdf0c77e1312c5b.tar.bz2
perlweeklychallenge-club-c4a90badc5a44fef7f8b8dac9cdf0c77e1312c5b.zip
Merge pull request #8425 from 0rir/226
226
-rw-r--r--challenge-222/0rir/ch-1.raku49
-rw-r--r--challenge-222/0rir/ch-2.raku90
-rw-r--r--challenge-226/0rir/raku/ch-1.raku41
-rw-r--r--challenge-226/0rir/raku/ch-2.raku49
-rw-r--r--challenge-226/0rir/raku/ch-2a.raku56
5 files changed, 146 insertions, 139 deletions
diff --git a/challenge-222/0rir/ch-1.raku b/challenge-222/0rir/ch-1.raku
deleted file mode 100644
index 8bb35039b9..0000000000
--- a/challenge-222/0rir/ch-1.raku
+++ /dev/null
@@ -1,49 +0,0 @@
-#!/usr/bin/env raku
-# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
-use v6.d;
-use Test;
-
-=begin comment
-222-1: Matching Members Submitted by: Mohammad S Anwar
-
-Given a list of positive integers, @ints, find the total matching members
-after sorting the list increasing order.
-
-Example 1
-Input: @ints = (1, 1, 4, 2, 1, 3)
-Output: 3
-
-Original list: (1, 1, 4, 2, 1, 2)
-Sorted list : (1, 1, 1, 2, 3, 4)
-Compare the two lists, we found 3 matching members (1, 1, 2).
-
-Example 2
-Input: @ints = (5, 1, 2, 3, 4)
-Output: 0
-
-Example 3
-Input: @ints = (1, 2, 3, 4, 5)
-Output: 5
-=end comment
-
-my @Test =
- (1, 1, 4, 2, 1, 3), 3,
- (5, 1, 2, 3, 4), 0,
- (1, 2, 3, 4, 5), 5,
- (1,2,3,4,5,5,4,), 5,
- (), 0,
-;
-
-plan @Test ÷ 2;
-
-for @Test -> @in, $exp {
- is ([Z==] @in, @in.sort).grep( *.Bool ).elems
- , $exp
- , 'count matches';
-}
-
-done-testing;
-my @int = (1,2,3,4,5,5,4,);
-say "\n\nInput: @ints = @int.raku()\nOutput: ",
- ([Z==] @int, @int.sort).grep( *.Bool ).elems;
-
diff --git a/challenge-222/0rir/ch-2.raku b/challenge-222/0rir/ch-2.raku
deleted file mode 100644
index 0f51ebd0c3..0000000000
--- a/challenge-222/0rir/ch-2.raku
+++ /dev/null
@@ -1,90 +0,0 @@
-#!/usr/bin/env raku
-# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
-use v6.d;
-use Test;
-
-=begin comment
-222-2: Last Member Submitted by: Mohammad S Anwar
-Given rules and an array of positive integers, @ints, find the last member
-if found otherwise return 0 by applying the follwoing rules. Each turn
-pick 2 biggest members (x, y) then decide based on the following conditions,
-continue this until you are left with 1 member or none.
-
-a) if x == y then remove both members
-
-b) if x != y then remove both members and add new member (y-x)
-
-
-Example 1:
-Input: @ints = (2, 7, 4, 1, 8, 1)
-Output: 1
-
-Step 1: pick 7 and 8, we remove both and add new member 1 => (2, 4, 1, 1, 1).
-Step 2: pick 2 and 4, we remove both and add new member 2 => (2, 1, 1, 1).
-Step 3: pick 2 and 1, we remove both and add new member 1 => (1, 1, 1).
-Step 4: pick 1 and 1, we remove both => (1).
-
-Example 2:
-Input: @ints = (1)
-Output: 1
-
-Example 3:
-Input: @ints = (1, 1)
-Output: 0
-=end comment
-
-my @Test =
- (), 0,
- (1,), 1,
- (1,1), 0,
- (1,1,1), 1,
- [∞, ∞], 0,
- (2,1,1), 0,
- (2,1,1,1), 1,
- (5,1), 1,
- (5,5,5,5,), 0,
- (5,5,5,5,5,), 1,
- (6,6,5,5,5,), 1,
- (2, 7, 4, 1, 8, 1), 1,
- [2, 2, 2, 2, 2, 2, 1], 1,
- [2, 2, 2, 2, 2, 2, 2, 1], 1,
- [10, 15, 4], 1,
- [10, 15, 2], 1,
- [20, 15, 2], 1,
-;
-
-plan @Test ÷ 2;
-
-sub func( @b ) {
- my $b = BagHash.new: @b;
-
- loop {
- # done?
- return 0 if $b.total == 0;
- return 1 if $b.total == 1;
-
- # elim even count max
- if $b.max.value %% 2 {
- $b{$b.max.key} = 0;
- next;
- }
-
- # resolve singular max
- $b{$b.max.key} = 1;
- return 1 if $b.total == 1;
- my $super-max = $b.max.key;
- $b.remove: $super-max;
- my $max = $b.max.key;
- $b.remove: $max;
- $b.add: $super-max - $max;
- }
-}
-
-for @Test -> @in, $exp {
- is func(@in), $exp, "$exp <- @in.sort.reverse()";
-}
-done-testing;
-
-my @int = ( 20, 70, 70, 40, 10, 80 ).sort;
-say "\n\nInput: @ints = @int[]\nOutput: ", func(@int);
-
diff --git a/challenge-226/0rir/raku/ch-1.raku b/challenge-226/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..417409f450
--- /dev/null
+++ b/challenge-226/0rir/raku/ch-1.raku
@@ -0,0 +1,41 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+226-1: Shuffle String Submitted by: Mohammad S Anwar
+Given a string and an array of indices of same length as string, return the
+string after re-arranging the indices in the correct order.
+
+Example 1
+Input: $string = 'lacelengh', @indices = (3,2,0,5,4,8,6,7,1)
+Output: 'challenge'
+Example 2
+Input: $string = 'rulepark', @indices = (4,7,3,1,0,5,2,6)
+Output: 'perlraku'
+=end comment
+
+my @Test =
+ 'lacelengh', 'challenge', [3,2,0,5,4,8,6,7,1],
+ 'rulepark', 'perlraku', [4,7,3,1,0,5,2,6],
+;
+plan @Test ÷ 3;
+
+sub func( $l, @i -->Str) {
+ my @word = $l.comb;
+ my @return;
+ sink @word.map: { @return[@i[$++]] = @word[$++] };
+ @return.join;
+}
+
+for @Test -> $in, $out, @in {
+ is func($in, @in ), $out, "$out <- $in [ @in[] ]";
+}
+done-testing;
+
+my $string = 'lacelengh';
+my @indices = (3,2,0,5,4,8,6,7,1);
+say "\nInput: \$string = '$string', @indices = (3,2,0,5,4,8,6,7,1)
+Output: '&func($string, @indices)'";
+
diff --git a/challenge-226/0rir/raku/ch-2.raku b/challenge-226/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..aceadb4048
--- /dev/null
+++ b/challenge-226/0rir/raku/ch-2.raku
@@ -0,0 +1,49 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6.d;
+use lib $?FILE.IO.cleanup.parent(2).add("lib");
+use Test;
+
+=begin comment
+
+226-2: Zero Array Submitted by: Mohammad S Anwar
+Given an array of non-negative integers, @ints, return the minimum number of
+operations to make every element equal zero.
+In each operation, you are required to pick a positive number less than or
+equal to the smallest element in the array, then subtract that from each positive element in the array.
+
+Example 1:
+Input: @ints = (1, 5, 0, 3, 5)
+Output: 3
+
+operation 1: pick 1 => (0, 4, 0, 2, 4)
+operation 2: pick 2 => (0, 2, 0, 0, 2)
+operation 3: pick 2 => (0, 0, 0, 0, 0)
+=end comment
+
+# NOTE: Per the instructions. No solution unless all values are same.
+
+my @Test =
+ Int, [,],
+ 0, [0,0],
+ Int, [1,2],
+ 1, [1,1],
+ Int, [1,5,0,3,5],
+ Int, [10, 8, 20],
+;
+
+plan @Test ÷ 2;
+
+sub func( @a is copy -->Int) {
+ return Int if @a ~~ [];
+ return 0 if @a.all == 0;
+ return 1 if @a.all == @a[0];
+ return Int;
+}
+
+for @Test -> $exp, @in {
+ is func(@in), $exp, "$exp.raku() <-- @in.raku()";
+}
+done-testing;
+exit;
+
diff --git a/challenge-226/0rir/raku/ch-2a.raku b/challenge-226/0rir/raku/ch-2a.raku
new file mode 100644
index 0000000000..81eefdf47b
--- /dev/null
+++ b/challenge-226/0rir/raku/ch-2a.raku
@@ -0,0 +1,56 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6.d;
+use lib $?FILE.IO.cleanup.parent(2).add("lib");
+use Test;
+
+=begin comment
+
+226-2: Zero Array Submitted by: Mohammad S Anwar
+Given an array of non-negative integers, @ints, return the minimum number of
+operations to make every element equal zero.
+In each operation, you are required to pick a positive number less than or
+equal to the smallest element in the array, then subtract that from each positive element in the array.
+
+Example 1:
+Input: @ints = (1, 5, 0, 3, 5)
+Output: 3
+
+operation 1: pick 1 => (0, 4, 0, 2, 4)
+operation 2: pick 2 => (0, 2, 0, 0, 2)
+operation 3: pick 2 => (0, 0, 0, 0, 0)
+=end comment
+
+# NOTE: Per the example. Every unique value greater than 0 adds 1 turn.
+
+my @Test =
+ Int, [,],
+ 0, [0,0],
+ 2, [1,2],
+ 1, [1,1],
+ 3, [1,5,0,3,5],
+ 3, [10, 8, 20],
+ 100, (0..100),
+ 100, (1..100),
+ 98, (1^..^100),
+ 10_000, (0..10_000).pick(*),
+;
+
+plan @Test ÷ 2;
+
+sub func( @a is copy -->Int) {
+ return Int if @a ~~ [,];
+ @a = @a.sort.squish;
+ @a.shift if @a[0] == 0;
+ return 0 if @a ~~ [,];
+ return @a.elems;
+}
+
+for @Test -> $exp, @in {
+ is func(@in), $exp, "$exp.raku() <-- @in.gist()";
+}
+done-testing;
+
+my @int = (1, 5, 0, 3, 5);
+say "\nInput: @ints = @int[]\n Output: ", func( @int);
+