aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-04-16 03:39:57 +0100
committerGitHub <noreply@github.com>2023-04-16 03:39:57 +0100
commit3dba20d5f955080589b7955b0edc3cdb7b3bc945 (patch)
tree38081252baabcb9d60ac2290f54846abb06d3d95
parent75a07010d37b8521a0581e37d1f0dd4803d0bc12 (diff)
parentcc358e6080576b16998977a916d8ebb138e0f982 (diff)
downloadperlweeklychallenge-club-3dba20d5f955080589b7955b0edc3cdb7b3bc945.tar.gz
perlweeklychallenge-club-3dba20d5f955080589b7955b0edc3cdb7b3bc945.tar.bz2
perlweeklychallenge-club-3dba20d5f955080589b7955b0edc3cdb7b3bc945.zip
Merge pull request #7903 from 0rir/212
212
-rwxr-xr-xchallenge-212/0rir/raku/ch-1.raku70
-rwxr-xr-xchallenge-212/0rir/raku/ch-2.raku131
2 files changed, 201 insertions, 0 deletions
diff --git a/challenge-212/0rir/raku/ch-1.raku b/challenge-212/0rir/raku/ch-1.raku
new file mode 100755
index 0000000000..1b6737f634
--- /dev/null
+++ b/challenge-212/0rir/raku/ch-1.raku
@@ -0,0 +1,70 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # πŸ¦‹ βˆ…βˆͺβˆ©βˆ‹βˆˆβˆ‰ ≑ β‰’ «␀ Β» ∴
+use v6.d;
+use Test;
+
+=begin comment
+212-1: Jumping Letters Submitted by: Mohammad S Anwar
+Given a word having only alphabetic characters and a list of positive
+integers of the same length print the new word generated after jumping
+forward each letter in the given word by the integer in the list.
+
+Example 1
+Input: $word = 'Perl' and @jump = (2,22,19,9)
+Output: Raku
+
+'P' jumps 2 place forward and becomes 'R'.
+'e' jumps 22 place forward and becomes 'a'. (jump is cyclic i.e. after 'z'
+ you go back to 'a')
+'r' jumps 19 place forward and becomes 'k'.
+'l' jumps 9 place forward and becomes 'u'.
+Example 2
+Input: $word = 'Raku' and @jump = (24,4,7,17)
+Output: 'Perl'
+=end comment
+
+my @Test =
+ 'Perl', (2,22,19,9), 'Raku',
+ 'vWxYz', (5,5,5,5,5), 'aBcDe',
+ 'abc', (1,2,3), 'bdf',
+;
+plan @Test Γ· 3;
+
+class Rot-n {
+ has Any $.abc is required; # a ring-table of the alphabet in use
+ has Int $!elems; # of $.abc
+ has Any %!index; # kv,s of abc
+
+ method key-of( $char ) { %!index{$char.lc} }
+ method val-of( UInt $idx ) { $!abc[$idx % $!elems];}
+
+ method new( @alphabet){ ::?CLASS.Mu::new( abc => @alphabet ); }
+ method TWEAK { $!elems = $!abc.elems; %!index = $!abc.antipairs; }
+
+ # rotate each char in $in by zip sister in @offset
+ method rot-n( Str $in, @offset where *.elems == $in.chars -->Str) {
+ my Str @orig = [$in.comb];
+ my @return = @orig;
+ for ^@return -> $i {
+ @return[$i] = $.val-of( $.key-of(@return[$i]) + @offset[ $i]);
+ @return[$i] = @orig[$i] eq @orig[$i].uc
+ ?? @return[$i].uc
+ !! @return[$i];
+ }
+ return @return.join;
+} }
+
+
+my Rot-n $r = Rot-n.new( 'a'...'z' );
+
+for @Test -> $in, @offset, $exp {
+ is $r.rot-n( $in, @offset), $exp, "$in --> $exp";
+}
+done-testing;
+
+
+my $word = 'Perl';
+my @jump = (2,22,19,9);
+
+say "\nInput: $word = 'Perl' and @jump = @jump.raku()
+Output: $r.rot-n($word, @jump)";
diff --git a/challenge-212/0rir/raku/ch-2.raku b/challenge-212/0rir/raku/ch-2.raku
new file mode 100755
index 0000000000..e0d9d8d217
--- /dev/null
+++ b/challenge-212/0rir/raku/ch-2.raku
@@ -0,0 +1,131 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # πŸ¦‹ βˆ…βˆͺβˆ©βˆ‹βˆˆβˆ‰ ≑ β‰’ «␀ Β» ∴
+use v6.d;
+use Test;
+
+=begin comment
+212-2: Rearrange Groups Submitted by: Mohammad S Anwar
+
+Given a list of integers and a group size greater than zero, split the list
+into groups of the given size where integers are in sequential order.
+If it can’t be done then print -1.
+
+
+Example 1:
+Input: @list = (1,2,3,5,1,2,7,6,3) and $size = 3
+Output: (1,2,3), (1,2,3), (5,6,7)
+Example 2:
+Input: @list = (1,2,3) and $size = 2
+Output: -1
+Example 3:
+Input: @list = (1,2,4,3,5,3) and $size = 2
+Output: (1,2,3), (3,4,5)
+Example 4:
+Input: @list = (1,5,2,6,4,7) and $size = 3
+Output: -1
+
+=end comment
+
+my @Test =
+ # part-size, list, exp
+#dies 1, [,], Array,
+ 2, [1,2,3,4,5], Array,
+ 3, [1,2,3,4,5], Array,
+ 4, [1,2,3,4,5], Array,
+ 6, [1,2,3,4,5], Array,
+ 1, [1,2,3,4], [[1,],[2,],[3,],[4,]],
+ 2, [1,3,9], Array,
+ 2, [1,2], [[1,2]],
+ 2, [1,2,2,3], [[1,2],[2,3]],
+ 2, [1,2,3,4], [[1,2],[3,4]],
+ 2, [1,2,8,9], [[1,2],[8,9]],
+ 2, [1,2,2,3,9,9], Array,
+
+ 5, [1,2,3,4,2,3,4,5,3,4,5,6,6,7,8,9,-9,-8,-7,-6,-1,0,1,2], Array,
+ 3, [1,2,3,4,2,3,4,5,3,4,5,6,6,7,8,9,-9,-8,-7,-6,-1,0,1,2], Array,
+ 4, [1,2,3,4,2,3,4,5,3,4,5,6,6,7,8,9,-9,-8,-7,-6,-1,0,1,2],
+ [ [ -9,-8,-7,-6,], [ -1,0,1,2], [1,2,3,4],
+ [2,3,4,5,], [3,4,5,6,], [6,7,8,9,], ],
+ 2, [1,2,3,4,2,3,4,5,3,4,5,6,6,7,8,9,-9,-8,-7,-6,-1,0,1,2],
+ [ [ -9,-8,],[-7,-6,], [ -1,0,],[1,2], [1,2,],[3,4],
+ [2,3,],[4,5,], [3,4,],[5,6,], [6,7,],[8,9,], ].sort,
+ 1, [1,2,3,4,2,3,4,5,3,4,5,6,6,7,8,9,-9,-8,-7,-6,-1,0,1,2],
+ [ [ -9],[-8],[-7],[-6],[-1],[0],[1],[2],
+ [1],[2],[3],[4],[2],[3],[4],[5],[3],
+ [4],[5],[6],[6],[7],[8],[9], ].sort,
+;
+
+class All-seq {
+ has Int @.in is required; # input
+ has Int $.psize is required; # part size
+ has Int @.val; # input stripped to unique
+ has Int @.qty; # zip sister give qty of elem in @val
+ has @.result is rw = []; # solution
+ has Int $.cur is rw = 0; # current head of workspace
+
+ method new( Int $psize, @in ) {
+ ::?CLASS.Mu::new( in => @in, psize => $psize );
+ }
+
+ method process( -->Array) {
+ # easy solves
+ when not +@!in %% $!psize {
+ return Array;
+ }
+ when $!psize == 1 {
+ @!in.=sort;
+ return @!in.map( { @[0] = $_ }).Array;
+ }
+ # set up
+ {
+ my @tmp = @!in.Bag.sort;
+ @!qty = @tmp.map: *.value;
+ @!val = @tmp.map: *.key;
+ }
+
+ if parts() { return @!result }
+ return Array;
+
+ #---- not reached ----
+
+ sub parts( -->Bool) { # build @!result, return True if done
+ loop {
+ while 0 == @!qty[$!cur] and $!cur < +@!val - $!psize + 1 {
+ ++$!cur;
+ }
+ # done?
+ if $!cur > +@!val - $!psize {
+ if @!qty[$!cur..^@!val].any β‰  0 {
+ return False;
+ }
+ return True;
+ }
+ # slide window
+ my $tail = $!cur + $!psize -1;
+ for ($!cur..$tail ).reverse -> $i {
+ if $i - $!cur β‰  @!val[$i] - @!val[$!cur] {
+ return False;
+ } }
+ # have match
+ @!result.push: @!val[$!cur..$tail].Array;
+ # clean-up
+ @!qty[ $!cur..$tail].=map( --* );
+ next;
+ }
+ # ---- not reached ----
+ die "Bad programmer";
+} } }
+
+plan @Test Γ· 3;
+for @Test -> $siz, @in, @exp {
+ is All-seq.new( $siz ,@in).process, @exp,
+ "@in.raku() P $siz -> @exp.raku()" ;
+}
+done-testing;
+
+my @list = 1,2,3,5,1,2,7,6,3;
+my $size = 3;
+
+say "\nInput: @list = @list[] and \$size = $size
+Output: ", All-seq.new( $size, @list).process // -1;
+