This file contains 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; | |
# add commas to a number (en_US only) | |
# adapted from: http://www.perlmonks.org/?node_id=653 | |
sub cfy ($n --> Str) { | |
S:g/ (\d) <?before [\d ** 3]+ [\D|$$]> /$0\,/ | |
given ~$n | |
} |
This file contains 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
sub escape ($str) { | |
# Puts a slash before non-alphanumeric characters | |
S:g[<-alpha -digit>] = "\\$/" given $str | |
} | |
say escape 'foo#bar?'; # foo\#bar\? | |
{ | |
sub escape ($str) { | |
# Writes each non-alphanumeric character in its hexadecimal escape |
This file contains 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); |
This file contains 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 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 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 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 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 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 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; |
OlderNewer