This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Question: why do #s 1 & 2 work, but #3 does not? | |
| ## | |
| my @w = <cat bat rat>; | |
| my $s = 'the cattle was rattle by the battery'; | |
| say '1: ', $s.subst(/ <|w>( @w )\w* /, {$0}, :g); | |
| ## Output> the cat was rat by the bat | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ raku cppc.raku | |
| 'alpha' --> {a => [0 4], h => [3], l => [1], p => [2]} | |
| {...} --> [a l p h a] | |
| ## ... uncomment 'Hash' on line 10 ... | |
| $ raku cppc.raku | |
| 'alpha' --> {a => [0 4], h => [3], l => [1], p => [2]} | |
| Type check failed in binding to parameter '%cp'; expected Associative[Hash] but got Hash[Any,Mu] ((my Any %{Mu} = | |
| :a($...) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Task 2: Line Counts | |
| # | |
| # You are given a string, $str, and a 26-items array @widths | |
| # containing the width of each character from a to z. | |
| # | |
| # Write a script to find out the number of lines and the width of the | |
| # last line needed to display the given string, assuming you can only | |
| # fit 100 width units on a line. | |
| constant $width = 100; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://theweeklychallenge.org/blog/perl-weekly-challenge-202/#TASK2 | |
| # | |
| # Given a profile as a list of altitudes, return the leftmost widest | |
| # valley. A valley is defined as a subarray of the profile consisting | |
| # of two parts: the first part is non-increasing and the second part | |
| # is non-decreasing. Either part can be empty. | |
| # encode: convert list of Int to Arry of symbols: | |
| # (1 5 5 2 8) --> [< = > <] | |
| # |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use v6; | |
| # generate a range delimiting each possible sub array | |
| # | |
| multi sub gensub (@a, $min = 1) { | |
| gensub(@a.elems, $min) | |
| } | |
| multi sub gensub (Int $n where $n > 0, Int $min = 1) { | |
| gather { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use v6; | |
| do -> @list, $target { | |
| my $char = @list.sort.first(* gt $target); | |
| say "List: (@list[]), ", | |
| "Target: $target, ", | |
| defined($char) ?? | |
| "Found: $char" !! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| subset Even of Int where * %% 2; | |
| sub median (*@n) { | |
| given @n.elems { | |
| when 0 { return () } | |
| when Even { return @n.sort[@n/2-1, @n/2].sum/2 } | |
| default { return @n.sort[(@n+1)/2-1] } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| subset Even of Int where * %% 2; | |
| sub median { | |
| my @n = @_; | |
| given @n.elems { | |
| when 0 { return () } | |
| when Even { return @n.sort[@n/2-1, @n/2].sum/2 } # avg of two middle elements | |
| default { return @n.sort[(@n+1)/2-1] } # middle element | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use v6; | |
| sub MAIN (*@files, :$fmt='%d', :$sep=': ') { | |
| my $nl = 1; | |
| @files = '-' unless @files; | |
| for @files>>.IO -> $f { | |
| $nl = 1; | |
| for $f.lines -> $line { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use v6; | |
| sub MAIN (:$sep = "\t", :$min = 0) { | |
| my ($lines, $words, $chars) = 0 xx 3; | |
| for lines() -> $l { | |
| $lines += 1; | |
| $chars += $l.chars + 1; | |
| $words += $l.subst(/<-alpha -space>/,'',:g).words.grep(*.chars >= $min); |
NewerOlder