diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-29 02:19:41 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-29 02:19:41 +0000 |
| commit | 9d7fbcb5c20d3f10f243cc05fef3e036962b6948 (patch) | |
| tree | 7e64ca6d3ca66cfe42f383711965dc376090808b | |
| parent | 59ca57d90b96d74296f55d67c014da32e38ee29e (diff) | |
| parent | 8c953f90a689af3e95831048128acca2352a7848 (diff) | |
| download | perlweeklychallenge-club-9d7fbcb5c20d3f10f243cc05fef3e036962b6948.tar.gz perlweeklychallenge-club-9d7fbcb5c20d3f10f243cc05fef3e036962b6948.tar.bz2 perlweeklychallenge-club-9d7fbcb5c20d3f10f243cc05fef3e036962b6948.zip | |
Merge pull request #9478 from 0rir/master
253
| -rw-r--r-- | challenge-253/0rir/raku/ch-1.raku | 90 | ||||
| -rw-r--r-- | challenge-253/0rir/raku/ch-2.raku | 87 |
2 files changed, 177 insertions, 0 deletions
diff --git a/challenge-253/0rir/raku/ch-1.raku b/challenge-253/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..736f9e1e70 --- /dev/null +++ b/challenge-253/0rir/raku/ch-1.raku @@ -0,0 +1,90 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6; +use lib $?FILE.IO.cleanup.parent(2).add("lib"); +use Test; + +=begin comment +Task 1: Split Strings Submitted by: Mohammad S Anwar +Given an array of strings and a character separator; write a script to +return all words separated by the given character excluding empty string. + +Example 1 +Input: @words = ("one.two.three","four.five","six") + $separator = "." +Output: "one","two","three","four","five","six" +Example 2 +Input: @words = ("$perl$$", "$$raku$") + $separator = "$" +Output: "perl","raku" + +=end comment + +sub ____ (+@list) { gather @list.deepmap: *.take } # move to Flatland +constant \pancake-flat = &____; + +my @Test = + '@', ('one@@two@three','four$five',"six"), + Q'"one","two","three","four$five","six"', + ':', ("one:two:three","four:five","six"), + Q'"one","two","three","four","five","six"', + '.', ("one.two.three","four.five","six"), + Q'"one","two","three","four","five","six"', + '$', ('$perl$$', '$$raku$'), + Q'"perl","raku"', + '#', ('one##two#three','four$five',"six"), + Q'"one","two","three","four$five","six"', + '%', ('one%%two%three','four$five',"six"), + Q'"one","two","three","four$five","six"', + '^', ('one^^two^three','four$five',"six"), + Q'"one","two","three","four$five","six"', + '&', ('one&&two&three','four$five',"six"), + Q'"one","two","three","four$five","six"', + '*', ('one**two*three','four$five',"six"), + Q'"one","two","three","four$five","six"', + '(', ('one((two((three','four$five',"six"), + Q'"one","two","three","four$five","six"', + ')', ('one))two)three','four$five',"six"), + Q'"one","two","three","four$five","six"', + '?', ('one??two?three','four$five',"six"), + Q'"one","two","three","four$five","six"', + + # Q'one\\two' ; is confused. + +# Q'\', ( Q{one\\two\three}, Q{four$five}, "six"), # this will work +# Q'"one","two","three","four$five","six"', + + # but punted these +# Q'\', ( Q{one\\two\three}, Q{four\$five}, "six"), +# Q'"one","two","three","four$five","six"', +# Q'\', ( Q{one\\two\three}, Q{four\\$five}, "six"), +# Q'"one","two","three","four$five","six"', +# Q'\', ( Q{one\\two\three}, Q{four\\\$five}, "six"), +# Q'"one","two","three","four$five","six"', +# Q'\', ( Q{one\\two\three}, Q{four\\\\$five}, "six"), +# Q'"one","two","three","four$five","six"', +; +plan @Test ÷ 3; + +sub func( $sep where * ne Q{\}, @word --> Str) { + # more efficient to be taught how to normalize backslash than self learning? + '"' + ~ @word.map( *.split( / $sep+ / , :skip-empty ) + ).&____.join( '","').grep( none "") + ~ '"' +} + +for @Test -> $sep, @in, $exp { + is func($sep, @in), $exp, "$exp <- $sep <- @in[]"; +} +done-testing; +my $sep = 'x'; +my @word = Q{??xxx!!x##X#@@x^}, Q{^x&x*x(x)x-x+x=[x;]'x/x\x|x<x>x_x}; + + +print "\nInput: @word = ( @word[] )\n \$separator = " + ~ $sep ~ "\nOutput: ", func( $sep, @word); + +die unless func( $sep, @word) ~~ Q{"??","!!","##X#@@","^","^","&","*","(",")","-","+","=[",";]'","/","\","|","<",">","_"}; +exit; + diff --git a/challenge-253/0rir/raku/ch-2.raku b/challenge-253/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..1e75bafa40 --- /dev/null +++ b/challenge-253/0rir/raku/ch-2.raku @@ -0,0 +1,87 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6; +use lib $?FILE.IO.cleanup.parent(2).add("lib"); +use Test; + +=begin comment +Task 2: Weakest Row +Submitted by: Mohammad S Anwar +You are given an m x n binary matrix i.e. only 0 and 1 where 1 always appear before 0. + +A row i is weaker than a row j if one of the following is true: + +a) The number of 1s in row i is less than the number of 1s in row j. +b) Both rows have the same number of 1 and i < j. +Write a script to return the order of rows from weakest to strongest. + +Example 1 +Input: $matrix = [ + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 0], + [1, 0, 0, 0, 0], + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 1] + ] +Output: (2, 0, 3, 1, 4) + +The number of 1s in each row is: +- Row 0: 2 +- Row 1: 4 +- Row 2: 1 +- Row 3: 2 +- Row 4: 5 +Example 2 +Input: $matrix = [ + [1, 0, 0, 0], + [1, 1, 1, 1], + [1, 0, 0, 0], + [1, 0, 0, 0] + ] +Output: (0, 2, 3, 1) + +The number of 1s in each row is: +- Row 0: 1 +- Row 1: 4 +- Row 2: 1 +- Row 3: 1 +=end comment + +my @Test = + [ [1, 1, 0, 0, 0], + [1, 1, 1, 1, 0], + [1, 0, 0, 0, 0], + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 1] + ], (2, 0, 3, 1, 4), + [ [1, 0, 0, 0], + [1, 1, 1, 1], + [1, 0, 0, 0], + [1, 0, 0, 0] + ], (0, 2, 3, 1), +; + +plan @Test ÷ 2; + +sub func( @in ) { + @in.map( { ( @$_.first( 0, :k) // +@in ), $++}) + .sort( {$_.[0], $_.[1]}).map( *[1]); +} + + +for @Test -> @in, @exp { + is func(@in), @exp, "@exp.raku() <- @in.raku()"; +} + +done-testing; + +my @matrix = [ [1, 1, 1, 0, 0], + [1, 1, 1, 1, 0], + [1, 0, 0, 0, 0], + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 1]; +]; +; +say "\nInput: @matrix = @matrix.raku()\nOutput: ", func( @matrix); +exit; + |
