langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
54
Task/Align-columns/OCaml/align-columns.ocaml
Normal file
54
Task/Align-columns/OCaml/align-columns.ocaml
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#load "str.cma"
|
||||
open Str
|
||||
|
||||
let input = "\
|
||||
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."
|
||||
|
||||
let () =
|
||||
let lines = split (regexp_string "\n") input in
|
||||
let fields_l = List.map (split (regexp_string "$")) lines in
|
||||
let fields_l = List.map Array.of_list fields_l in
|
||||
let n = (* number of columns *)
|
||||
List.fold_left
|
||||
(fun n fields -> max n (Array.length fields))
|
||||
0 fields_l
|
||||
in
|
||||
let pads = Array.make n 0 in
|
||||
List.iter (
|
||||
(* calculate the max padding for each column *)
|
||||
Array.iteri
|
||||
(fun i word -> pads.(i) <- max pads.(i) (String.length word))
|
||||
) fields_l;
|
||||
|
||||
let print f =
|
||||
List.iter (fun fields ->
|
||||
Array.iteri (fun i word ->
|
||||
f word (pads.(i) - (String.length word))
|
||||
) fields;
|
||||
print_newline()
|
||||
) fields_l;
|
||||
in
|
||||
|
||||
(* left column-aligned output *)
|
||||
print (fun word pad ->
|
||||
let spaces = String.make pad ' ' in
|
||||
Printf.printf "%s%s " word spaces);
|
||||
|
||||
(* right column-aligned output *)
|
||||
print (fun word pad ->
|
||||
let spaces = String.make pad ' ' in
|
||||
Printf.printf "%s%s " spaces word);
|
||||
|
||||
(* center column-aligned output *)
|
||||
print (fun word pad ->
|
||||
let pad1 = pad / 2 in
|
||||
let pad2 = pad - pad1 in
|
||||
let sp1 = String.make pad1 ' ' in
|
||||
let sp2 = String.make pad2 ' ' in
|
||||
Printf.printf "%s%s%s " sp1 word sp2);
|
||||
;;
|
||||
60
Task/Align-columns/OpenEdge-Progress/align-columns.openedge
Normal file
60
Task/Align-columns/OpenEdge-Progress/align-columns.openedge
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
FUNCTION alignColumns RETURNS CHAR (
|
||||
i_c AS CHAR,
|
||||
i_calign AS CHAR
|
||||
):
|
||||
|
||||
DEF VAR ipass AS INT.
|
||||
DEF VAR iline AS INT.
|
||||
DEF VAR icol AS INT.
|
||||
DEF VAR iwidth AS INT EXTENT.
|
||||
DEF VAR cword AS CHAR.
|
||||
DEF VAR cspace AS CHAR.
|
||||
DEF VAR cresult AS CHAR.
|
||||
|
||||
EXTENT( iwidth ) = NUM-ENTRIES( ENTRY( 1, i_c, "~n" ), "$" ).
|
||||
|
||||
DO ipass = 0 TO 1:
|
||||
DO iline = 1 TO NUM-ENTRIES( i_c, "~n" ):
|
||||
DO icol = 1 TO NUM-ENTRIES( ENTRY( iline, i_c, "~n" ), "$" ):
|
||||
cword = ENTRY( icol, ENTRY( iline, i_c, "~n" ), "$" ).
|
||||
IF ipass = 0 THEN
|
||||
iwidth = MAXIMUM( LENGTH( cword ), iwidth[ icol ] ).
|
||||
ELSE DO:
|
||||
cspace = FILL( " ", iwidth[ icol ] - LENGTH( cword ) ).
|
||||
CASE i_calign:
|
||||
WHEN "left" THEN cresult = cresult + cword + cspace.
|
||||
WHEN "right" THEN cresult = cresult + cspace + cword.
|
||||
WHEN "center" THEN DO:
|
||||
cword = FILL( " ", INTEGER( LENGTH( cspace ) / 2 ) ) + cword.
|
||||
cresult = cresult + cword + FILL( " ", iwidth[icol] - LENGTH( cword ) ).
|
||||
END.
|
||||
END CASE. /* i_calign */
|
||||
cresult = cresult + " ".
|
||||
END.
|
||||
END. /* DO icol = 1 TO ... */
|
||||
IF ipass = 1 THEN
|
||||
cresult = cresult + "~n".
|
||||
END. /* DO iline = 1 TO ... */
|
||||
END. /* DO ipass = 0 TO 1 */
|
||||
|
||||
RETURN cresult.
|
||||
|
||||
END FUNCTION.
|
||||
|
||||
DEF VAR cc AS CHAR.
|
||||
|
||||
cc = SUBSTITUTE(
|
||||
"&1~n&2~n&3~n&4~n&5~n&6",
|
||||
"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."
|
||||
).
|
||||
|
||||
MESSAGE
|
||||
alignColumns( cc, "left" ) SKIP
|
||||
alignColumns( cc, "right" ) SKIP
|
||||
alignColumns( cc, "center" )
|
||||
VIEW-AS ALERT-BOX.
|
||||
75
Task/Align-columns/Oz/align-columns.oz
Normal file
75
Task/Align-columns/Oz/align-columns.oz
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
declare
|
||||
%% Lines: list of strings
|
||||
%% Alignment: function like fun {Left Txt ExtraSpace} ... end
|
||||
%% Returns: list of aligned (virtual) strings
|
||||
fun {Align Lines Alignment}
|
||||
ParsedLines = {Map Lines ParseLine}
|
||||
NumColumns = {Maximum {Map ParsedLines Record.width}}
|
||||
%% maps column index to column width:
|
||||
WidthOfColumn = {Record.map {TupleRange NumColumns}
|
||||
fun {$ ColumnIndex}
|
||||
fun {LengthOfThisColumn ParsedLine}
|
||||
{Length {CondSelect ParsedLine ColumnIndex nil}}
|
||||
end
|
||||
in
|
||||
{Maximum {Map ParsedLines LengthOfThisColumn}}
|
||||
end}
|
||||
in
|
||||
{Map ParsedLines
|
||||
fun {$ Columns}
|
||||
{Record.mapInd Columns
|
||||
fun {$ ColumnIndex ColumnText}
|
||||
Extra = WidthOfColumn.ColumnIndex - {Length ColumnText}
|
||||
in
|
||||
{Alignment ColumnText Extra}#" "
|
||||
end}
|
||||
end}
|
||||
end
|
||||
|
||||
%% A parsed line is a tuple of columns.
|
||||
%% "a$b$c" -> '#'(1:"a" 2:"b" 3:"c")
|
||||
fun {ParseLine Line}
|
||||
{List.toTuple '#' {String.tokens Line &$}}
|
||||
end
|
||||
|
||||
%% possible alignments:
|
||||
|
||||
fun {Left Txt Extra}
|
||||
Txt#{Spaces Extra}
|
||||
end
|
||||
|
||||
fun {Right Txt Extra}
|
||||
{Spaces Extra}#Txt
|
||||
end
|
||||
|
||||
fun {Center Txt Extra}
|
||||
Half = Extra div 2
|
||||
in
|
||||
{Spaces Half}#Txt#{Spaces Half + Extra mod 2}
|
||||
end
|
||||
|
||||
%% helpers:
|
||||
|
||||
%% 3 -> unit(1 2 3)
|
||||
fun {TupleRange Max}
|
||||
{List.toTuple unit {List.number 1 Max 1}}
|
||||
end
|
||||
|
||||
fun {Maximum X|Xr}
|
||||
{FoldL Xr Value.max X}
|
||||
end
|
||||
|
||||
fun {Spaces N}
|
||||
case N of 0 then nil
|
||||
else & |{Spaces N-1}
|
||||
end
|
||||
end
|
||||
|
||||
Lines = ["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."]
|
||||
in
|
||||
{ForAll {Align Lines Left} System.showInfo}
|
||||
37
Task/Align-columns/PL-I/align-columns.pli
Normal file
37
Task/Align-columns/PL-I/align-columns.pli
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
declare text character (300) varying;
|
||||
declare word character (20) varying;
|
||||
declare justification character (1);
|
||||
declare k fixed binary;
|
||||
declare input file, output file output;
|
||||
|
||||
open file (input) title ( '/CENTER.DAT,type(text),recsize(1000)' );
|
||||
open file (output) title ( '/OUT.TXT,type(text),recsize(1000)' );
|
||||
on endfile (input) stop;
|
||||
|
||||
display ('Specify whether justification is left, centered, or right');
|
||||
display ('Reply with a single letter: L, C, or R');
|
||||
get edit (justification) (A(1));
|
||||
|
||||
do forever;
|
||||
get file (input) edit (text) (L);
|
||||
put skip list (text);
|
||||
text = trim(text, '$', '$');
|
||||
do until (k = 0);
|
||||
k = index(text, '$');
|
||||
if k = 0 then /* last word in line */
|
||||
word = text;
|
||||
else
|
||||
do;
|
||||
word = substr(text, 1, k-1);
|
||||
text = substr(text, k);
|
||||
text = trim(text, '$');
|
||||
end;
|
||||
select (justification);
|
||||
when ('C', 'c') word = center(word, maxlength(word));
|
||||
when ('R', 'r') word = right (word, maxlength(word));
|
||||
otherwise ; /* The default is left adjusted. */
|
||||
end;
|
||||
put file (output) edit (word) (a(maxlength(word)));
|
||||
end;
|
||||
put file (output) skip;
|
||||
end;
|
||||
38
Task/Align-columns/Perl-6/align-columns-1.pl6
Normal file
38
Task/Align-columns/Perl-6/align-columns-1.pl6
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
###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
|
||||
}
|
||||
15
Task/Align-columns/Perl-6/align-columns-2.pl6
Normal file
15
Task/Align-columns/Perl-6/align-columns-2.pl6
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
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 }
|
||||
}
|
||||
}
|
||||
75
Task/Align-columns/PureBasic/align-columns.purebasic
Normal file
75
Task/Align-columns/PureBasic/align-columns.purebasic
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
Declare max(a,b)
|
||||
|
||||
If OpenConsole()
|
||||
Define a, i, x, y, maxlen
|
||||
Dim txt.s(0)
|
||||
Restore lines ; Get address of the first data block
|
||||
Read.i a
|
||||
ReDim txt(a)
|
||||
For i=0 To a ; Read the raw data lines
|
||||
Read.s txt(i)
|
||||
txt(i)=Trim(txt(i),"$") ; Remove any bad '$' that may be useless in the end...
|
||||
x=max(CountString(txt(i),"$"),x)
|
||||
Next
|
||||
y=a
|
||||
Dim matrix.s(x,y) ; Set up a nice matrix to work with, each word cleanly separated
|
||||
For x=0 To ArraySize(matrix(),1)
|
||||
For y=0 To ArraySize(matrix(),2)
|
||||
matrix(x,y)=StringField(txt(y),x+1,"$")
|
||||
maxlen=max(maxlen,Len(matrix(x,y)))
|
||||
Next
|
||||
Next
|
||||
If maxlen%2
|
||||
maxlen+1 ; Just to make sure that 'centered' output looks nice....
|
||||
EndIf
|
||||
|
||||
PrintN(#CRLF$+"---- Right ----")
|
||||
For y=0 To ArraySize(matrix(),2)
|
||||
For x=0 To ArraySize(matrix(),1)
|
||||
Print(RSet(matrix(x,y),maxlen+1))
|
||||
Next
|
||||
PrintN("")
|
||||
Next
|
||||
|
||||
PrintN(#CRLF$+"---- Left ----")
|
||||
For y=0 To ArraySize(matrix(),2)
|
||||
For x=0 To ArraySize(matrix(),1)
|
||||
Print(LSet(matrix(x,y),maxlen+1))
|
||||
Next
|
||||
PrintN("")
|
||||
Next
|
||||
|
||||
PrintN(#CRLF$+"---- Center ----")
|
||||
For y=0 To ArraySize(matrix(),2)
|
||||
For x=0 To ArraySize(matrix(),1)
|
||||
a=maxlen-Len(matrix(x,y))
|
||||
Print(LSet(RSet(matrix(x,y),maxlen-a/2),maxlen))
|
||||
Next
|
||||
PrintN("")
|
||||
Next
|
||||
|
||||
PrintN(#CRLF$+#CRLF$+"Press ENTER to quit."): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
|
||||
|
||||
Procedure max(x,y)
|
||||
If x>=y
|
||||
ProcedureReturn x
|
||||
Else
|
||||
ProcedureReturn y
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
||||
|
||||
DataSection
|
||||
lines:
|
||||
Data.i 5 ; e.g. 6-1 since first line is equal to 'zero'.
|
||||
text:
|
||||
Data.s "Given$a$text$file$of$many$lines,$where$fields$within$a$line$"
|
||||
Data.s "are$delineated$by$a$single$'dollar'$character,$write$a$program"
|
||||
Data.s "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$"
|
||||
Data.s "column$are$separated$by$at$least$one$space."
|
||||
Data.s "Further,$allow$for$each$word$in$a$column$oo$be$either$left$"
|
||||
Data.s "justified,$right$justified,$or$center$justified$within$its$column."
|
||||
EndDataSection
|
||||
59
Task/Align-columns/REBOL/align-columns.rebol
Normal file
59
Task/Align-columns/REBOL/align-columns.rebol
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
REBOL [
|
||||
Title: "Align Columns"
|
||||
Author: oofoe
|
||||
Date: 2010-09-29
|
||||
URL: http://rosettacode.org/wiki/Align_columns
|
||||
]
|
||||
|
||||
specimen: {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.}
|
||||
|
||||
; Parse specimen into data grid.
|
||||
|
||||
data: copy []
|
||||
foreach line parse specimen to-string lf [ ; Break into lines.
|
||||
append/only data parse line "$" ; Break into columns.
|
||||
]
|
||||
|
||||
; Compute independent widths for each column.
|
||||
|
||||
widths: copy [] insert/dup widths 0 length? data/1
|
||||
foreach line data [
|
||||
forall line [
|
||||
i: index? line
|
||||
widths/:i: max widths/:i length? line/1
|
||||
]
|
||||
]
|
||||
|
||||
pad: func [n /local x][x: copy "" insert/dup x " " n x]
|
||||
|
||||
; These formatting functions are passed as arguments to entable.
|
||||
|
||||
right: func [n s][rejoin [pad n - length? s s]]
|
||||
|
||||
left: func [n s][rejoin [s pad n - length? s]]
|
||||
|
||||
centre: func [n s /local h][
|
||||
h: round/down (n - length? s) / 2
|
||||
rejoin [pad h s pad n - h - length? s]
|
||||
]
|
||||
|
||||
; Display data as table.
|
||||
|
||||
entable: func [data format] [
|
||||
foreach line data [
|
||||
forall line [
|
||||
prin rejoin [format pick widths index? line line/1 " "]
|
||||
]
|
||||
print ""
|
||||
]
|
||||
]
|
||||
|
||||
; Format data table.
|
||||
|
||||
foreach i [left centre right] [
|
||||
print ["^/Align" i "...^/"] entable data get i]
|
||||
39
Task/Align-columns/Run-BASIC/align-columns.run
Normal file
39
Task/Align-columns/Run-BASIC/align-columns.run
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
theString$ = "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."
|
||||
|
||||
x = shoTable(theString$,"left",6)
|
||||
x = shoTable(theString$,"right",6)
|
||||
x = shoTable(theString$,"center",6)
|
||||
end
|
||||
|
||||
FUNCTION shoTable(theString$,align$,across)
|
||||
print "------------ align:";align$;" -- across:";across;" ------------"
|
||||
dim siz(across)
|
||||
b$ = " "
|
||||
while word$(theString$,i+1,"$") <> ""
|
||||
siz(i mod across) = max(siz(i mod across),len(word$(theString$,i + 1,"$")))
|
||||
i = i + 1
|
||||
wend
|
||||
for i = 0 to across - 1
|
||||
siz(i) = siz(i) + 1
|
||||
if siz(i) and 1 then siz(i) = siz(i) + 1
|
||||
next i
|
||||
|
||||
i = 0
|
||||
a$ = word$(theString$,i+1,"$")
|
||||
while a$ <> ""
|
||||
s = siz(i mod across) - len(a$)
|
||||
if align$ = "right" then a$ = left$(b$,s);a$
|
||||
if align$ = "left" then a$ = a$;left$(b$,s)
|
||||
if align$ = "center" then a$ = left$(b$,int(s / 2));a$;left$(b$,int(s / 2) + (s and 1))
|
||||
print "|";a$;
|
||||
i = i + 1
|
||||
if i mod across = 0 then print "|"
|
||||
a$ = word$(theString$,i+1,"$")
|
||||
wend
|
||||
print
|
||||
end function
|
||||
52
Task/Align-columns/Seed7/align-columns.seed7
Normal file
52
Task/Align-columns/Seed7/align-columns.seed7
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const array string: inputLines is [] (
|
||||
"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.");
|
||||
|
||||
const func array integer: computeColumnWidths (in array string: inputLines) is func
|
||||
result
|
||||
var array integer: columnWidths is 0 times 0;
|
||||
local
|
||||
var string: line is "";
|
||||
var array string: lineFields is 0 times "";
|
||||
var integer: index is 0;
|
||||
begin
|
||||
for line range inputLines do
|
||||
lineFields := split(line, "$");
|
||||
if length(lineFields) > length(columnWidths) then
|
||||
columnWidths &:= (length(lineFields) - length(columnWidths)) times 0;
|
||||
end if;
|
||||
for index range 1 to length(lineFields) do
|
||||
if length(lineFields[index]) > columnWidths[index] then
|
||||
columnWidths[index] := length(lineFields[index]);
|
||||
end if;
|
||||
end for;
|
||||
end for;
|
||||
end func;
|
||||
|
||||
const func string: center (in string: stri, in integer: length) is
|
||||
return ("" lpad (length - length(stri)) div 2 <& stri) rpad length;
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
var array integer: columnWidths is 0 times 0;
|
||||
var string: line is "";
|
||||
var array string: lineFields is 0 times "";
|
||||
var integer: index is 0;
|
||||
begin
|
||||
columnWidths := computeColumnWidths(inputLines);
|
||||
for line range inputLines do
|
||||
lineFields := split(line, "$");
|
||||
for index range 1 to length(lineFields) do
|
||||
# write(lineFields[index] rpad columnWidths[index] <& " "); # Left justify
|
||||
# write(lineFields[index] lpad columnWidths[index] <& " "); # Right justify
|
||||
write(center(lineFields[index], columnWidths[index]) <& " ");
|
||||
end for;
|
||||
writeln;
|
||||
end for;
|
||||
end func;
|
||||
39
Task/Align-columns/Shiny/align-columns.shiny
Normal file
39
Task/Align-columns/Shiny/align-columns.shiny
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
text: '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.'
|
||||
|
||||
align: action text; position;
|
||||
|
||||
# split text into 2D array of lines and words
|
||||
lines : { for text.split ~\$?\r?\n~ { for a.split '$' a end } end }
|
||||
|
||||
# calculate max required width for each column
|
||||
widths: { for lines for a here[b]: a.length.max here[b]? ends }
|
||||
|
||||
spaces: action out ("%%%ds" in).format '' end
|
||||
|
||||
# formatting functions
|
||||
left: action word; width;
|
||||
pad: width-word.length
|
||||
print "%s%s " word spaces pad
|
||||
end
|
||||
right: action word; width;
|
||||
pad: width-word.length
|
||||
print "%s%s " spaces pad word
|
||||
end
|
||||
center: action word; width;
|
||||
pad: (width-word.length)/2
|
||||
print "%s%s%s " spaces pad.floor word spaces pad.ceil
|
||||
end
|
||||
|
||||
if position.match ~^(left|center|right)$~ for lines
|
||||
for a local[position] a widths[b] end say ''
|
||||
ends say ''
|
||||
end
|
||||
|
||||
align text 'left'
|
||||
align text 'center'
|
||||
align text 'right'
|
||||
22
Task/Align-columns/TUSCRIPT/align-columns.tuscript
Normal file
22
Task/Align-columns/TUSCRIPT/align-columns.tuscript
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
$$ MODE TUSCRIPT
|
||||
MODE DATA
|
||||
$$ SET exampletext=*
|
||||
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.
|
||||
$$ MODE TUSCRIPT
|
||||
SET nix=SPLIT (exampletext,":$:",c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12)
|
||||
LOOP l1=1,12
|
||||
SET colum=CONCAT ("c",l1)
|
||||
SET newcolum=CONCAT ("new",l1)
|
||||
SET @newcolum="", length=MAX LENGTH (@colum), space=length+2
|
||||
LOOP n,l2=@colum
|
||||
SET newcell=CENTER (l2,space)
|
||||
SET @newcolum=APPEND (@newcolum,"~",newcell)
|
||||
ENDLOOP
|
||||
SET @newcolum=SPLIT (@newcolum,":~:")
|
||||
ENDLOOP
|
||||
SET exampletext=JOIN(new1,"$",new2,new3,new4,new5,new6,new7,new8,new9,new10,new11,new12)
|
||||
30
Task/Align-columns/TXR/align-columns.txr
Normal file
30
Task/Align-columns/TXR/align-columns.txr
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
@(collect)
|
||||
@ (coll)@{item /[^$]+/}@(end)
|
||||
@(end)
|
||||
@; nc = number of columns
|
||||
@; pi = padded items (data with row lengths equalized with empty strings)
|
||||
@; cw = vector of max column widths
|
||||
@; ce = center padding
|
||||
@(bind nc @(apply (fun max) (mapcar (fun length) item)))
|
||||
@(bind pi @(mapcar (lambda (row)
|
||||
(append row (repeat '("") (- nc (length row)))))
|
||||
item))
|
||||
@(bind cw @(vector-list
|
||||
(mapcar (lambda (column)
|
||||
(apply (fun max) (mapcar (fun length) column)))
|
||||
;; matrix transpose trick cols become rows:
|
||||
(apply (fun mapcar) (cons (fun list) pi)))))
|
||||
@(bind ns "")
|
||||
@(output)
|
||||
@ (repeat)
|
||||
@ (rep :counter i)@{pi @[cw i]} @(end)
|
||||
@ (end)
|
||||
@ (repeat)
|
||||
@ (rep :counter i)@{pi @(- [cw i])} @(end)
|
||||
@ (end)
|
||||
@ (repeat)
|
||||
@ (rep :counter i)@\
|
||||
@{ns @(trunc (- [cw i] (length pi)) 2)}@\
|
||||
@{pi @(- [cw i] (trunc (- [cw i] (length pi)) 2))} @(end)
|
||||
@ (end)
|
||||
@(end)
|
||||
37
Task/Align-columns/UNIX-Shell/align-columns-1.sh
Normal file
37
Task/Align-columns/UNIX-Shell/align-columns-1.sh
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
cat <<EOF_OUTER > just-nocenter.sh
|
||||
#!/bin/sh
|
||||
|
||||
td() {
|
||||
cat <<'EOF'
|
||||
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.
|
||||
EOF
|
||||
}
|
||||
|
||||
rows=$( td | wc -l )
|
||||
|
||||
# get the number of fields
|
||||
fields=$(td | rs -c'$' -g1 -h | awk '{print $2}')
|
||||
|
||||
# get the max of the value widths
|
||||
cwidth=$(td | rs -c'$' -g1 -w1 2>/dev/null | awk 'BEGIN{w=0}{if(length>w){w=length}}END{print w}')
|
||||
|
||||
# compute the minimum line width for the columns
|
||||
lwidth=$(( (1 + cwidth) * fields ))
|
||||
|
||||
# left adjusted columns
|
||||
td | rs -c'$' -g1 -zn -w$lwidth
|
||||
|
||||
echo ""
|
||||
|
||||
# right adjusted columns
|
||||
td | rs -c'$' -g1 -znj -w$lwidth
|
||||
|
||||
echo ""
|
||||
|
||||
exit
|
||||
EOF_OUTER
|
||||
14
Task/Align-columns/UNIX-Shell/align-columns-2.sh
Normal file
14
Task/Align-columns/UNIX-Shell/align-columns-2.sh
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
$ ./just-nocenter.sh
|
||||
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.
|
||||
|
||||
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.
|
||||
20
Task/Align-columns/Ursala/align-columns.ursala
Normal file
20
Task/Align-columns/Ursala/align-columns.ursala
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#import std
|
||||
|
||||
text =
|
||||
|
||||
-[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.]-
|
||||
|
||||
pad = sep`$*; @FS ~&rSSSK7+ (zipp` ^*D\~& leql$^)*rSSK7+ zipp0^*D/leql$^ ~&
|
||||
|
||||
just_left = mat` *+ pad
|
||||
just_right = mat` *+ pad; ==` ~-rlT**
|
||||
just_center = mat` *+ pad; ==` ~-rK30PlrK31PTT**
|
||||
|
||||
#show+
|
||||
|
||||
main = mat0 <.just_left,just_center,just_right> text
|
||||
65
Task/Align-columns/VBA/align-columns.vba
Normal file
65
Task/Align-columns/VBA/align-columns.vba
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
Public Sub TestSplit(Optional align As String = "left", Optional spacing As Integer = 1)
|
||||
Dim word() As String
|
||||
Dim colwidth() As Integer
|
||||
Dim ncols As Integer
|
||||
Dim lines(6) As String
|
||||
Dim nlines As Integer
|
||||
|
||||
'check arguments
|
||||
If Not (align = "left" Or align = "right" Or align = "center") Then
|
||||
MsgBox "TestSplit: wrong argument 'align': " & align
|
||||
Exit Sub
|
||||
End If
|
||||
If spacing < 0 Then
|
||||
MsgBox "TestSplit: wrong argument: 'spacing' cannot be negative."
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' Sample Input (should be from a file)
|
||||
nlines = 6
|
||||
lines(1) = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$"
|
||||
lines(2) = "are$delineated$by$a$single$'dollar'$character,$write$a$program"
|
||||
lines(3) = "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$"
|
||||
lines(4) = "column$are$separated$by$at$least$one$space."
|
||||
lines(5) = "Further,$allow$for$each$word$in$a$column$to$be$either$left$"
|
||||
lines(6) = "justified,$right$justified,$or$center$justified$within$its$column."
|
||||
|
||||
'first pass: count columns and column widths
|
||||
'the words are not kept in memory
|
||||
ncols = -1
|
||||
For l = 1 To nlines
|
||||
word = Split(RTrim(lines(l)), "$")
|
||||
If UBound(word) > ncols Then
|
||||
ncols = UBound(word)
|
||||
ReDim Preserve colwidth(ncols)
|
||||
End If
|
||||
For i = 0 To UBound(word)
|
||||
If Len(word(i)) > colwidth(i) Then colwidth(i) = Len(word(i))
|
||||
Next i
|
||||
Next l
|
||||
|
||||
'discard possibly empty columns at the right
|
||||
'(this assumes there is at least one non-empty column)
|
||||
While colwidth(ncols) = 0
|
||||
ncols = ncols - 1
|
||||
Wend
|
||||
|
||||
'second pass: print in columns
|
||||
For l = 1 To nlines
|
||||
word = Split(RTrim(lines(l)), "$")
|
||||
For i = 0 To UBound(word)
|
||||
a = word(i)
|
||||
w = colwidth(i)
|
||||
If align = "left" Then
|
||||
Debug.Print a + String$(w - Len(a), " ");
|
||||
ElseIf align = "right" Then
|
||||
Debug.Print String$(w - Len(a), " ") + a;
|
||||
ElseIf align = "center" Then
|
||||
d = Int((w - Len(a)) / 2)
|
||||
Debug.Print String$(d, " ") + a + String$(w - (d + Len(a)), " ");
|
||||
End If
|
||||
If i < ncols Then Debug.Print Spc(spacing);
|
||||
Next i
|
||||
Debug.Print
|
||||
Next l
|
||||
End Sub
|
||||
40
Task/Align-columns/Vedit-macro-language/align-columns.vedit
Normal file
40
Task/Align-columns/Vedit-macro-language/align-columns.vedit
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
RS(10, "$") // Field separator
|
||||
#11 = 1 // Align: 1 = left, 2 = center, 3 = right
|
||||
|
||||
// Reset column widths. Max 50 columns
|
||||
for (#1=40; #1<90; #1++) { #@1 = 0 }
|
||||
|
||||
// Find max width of each column
|
||||
BOF
|
||||
Repeat(ALL) {
|
||||
for (#1=40; #1<90; #1++) {
|
||||
Match(@10, ADVANCE) // skip field separator if any
|
||||
#2 = Cur_Pos
|
||||
Search("|{|@(10),|N}", NOERR) // field separator or end of line
|
||||
#3 = Cur_Pos - #2 // width of text
|
||||
if (#3 > #@1) { #@1 = #3 }
|
||||
if (At_EOL) { Break }
|
||||
}
|
||||
Line(1, ERRBREAK)
|
||||
}
|
||||
|
||||
// Convert lines
|
||||
BOF
|
||||
Repeat(ALL) {
|
||||
for (#1=40; #1<90; #1++) {
|
||||
#2 = Cur_Pos
|
||||
Search("|{|@(10),|N}", NOERR)
|
||||
if (At_EOL==0) { Del_Char(Chars_Matched) }
|
||||
#3 = #@1 - Cur_Pos + #2 // number of spaces to insert
|
||||
#4 = 0
|
||||
if (#11 == 2) { #4 = #3/2; #3 -= #4 } // Center
|
||||
if (#11 == 3) { #4 = #3; #3 = 0 } // Right justify
|
||||
Set_Marker(1, Cur_Pos)
|
||||
Goto_Pos(#2)
|
||||
Ins_Char(' ', COUNT, #4) // add spaces before the word
|
||||
Goto_Pos(Marker(1))
|
||||
Ins_Char(' ', COUNT, #3+1) // add spaces after the word
|
||||
if (At_EOL) { Break }
|
||||
}
|
||||
Line(1, ERRBREAK)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue