June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,55 +1,45 @@
data b;
file f;
text n, t;
list c, r, s;
integer a, i, j, k, m, w;
integer a, i, k, m, w;
b_cast(b, "Given$a$text$file$of$many$lines,$where$fields$within$a$line$\n"
"are$delineated$by$a$single$'dollar'$character,$write$a$program\n"
"that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$\n"
"column$are$separated$by$at$least$one$space.\n"
"Further,$allow$for$each$word$in$a$column$to$be$either$left$\n"
"justified,$right$justified,$or$center$justified$within$its$column.");
b = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$\n"
"are$delineated$by$a$single$'dollar'$character,$write$a$program\n"
"that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$\n"
"column$are$separated$by$at$least$one$space.\n"
"Further,$allow$for$each$word$in$a$column$to$be$either$left$\n"
"justified,$right$justified,$or$center$justified$within$its$column.";
f_b_affix(f, b);
f.b_affix(b);
m = 0;
while (f_news(f, r, 0, 0, "$") ^ -1) {
l_append(c, r);
m = max(m, l_length(r));
while (f.news(r, 0, 0, "$") ^ -1) {
c.append(r);
m = max(m, ~r);
}
i = 0;
while (i < m) {
w = 0;
j = 0;
while (j < l_length(c)) {
r = c[j];
if (i < l_length(r)) {
for (, r in c) {
if (i < ~r) {
w = max(w, length(r[i]));
}
j += 1;
}
l_append(s, w + 1);
s.append(w + 1);
i += 1;
}
k = 3;
while (k) {
k -= 1;
o_plan(l_effect("right", "center", "left")[k], " justified", "\n");
j = 0;
while (j < l_length(c)) {
i = 0;
r = c[j];
while (i < l_length(r)) {
w = s[i];
m = w - length(r[i]);
o_form("/w~3/~/w~1/", a = k * m >> 1, "", m - a, "", r[i]);
i += 1;
for (k, t in list("left", "center", "right")) {
o_(t, " justified\n");
for (, r in c) {
for (i, n in r) {
m = s[i] - ~n;
o_form("/w~3/~/w~1/", a = (2 - k) * m >> 1, "", m - a, "", n);
}
o_newline();
j += 1;
}
o_newline();
}

View file

@ -0,0 +1,34 @@
DECLARE in$[] = { "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." }
OPTION DELIM "$"
CONST items = 6
SUB Print_In_Columns(style)
' Find widest column
FOR y = 0 TO items-1
FOR x = 1 TO AMOUNT(in$[y])
IF LEN(TOKEN$(in$[y], x)) > max THEN max = LEN(TOKEN$(in$[y], x))
NEXT
NEXT
' Print aligned
FOR y = 0 TO items-1
FOR x = 1 TO AMOUNT(in$[y])
PRINT ALIGN$(TOKEN$(in$[y], x), max+1, style);
NEXT
PRINT
NEXT
PRINT
END SUB
Print_In_Columns(0)
Print_In_Columns(1)
Print_In_Columns(2)

View file

@ -1,15 +1,21 @@
String method: justify(n, just) // ( just size string -- string )
import: mapping
import: file
: <<nbl \ stream n -- stream
#[ ' ' <<c ] times ;
String method: justify( n just -- s )
| l m |
n self size - dup ->l 2 / ->m
StringBuffer new
just $RIGHT == ifTrue: [ " " <<n(l) self << return ]
just $LEFT == ifTrue: [ self << " " <<n(l) return ]
" " <<n(m) self << " " <<n( l m - ) ;
String new
just $RIGHT if=: [ l <<nbl self << return ]
just $LEFT if=: [ self << l <<nbl return ]
m <<nbl self << l m - <<nbl
;
: align(just)
: align( file just -- )
| lines maxsize |
File new("align.txt") map(#[ wordsWith('$') ]) ->lines
0 lines apply(#[ apply(#[ size max ]) ]) ->maxsize
lines apply(#[ apply(#[ justify(maxsize, just) . ]) printcr ]) ;
#[ wordsWith( '$' ) ] file File new map ->lines
0 #[ apply( #[ size max ] ) ] lines apply ->maxsize
#[ apply( #[ justify( maxsize , just) . ] ) printcr ] lines apply
;

View file

@ -1,38 +1,23 @@
###to be called with perl6 columnaligner.pl <orientation>(left, center , right )
###with left as default
my $fh = open "example.txt" , :r or die "Can't read text file!\n" ;
my @filelines = $fh.lines ;
close $fh ;
my @maxcolwidths ; #array of the longest words per column
#########fill the array with values#####################
for @filelines -> $line {
my @words = $line.split( "\$" ) ;
for 0..@words.elems - 1 -> $i {
if @maxcolwidths[ $i ] {
if @words[ $i ].chars > @maxcolwidths[$i] {
@maxcolwidths[ $i ] = @words[ $i ].chars ;
}
}
else {
@maxcolwidths.push( @words[ $i ].chars ) ;
}
}
}
my $justification = @*ARGS[ 0 ] || "left" ;
##print lines , $gap holds the number of spaces, 1 to be added
##to allow for space preceding or following longest word
for @filelines -> $line {
my @words = $line.split( "\$" ) ;
for 0 ..^ @words -> $i {
my $gap = @maxcolwidths[$i] - @words[$i].chars + 1 ;
if $justification eq "left" {
print @words[ $i ] ~ " " x $gap ;
} elsif $justification eq "right" {
print " " x $gap ~ @words[$i] ;
} elsif $justification eq "center" {
$gap = ( @maxcolwidths[ $i ] + 2 - @words[$i].chars ) div 2 ;
print " " x $gap ~ @words[$i] ~ " " x $gap ;
}
}
say ''; #for the newline
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 }
}
}

View file

@ -1,15 +1,7 @@
my @lines = slurp("example.txt").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 }
}
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;
}