Skip to content

Instantly share code, notes, and snippets.

View nd3i's full-sized avatar

Joe nd3i

  • US:NJ:E3
View GitHub Profile
$ 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($...)
# 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;
@nd3i
nd3i / pwc202b.raku
Last active February 7, 2023 16:28
# 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) --> [< = > <]
#
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 {
use v6;
do -> @list, $target {
my $char = @list.sort.first(* gt $target);
say "List: (@list[]), ",
"Target: $target, ",
defined($char) ??
"Found: $char" !!
@nd3i
nd3i / PWC187.raku
Created October 30, 2022 00:40
A scratch at PWC 187
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] }
}
}
@nd3i
nd3i / median.raku
Last active October 29, 2022 19:02
n-ary function example
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
}
}
@nd3i
nd3i / nl.p6
Last active July 7, 2019 16:37
use v6;
sub MAIN (*@files, :$fmt='%d', :$sep=': ') {
my $nl = 1;
@files = '-' unless @files;
for @files>>.IO -> $f {
$nl = 1;
for $f.lines -> $line {
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);
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