Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
23
Task/Align-columns/Raku/align-columns-1.raku
Normal file
23
Task/Align-columns/Raku/align-columns-1.raku
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
my @lines =
|
||||
q|Given$a$text$file$of$many$lines,$where$fields$within$a$line$|
|
||||
are$delineated$by$a$single$'dollar'$character,$write$a$program
|
||||
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
|
||||
column$are$separated$by$at$least$one$space.
|
||||
Further,$allow$for$each$word$in$a$column$to$be$either$left$
|
||||
justified,$right$justified,$or$center$justified$within$its$column.
|
||||
|.lines;
|
||||
|
||||
my @widths;
|
||||
|
||||
for @lines { for .split('$').kv { @widths[$^key] max= $^word.chars; } }
|
||||
for @lines { say |.split('$').kv.map: { (align @widths[$^key], $^word) ~ " "; } }
|
||||
|
||||
sub align($column_width, $word, $aligment = @*ARGS[0]) {
|
||||
my $lr = $column_width - $word.chars;
|
||||
my $c = $lr / 2;
|
||||
given ($aligment) {
|
||||
when "center" { " " x $c.ceiling ~ $word ~ " " x $c.floor }
|
||||
when "right" { " " x $lr ~ $word }
|
||||
default { $word ~ " " x $lr }
|
||||
}
|
||||
}
|
||||
7
Task/Align-columns/Raku/align-columns-2.raku
Normal file
7
Task/Align-columns/Raku/align-columns-2.raku
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
sub MAIN ($alignment where 'left'|'right', $file) {
|
||||
my @lines := $file.IO.lines.map(*.split('$').cache).cache;
|
||||
my @widths = roundrobin(|@lines).map(*».chars.max);
|
||||
my $align = {left=>'-', right=>''}{$alignment};
|
||||
my $format = @widths.map( '%' ~ ++$ ~ '$' ~ $align ~ * ~ 's' ).join(' ') ~ "\n";
|
||||
printf $format, |$_ for @lines;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue