From 017af0f60c0bc693a3b3f2cf8133ebc4c042345f Mon Sep 17 00:00:00 2001 From: rir Date: Sun, 28 Jan 2024 17:50:47 -0500 Subject: 253 --- challenge-253/0rir/raku/ch-1.raku | 90 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 challenge-253/0rir/raku/ch-1.raku 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|xx_x}; + + +print "\nInput: @word = ( @word[] )\n \$separator = " + ~ $sep ~ "\nOutput: ", func( $sep, @word); + +die unless func( $sep, @word) ~~ Q{"??","!!","##X#@@","^","^","&","*","(",")","-","+","=[",";]'","/","\","|","<",">","_"}; +exit; + -- cgit From 8c953f90a689af3e95831048128acca2352a7848 Mon Sep 17 00:00:00 2001 From: rir Date: Sun, 28 Jan 2024 18:54:35 -0500 Subject: 253-again --- challenge-253/0rir/raku/ch-2.raku | 87 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 challenge-253/0rir/raku/ch-2.raku 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; + -- cgit