September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
180
Task/Align-columns/360-Assembly/align-columns.360
Normal file
180
Task/Align-columns/360-Assembly/align-columns.360
Normal file
|
|
@ -0,0 +1,180 @@
|
|||
* Align columns 12/04/2019
|
||||
ALICOL CSECT
|
||||
USING ALICOL,R13 base register
|
||||
B 72(R15) skip savearea
|
||||
DC 17F'0' savearea
|
||||
SAVE (14,12) save previous context
|
||||
ST R13,4(R15) link backward
|
||||
ST R15,8(R13) link forward
|
||||
LR R13,R15 set addressability
|
||||
LA R8,1 i=1
|
||||
DO WHILE=(C,R8,LE,=A(NI)) do r=1 to hbound(t)
|
||||
LA R7,1 j=1
|
||||
LA R6,L'T i=length(t)
|
||||
DO WHILE=(C,R6,GE,=A(1)) do i=length(t) to 1 by -1
|
||||
LR R1,R8 r
|
||||
MH R1,=AL2(L'T) ~
|
||||
LA R4,T-L'T(R1) t(r)
|
||||
BCTR R4,0 -1
|
||||
AR R4,R6 +i
|
||||
MVC CI,0(R4) ci=substr(t(r),i,1)
|
||||
CLI CI,C' ' if ci=' '
|
||||
BE ITERI1 then iterate i
|
||||
CLI CI,C'$' if ci='$'
|
||||
BE ITERI1 then iterate i
|
||||
LR R7,R6 j=i
|
||||
B LEAVEI1 leave i
|
||||
ITERI1 BCTR R6,0 i--
|
||||
ENDDO , enddo i
|
||||
LEAVEI1 LR R1,R8 r
|
||||
MH R1,=AL2(L'T) ~
|
||||
LA R4,T-L'T(R1) @t(r)
|
||||
LA R2,WT @wt
|
||||
LR R5,R7 j
|
||||
ICM R5,B'1000',=C' ' padding
|
||||
LA R3,L'T length(wt)
|
||||
MVCL R2,R4 wt=substr(t(r),1,j)
|
||||
LA R0,1 1
|
||||
ST R0,I0 i0=1
|
||||
SR R9,R9 c=0
|
||||
LA R6,1 i=1
|
||||
DO WHILE=(CR,R6,LE,R7) do i=1 to j
|
||||
LA R4,WT-1 @wt
|
||||
AR R4,R6 i
|
||||
MVC CI(1),0(R4) ci=substr(wt,i,1)
|
||||
IF CLI,CI,EQ,C'$' THEN if ci='$' then
|
||||
BAL R14,SEQ call seq
|
||||
LR R2,R6 i
|
||||
LA R2,1(R2) +1
|
||||
ST R2,I0 i0=i+1
|
||||
ENDIF , endif
|
||||
LA R6,1(R6) i++
|
||||
ENDDO , enddo i
|
||||
BAL R14,SEQ call seq
|
||||
IF C,R9,GT,COLS THEN if c>cols then
|
||||
ST R9,COLS cols=c
|
||||
ENDIF , endif
|
||||
LA R8,1(R8) r++
|
||||
ENDDO , enddo r
|
||||
LR R2,R8 r
|
||||
BCTR R2,0 -1
|
||||
ST R2,ROWS rows=r-1
|
||||
LA R7,1 j=1
|
||||
DO WHILE=(C,R7,LE,=A(3)) do j=1 to 3
|
||||
XPRNT =C'--',2 print
|
||||
LA R8,1 r=1
|
||||
DO WHILE=(C,R8,LE,ROWS) do r=1 to rows
|
||||
MVC PG,=CL120' ' pg=' '
|
||||
LA R0,1 1
|
||||
ST R0,IB ib=1
|
||||
LA R9,1 c=1
|
||||
DO WHILE=(C,R9,LE,COLS) do c=1 to cols
|
||||
LR R1,R8 r
|
||||
BCTR R1,0 -1
|
||||
MH R1,=AL2(NJ) ~
|
||||
AR R1,R9 c
|
||||
MH R1,=AL2(L'WOR) ~
|
||||
LA R4,WOR-L'WOR(R1) @wor(r,c)
|
||||
MVC W,0(R4) w=wor(r,c)
|
||||
LA R6,L'W i=length(w)
|
||||
DO WHILE=(C,R6,GE,=A(1)) do i=length(w) to 1 by -1
|
||||
LA R4,W-1 @w
|
||||
AR R4,R6 i
|
||||
MVC CI,0(R4) ci=substr(w,i,1)
|
||||
CLI CI,C' ' if ci^=' '
|
||||
BNE LEAVEI2 then goto leavei2;
|
||||
BCTR R6,0 i--
|
||||
ENDDO , enddo i
|
||||
LEAVEI2 EQU * ~
|
||||
IF LTR,R6,Z,R6 THEN if i=0 then
|
||||
LA R10,1 l=1
|
||||
ELSE , else
|
||||
LR R10,R6 l=i
|
||||
ENDIF , endif
|
||||
IF C,R7,EQ,=F'1' THEN if j=1 then
|
||||
L R11,IB ibx=ib
|
||||
ENDIF , endif
|
||||
IF C,R7,EQ,=F'2' THEN if j=2 then
|
||||
LR R1,R9 c
|
||||
SLA R1,2 ~
|
||||
L R11,WID-L'WID(R1) wid(c)
|
||||
A R11,IB +ib
|
||||
SR R11,R10 ibx=ib+wid(c)-l
|
||||
ENDIF , endif
|
||||
IF C,R7,EQ,=F'3' THEN if j=3 then
|
||||
LR R1,R9 c
|
||||
SLA R1,2 ~
|
||||
L R11,WID-L'WID(R1) wid(c)
|
||||
SR R11,R10 -l
|
||||
SRA R11,1 /2
|
||||
A R11,IB ibx=ib+(wid(c)-l)/2
|
||||
ENDIF , endif
|
||||
LA R2,PG-1 @pg
|
||||
AR R2,R11 +ibx
|
||||
LR R3,R10 l
|
||||
LA R4,W @w
|
||||
LR R5,R10 l
|
||||
MVCL R2,R4 substr(pg,ibx,l)=substr(w,1,l)
|
||||
LR R1,R9 c
|
||||
SLA R1,2 ~
|
||||
L R2,WID-L'WID(R1) wid(c)
|
||||
A R2,IB +ib
|
||||
LA R2,1(R2) +1
|
||||
ST R2,IB ib=ib+wid(c)+1
|
||||
LA R9,1(R9) c++
|
||||
ENDDO , enddo c
|
||||
XPRNT PG,L'PG print
|
||||
LA R8,1(R8) r++
|
||||
ENDDO , enddo r
|
||||
LA R7,1(R7) j++
|
||||
ENDDO , enddo j
|
||||
L R13,4(0,R13) restore previous savearea pointer
|
||||
RETURN (14,12),RC=0 restore registers from calling sav
|
||||
SEQ EQU * --begin seq
|
||||
LA R9,1(R9) c=c+1
|
||||
LR R10,R6 i
|
||||
S R10,I0 l=i-i0
|
||||
LA R4,WT-1 @wt
|
||||
A R4,I0 +i0
|
||||
LR R5,R10 l
|
||||
ICM R5,B'1000',=C' ' padding
|
||||
LR R1,R8 r
|
||||
BCTR R1,0 -1
|
||||
MH R1,=AL2(NJ) ~
|
||||
AR R1,R9 +c
|
||||
MH R1,=AL2(L'WOR) ~
|
||||
LA R2,WOR-L'WOR(R1) @wor(r,c)
|
||||
LA R3,L'WOR length(wor)
|
||||
MVCL R2,R4 wor(r,c)=substr(wt,i0,l)
|
||||
LR R1,R9 c
|
||||
SLA R1,2 ~
|
||||
L R2,WID-L'WID(R1) wid(c)
|
||||
IF CR,R2,LT,R10 THEN if l>wid(c) then
|
||||
LR R1,R9 c
|
||||
SLA R1,2 ~
|
||||
ST R10,WID-L'WID(R1) wid(c)=l
|
||||
ENDIF , endif
|
||||
BR R14 --end seq
|
||||
NI EQU 6 ni
|
||||
NJ EQU 12 nj
|
||||
T DC CL68'Given$a$text$file$of$many$lines,$where$fields$within$a$line$'
|
||||
DC CL68'are$delineated$by$a$single$''dollar''$character,$write$a$progX
|
||||
ramm'
|
||||
DC CL68'that$aligns$each$column$of$fields$by$ensuring$that$words$in$eX
|
||||
ach$'
|
||||
DC CL68'column$are$separated$by$at$least$one$space.'
|
||||
DC CL68'Further,$allow$for$each$word$in$a$column$to$be$either$left$'
|
||||
DC CL68'justified,$right$justified,$or$center$justified$within$its$coX
|
||||
lumn.'
|
||||
WOR DC (NI*NJ)CL10' ' wor(ni,nj) char(10)
|
||||
WID DC 16F'0' wid(16)
|
||||
COLS DC F'0'
|
||||
ROWS DC F'0'
|
||||
WT DS CL(L'T)
|
||||
W DS CL(L'WOR)
|
||||
CI DS CL1
|
||||
I0 DS F
|
||||
IB DS F
|
||||
PG DS CL120
|
||||
REGEQU
|
||||
END ALICOL
|
||||
|
|
@ -1,50 +1,48 @@
|
|||
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.
|
||||
Alignment := "L" ; Options: L, R, C
|
||||
Text =
|
||||
( LTrim
|
||||
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.
|
||||
)
|
||||
|
||||
Clipboard := ColumnJustify(lines, "l")
|
||||
Loop, Parse, Text ; calculate column's width
|
||||
If A_LoopField in $,`n
|
||||
If (N > W)
|
||||
W := N, N := 0
|
||||
Else
|
||||
N := 0
|
||||
Else
|
||||
++N
|
||||
Width := ++W
|
||||
|
||||
MsgBox, , Column Justify, The clipboard now contains the justified text. Paste it into a text editor to see it.
|
||||
|
||||
ColumnJustify(lines, lcr = "l", del="$")
|
||||
Loop, Parse, Text, `n ; process each line
|
||||
{
|
||||
Loop, Parse, lines, `n, `r
|
||||
Loop, Parse, A_LoopField, %del%
|
||||
{
|
||||
If ((t := StrLen(A_LoopField)) > c%A_Index% )
|
||||
c%A_Index% := t
|
||||
If (t > max)
|
||||
max := t
|
||||
}
|
||||
blank := Fill( " ", max )
|
||||
If (lcr = "l") ;left-justify
|
||||
Loop, Parse, lines, `n, `r
|
||||
Loop, Parse, A_LoopField, %del%
|
||||
out .= (A_Index = 1 ? "`n" : " ") SubStr(A_LoopField blank, 1, c%A_Index%)
|
||||
Else If (lcr = "r") ;right-justify
|
||||
Loop, Parse, lines, `n, `r
|
||||
Loop, Parse, A_LoopField, %del%
|
||||
out .= (A_Index = 1 ? "`n" : " ") SubStr(blank A_LoopField, -c%A_Index%+1)
|
||||
Else If (lcr = "c") ;center-justify
|
||||
Loop, Parse, lines, `n, `r
|
||||
Loop, Parse, A_LoopField, %del%
|
||||
out .= (A_Index = 1 ? "`n" : " ") SubStr(blank A_LoopField blank
|
||||
, (Ceil((max * 2 + StrLen(A_LoopField))/2) - Ceil(c%A_Index%/2) + 1)
|
||||
, c%A_Index%)
|
||||
return SubStr(out, 2)
|
||||
Words := StrSplit(A_LoopField, "$")
|
||||
For i, Word in Words ; process each word
|
||||
Line .= Align(Word, Alignment, Width)
|
||||
Result .= RTrim(Line) . "`n"
|
||||
Line := ""
|
||||
}
|
||||
|
||||
Fill(chr, len)
|
||||
{
|
||||
static y
|
||||
if !y
|
||||
VarSetCapacity(x, 64), VarSetCapacity(x, 0), y := True
|
||||
return x, VarSetCapacity(x, len, Asc(chr))
|
||||
Clipboard := Result ; present results
|
||||
MsgBox, The results are in the Clipboard
|
||||
|
||||
Align(Pal, How, Width) { ; function for alignment
|
||||
Length := StrLen(Pal)
|
||||
If (How = "L")
|
||||
Return Pal . Spc(Width - Length)
|
||||
Else If (How = "R")
|
||||
Return Spc(Width - Length) . Pal
|
||||
Else If (How = "C")
|
||||
Return Spc((Width - Length)//2) . Pal . Spc(Width - Length - (Width - Length)//2)
|
||||
}
|
||||
|
||||
Spc(Number) { ; function to concatenate space characters
|
||||
Loop, %Number%
|
||||
Ret .= A_Space
|
||||
Return Ret
|
||||
}
|
||||
|
|
|
|||
83
Task/Align-columns/COBOL/align-columns-1.cobol
Normal file
83
Task/Align-columns/COBOL/align-columns-1.cobol
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
identification division.
|
||||
program-id. AlignColumns.
|
||||
|
||||
data division.
|
||||
working-storage section.
|
||||
*>-> Constants
|
||||
78 MAX-LINES value 6.
|
||||
78 MAX-LINE-SIZE value 66.
|
||||
78 MAX-COLUMNS value 12.
|
||||
78 MAX-COLUMN-SIZE value 16.
|
||||
*>-> Indexes
|
||||
01 w-idx pic is 9(2).
|
||||
01 w-idy pic is 9(2).
|
||||
01 w-pos pic is 9(3).
|
||||
*>-> Data structures
|
||||
01 w-lines.
|
||||
05 w-line pic is x(MAX-LINE-SIZE) occurs MAX-LINES.
|
||||
01 w-column-sizes.
|
||||
05 w-column-size pic is 99 occurs MAX-COLUMNS value zeros.
|
||||
01 w-matrix.
|
||||
05 filler occurs MAX-LINES.
|
||||
10 filler occurs MAX-COLUMNS.
|
||||
15 w-content pic is x(MAX-COLUMN-SIZE).
|
||||
*>-> Output
|
||||
01 w-line-out pic is x(120).
|
||||
*>-> Data alignment
|
||||
01 w-alignment pic is x(1).
|
||||
88 alignment-left value is "L".
|
||||
88 alignment-center value is "C".
|
||||
88 alignment-right value is "R".
|
||||
|
||||
procedure division.
|
||||
main.
|
||||
move "Given$a$text$file$of$many$lines,$where$fields$within$a$line$" to w-line(1)
|
||||
move "are$delineated$by$a$single$'dollar'$character,$write$a$program" to w-line(2)
|
||||
move "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$" to w-line(3)
|
||||
move "column$are$separated$by$at$least$one$space." to w-line(4)
|
||||
move "Further,$allow$for$each$word$in$a$column$to$be$either$left$" to w-line(5)
|
||||
move "justified,$right$justified,$or$center$justified$within$its$column." to w-line(6)
|
||||
perform calculate-size-columns
|
||||
set alignment-left to true
|
||||
perform show-content
|
||||
set alignment-center to true
|
||||
perform show-content
|
||||
set alignment-right to true
|
||||
perform show-content
|
||||
goback
|
||||
.
|
||||
calculate-size-columns.
|
||||
perform
|
||||
varying w-idx from 1 by 1
|
||||
until w-idx > MAX-LINES
|
||||
unstring w-line(w-idx) delimited by "$" into w-content(w-idx, 1), w-content(w-idx, 2),
|
||||
w-content(w-idx, 3), w-content(w-idx, 4), w-content(w-idx, 5), w-content(w-idx, 6),
|
||||
w-content(w-idx, 7), w-content(w-idx, 8), w-content(w-idx, 9), w-content(w-idx, 10),
|
||||
w-content(w-idx, 11), w-content(w-idx, 12),
|
||||
perform
|
||||
varying w-idy from 1 by 1
|
||||
until w-idy > MAX-COLUMNS
|
||||
if function stored-char-length(w-content(w-idx, w-idy)) > w-column-size(w-idy)
|
||||
move function stored-char-length(w-content(w-idx, w-idy)) to w-column-size(w-idy)
|
||||
end-if
|
||||
end-perform
|
||||
end-perform
|
||||
.
|
||||
show-content.
|
||||
move all "-" to w-line-out
|
||||
display w-line-out
|
||||
perform
|
||||
varying w-idx from 1 by 1
|
||||
until w-idx > MAX-LINES
|
||||
move spaces to w-line-out
|
||||
move 1 to w-pos
|
||||
perform
|
||||
varying w-idy from 1 by 1
|
||||
until w-idy > MAX-COLUMNS
|
||||
call "C$JUSTIFY" using w-content(w-idx, w-idy)(1:w-column-size(w-idy)), w-alignment
|
||||
move w-content(w-idx, w-idy) to w-line-out(w-pos:w-column-size(w-idy))
|
||||
compute w-pos = w-pos + w-column-size(w-idy) + 1
|
||||
end-perform
|
||||
display w-line-out
|
||||
end-perform
|
||||
.
|
||||
21
Task/Align-columns/COBOL/align-columns-2.cobol
Normal file
21
Task/Align-columns/COBOL/align-columns-2.cobol
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
------------------------------------------------------------------------------------------------------------------------
|
||||
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.
|
||||
------------------------------------------------------------------------------------------------------------------------
|
||||
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.
|
||||
40
Task/Align-columns/Harbour/align-columns-1.harbour
Normal file
40
Task/Align-columns/Harbour/align-columns-1.harbour
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
PROCEDURE Main()
|
||||
LOCAL a := { "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." }
|
||||
LOCAL e, nMax
|
||||
|
||||
// remove trailing dollars
|
||||
AEval( a, {|e,n| Iif( Right(e,1)=="$", a[n] := hb_StrShrink( e, 1 ), NIL ) } )
|
||||
|
||||
// find max word length
|
||||
nMax := 0
|
||||
AEval( a, {|e| AEval( hb_Atokens( e, "$"), {|i| nMax := Max( nMax, Len(i) )} ) } )
|
||||
nMax++
|
||||
|
||||
// start printing, padding words as needed
|
||||
?
|
||||
? "----Left aligned columns----"
|
||||
FOR EACH e IN a
|
||||
?
|
||||
AEval( hb_Atokens( e, "$"), {|i| QQout( PadR(i, nMax) )} )
|
||||
NEXT
|
||||
|
||||
?
|
||||
? "----Center aligned columns----"
|
||||
FOR EACH e IN a
|
||||
?
|
||||
AEval( hb_Atokens( e, "$"), {|i| QQout( PadC(i, nMax) )} )
|
||||
NEXT
|
||||
|
||||
?
|
||||
? "----Right aligned columns----"
|
||||
FOR EACH e IN a
|
||||
?
|
||||
AEval( hb_Atokens( e, "$"), {|i| QQout( PadL(i, nMax) )} )
|
||||
NEXT
|
||||
|
||||
RETURN
|
||||
23
Task/Align-columns/Harbour/align-columns-2.harbour
Normal file
23
Task/Align-columns/Harbour/align-columns-2.harbour
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
----Left aligned columns----
|
||||
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.
|
||||
|
||||
----Center aligned columns----
|
||||
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.
|
||||
|
||||
----Right aligned columns----
|
||||
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.
|
||||
|
|
@ -1,21 +1,18 @@
|
|||
txt = """Given$a$txt$file$of$many$lines,$where$fields$within$a$line$
|
||||
'''
|
||||
cat <<'EOF' > align_columns.dat
|
||||
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."""
|
||||
justified,$right$justified,$or$center$justified$within$its$column.
|
||||
EOF
|
||||
'''
|
||||
|
||||
parts = [line.rstrip("$").split("$") for line in txt.splitlines()]
|
||||
|
||||
max_widths = {}
|
||||
for line in parts:
|
||||
for i, word in enumerate(line):
|
||||
max_widths[i] = max(max_widths.get(i, 0), len(word))
|
||||
|
||||
for i, justify in enumerate([str.ljust, str.center, str.rjust]):
|
||||
print ["Left", "Center", "Right"][i], " column-aligned output:\n"
|
||||
for line in parts:
|
||||
for j, word in enumerate(line):
|
||||
print justify(word, max_widths[j]),
|
||||
print
|
||||
print "- " * 52
|
||||
for align in '<^>':
|
||||
rows = [ line.strip().split('$') for line in open('align_columns.dat') ]
|
||||
fmts = [ '{:%s%d}' % (align, max( len(row[i]) if i < len(row) else 0 for row in rows ))
|
||||
for i in range(max(map(len, rows))) ]
|
||||
for row in rows:
|
||||
print(' '.join(fmts).format(*(row + [''] * len(fmts))))
|
||||
print('')
|
||||
|
|
|
|||
|
|
@ -1,18 +1,21 @@
|
|||
'''
|
||||
cat <<'EOF' > align_columns.dat
|
||||
Given$a$text$file$of$many$lines,$where$fields$within$a$line$
|
||||
txt = """Given$a$txt$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
|
||||
'''
|
||||
justified,$right$justified,$or$center$justified$within$its$column."""
|
||||
|
||||
for align in '<^>':
|
||||
rows = [ line.strip().split('$') for line in open('align_columns.dat') ]
|
||||
fmts = [ '{:%s%d}' % (align, max( len(row[i]) if i < len(row) else 0 for row in rows ))
|
||||
for i in range(max(map(len, rows))) ]
|
||||
for row in rows:
|
||||
print(' '.join(fmts).format(*(row + [''] * len(fmts))))
|
||||
print('')
|
||||
parts = [line.rstrip("$").split("$") for line in txt.splitlines()]
|
||||
|
||||
max_widths = {}
|
||||
for line in parts:
|
||||
for i, word in enumerate(line):
|
||||
max_widths[i] = max(max_widths.get(i, 0), len(word))
|
||||
|
||||
for i, justify in enumerate([str.ljust, str.center, str.rjust]):
|
||||
print ["Left", "Center", "Right"][i], " column-aligned output:\n"
|
||||
for line in parts:
|
||||
for j, word in enumerate(line):
|
||||
print justify(word, max_widths[j]),
|
||||
print
|
||||
print "- " * 52
|
||||
|
|
|
|||
75
Task/Align-columns/Python/align-columns-4.py
Normal file
75
Task/Align-columns/Python/align-columns-4.py
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
'''Variously aligned columns
|
||||
from delimited text.
|
||||
'''
|
||||
|
||||
from functools import reduce
|
||||
from itertools import repeat
|
||||
|
||||
|
||||
# TEST ----------------------------------------------------
|
||||
# main :: IO ()
|
||||
def main():
|
||||
'''Test of three alignments.'''
|
||||
|
||||
txt = '''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.'''
|
||||
|
||||
rows = [x.split('$') for x in txt.splitlines()]
|
||||
table = paddedRows(max(map(len, rows)))('')(rows)
|
||||
|
||||
print('\n\n'.join(map(
|
||||
alignedTable(table)(' '),
|
||||
[-1, 0, 1] # Left, Center, Right
|
||||
)))
|
||||
|
||||
|
||||
# alignedTable :: [[String]] -> Alignment -> String -> String
|
||||
def alignedTable(rows):
|
||||
'''Tabulation of rows of cells, with cell alignment
|
||||
specified by:
|
||||
eAlign -1 = left
|
||||
eAlign 0 = center
|
||||
eAlign 1 = right
|
||||
and separator between columns
|
||||
supplied by the `sep` argument.
|
||||
'''
|
||||
def go(sep, eAlign):
|
||||
lcr = ['ljust', 'center', 'rjust'][1 + eAlign]
|
||||
|
||||
# nextAlignedCol :: [[String]] -> [String] -> [[String]]
|
||||
def nextAlignedCol(cols, col):
|
||||
w = max(len(cell) for cell in col)
|
||||
return cols + [
|
||||
[getattr(s, lcr)(w, ' ') for s in col]
|
||||
]
|
||||
|
||||
return '\n'.join([
|
||||
sep.join(cells) for cells in
|
||||
zip(*reduce(nextAlignedCol, zip(*rows), []))
|
||||
])
|
||||
return lambda sep: lambda eAlign: go(sep, eAlign)
|
||||
|
||||
|
||||
# GENERIC -------------------------------------------------
|
||||
|
||||
# paddedRows :: Int -> a -> [[a]] -> [[a]]
|
||||
def paddedRows(n):
|
||||
'''A list of rows of even length,
|
||||
in which each may be padded (but
|
||||
not truncated) to length n with
|
||||
appended copies of value v.'''
|
||||
def go(v, xs):
|
||||
def pad(x):
|
||||
d = n - len(x)
|
||||
return (x + list(repeat(v, d))) if 0 < d else x
|
||||
return [pad(row) for row in xs]
|
||||
return lambda v: lambda xs: go(v, xs) if xs else []
|
||||
|
||||
|
||||
# MAIN ---
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
62
Task/Align-columns/VBScript/align-columns.vb
Normal file
62
Task/Align-columns/VBScript/align-columns.vb
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
' Align columns - RC - VBScript
|
||||
Const nr=16, nc=16
|
||||
ReDim d(nc),t(nr), wor(nr,nc)
|
||||
i=i+1: t(i) = "Given$a$text$file$of$many$lines,$where$fields$within$a$line$"
|
||||
i=i+1: t(i) = "are$delineated$by$a$single$'dollar'$character,$write$a$program"
|
||||
i=i+1: t(i) = "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$"
|
||||
i=i+1: t(i) = "column$are$separated$by$at$least$one$space."
|
||||
i=i+1: t(i) = "Further,$allow$for$each$word$in$a$column$To$be$either$left$"
|
||||
i=i+1: t(i) = "justified,$right$justified,$or$center$justified$within$its$column."
|
||||
For r=1 to nr
|
||||
If t(r)="" Then Exit For
|
||||
w=xRTrim(t(r),"$")
|
||||
m=Split(w,"$")
|
||||
For c=1 To UBound(m)+1
|
||||
wor(r,c)=m(c-1)
|
||||
If Len(wor(r,c))>d(c) Then d(c)=Len(wor(r,c))
|
||||
Next 'c
|
||||
If c>cols Then cols=c
|
||||
Next 'r
|
||||
rows=r-1
|
||||
tt=Array("Left","Right","Center")
|
||||
For n=1 To 3
|
||||
Wscript.Echo
|
||||
Wscript.Echo "*****" & tt(n-1) & "*****"
|
||||
For r=1 To rows
|
||||
w=""
|
||||
For c=1 To cols
|
||||
x=wor(r,c): s=Space(d(c))
|
||||
Select Case n
|
||||
Case 1: w=w &" "& Left (x & s,d(c))
|
||||
Case 2: w=w &" "& Right (s & x,d(c))
|
||||
Case 3: w=w &" "& xCentre(x,d(c)," ")
|
||||
End Select 'n
|
||||
Next 'c
|
||||
Wscript.Echo Mid(w,2)
|
||||
Next 'r
|
||||
Next 'n
|
||||
|
||||
Function xCentre(c, n, Pad)
|
||||
Dim j
|
||||
If n > Len(c) Then
|
||||
j = (n - Len(c)) \ 2
|
||||
If (n - Len(c)) Mod 2 <> 0 Then j = j + 1
|
||||
xCentre = Mid(String(j, Pad) & c & String(j, Pad), 1, n)
|
||||
Else
|
||||
xCentre = c
|
||||
End If
|
||||
End Function 'xCentre
|
||||
|
||||
Function xRTrim(c, Pad)
|
||||
Dim i2, l, cc
|
||||
cc = "": l = Len(c)
|
||||
If l > 0 Then
|
||||
i2 = l
|
||||
Do While (Mid(c, i2, 1) = Pad And i2 > 1)
|
||||
i2 = i2 - 1
|
||||
Loop
|
||||
If i2 = 1 And Mid(c, i2, 1) = Pad Then i2 = 0
|
||||
If i2 > 0 Then cc = Mid(c, 1, i2)
|
||||
End If
|
||||
xRTrim = cc
|
||||
End Function 'xRTrim
|
||||
Loading…
Add table
Add a link
Reference in a new issue