aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Davies <kaiepi@outlook.com>2021-10-15 17:54:04 -0300
committerBen Davies <kaiepi@outlook.com>2021-10-15 18:00:08 -0300
commit63a399d17ab45ba5c90abf51c1e06f690cbdf458 (patch)
tree0c6826d3a244a4869748f5e1607554af71056971
parent83b1f9e3d3e752ea9419b94dd7cdd76d662b6ccb (diff)
downloadperlweeklychallenge-club-63a399d17ab45ba5c90abf51c1e06f690cbdf458.tar.gz
perlweeklychallenge-club-63a399d17ab45ba5c90abf51c1e06f690cbdf458.tar.bz2
perlweeklychallenge-club-63a399d17ab45ba5c90abf51c1e06f690cbdf458.zip
Fix table cell width errors Ben Davies' challenge 134 task 2 solution
They don't appear in the provided test output, but do in other cases, breaking how the table is rendered due to the leftmost column getting the wrong width.
-rw-r--r--challenge-134/ben-davies/raku/ch-2.raku30
1 files changed, 16 insertions, 14 deletions
diff --git a/challenge-134/ben-davies/raku/ch-2.raku b/challenge-134/ben-davies/raku/ch-2.raku
index 4ca97a4c5f..624ebf127b 100644
--- a/challenge-134/ben-davies/raku/ch-2.raku
+++ b/challenge-134/ben-davies/raku/ch-2.raku
@@ -4,25 +4,27 @@ unit sub MAIN(IntStr:D $m where * > 0, IntStr:D $n where * > 0) {
my @n = <1>...$n;
my @mn = @m X* @n;
- my Int:D $nw = @n.map(*.chars).max + 1;
- my Int:D $cw = @m.map(*.chars).max + 1;
- my Int:D $mw = @mn.map(*.chars).max + 1;
+ my Int:D $nw = @n.map(*.chars).max;
+ my Int:D $mw = @m.map(*.chars).max + 1;
- put sprintf '╭%-s┬%-s╮', '─' x $nw, border('─', '┬');
- put sprintf '│%-s│%-s│', ' ' x ($nw - 1) ~ '✕', fmt-row(@n, '│');
- put sprintf '├%-s╆%-s┪', '─' x $nw, border('━', '┿');
- put sprintf '│%-s┃%-s┃', fmt-cell($_.head), fmt-row($_, '│') for @mn.rotor($n);
- put sprintf '╰%-s┺%-s┛', '─' x $nw, border('━', '┷');
+ put sprintf '╭%-s┬%-s╮', '─' x $nw, '─'.&border('┬');
+ put sprintf '│%-s│%-s│', '✕'.&col(' '), @n.&row('│');
+ put sprintf '├%-s╆%-s┪', '─' x $nw, '━'.&border('┿');
+ put sprintf '│%-s┃%-s┃', .head.&col(' '), .&row('│') for @mn.rotor($n);
+ put sprintf '╰%-s┺%-s┛', '─' x $nw, '━'.&border('┷');
put "Distinct Terms: $_" and
put "Count: $_.:<+>" given @mn.unique.sort;
- sub border(Str:D $token, Str:D $delim) {
- join $delim, $token x $cw xx $n
+ sub border(Str:D $border, Str:D $delim --> Str:D) {
+ join $delim, $border x $mw xx $n
}
- sub fmt-row(@xs, Str:D $delim) {
- @xs.map(&fmt-cell).join($delim)
+ sub col(Str:D(Cool:D) $cell, Str:D $delim --> Str:D) {
+ ($delim x ($nw - $cell.chars)) ~ $cell
}
- sub fmt-cell(Int:D $mn) {
- (' ' x $cw).chop($mn.chars) ~ $mn
+ sub row(@xs, Str:D $delim --> Str:D) {
+ @xs.map(*.&cell: ' ').join($delim)
+ }
+ sub cell(Str:D(Cool:D) $cell, Str:D $delim --> Str:D) {
+ ($delim x ($mw - $cell.chars)) ~ $cell
}
}