Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
67
Task/Align-columns/Phix/align-columns.phix
Normal file
67
Task/Align-columns/Phix/align-columns.phix
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
constant data = {
|
||||
"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."
|
||||
}
|
||||
|
||||
function split(sequence s, integer c)
|
||||
sequence out = {}
|
||||
integer first = 1, delim
|
||||
while first<=length(s) do
|
||||
delim = find_from(c,s,first)
|
||||
if delim = 0 then
|
||||
delim = length(s)+1
|
||||
end if
|
||||
out = append(out,s[first..delim-1])
|
||||
first = delim + 1
|
||||
end while
|
||||
return out
|
||||
end function
|
||||
|
||||
function align(sequence s, integer width, integer alignment)
|
||||
integer n = width-length(s)
|
||||
if n<=0 then
|
||||
return s
|
||||
elsif alignment<0 then
|
||||
return s & repeat(' ', n)
|
||||
elsif alignment>0 then
|
||||
return repeat(' ', n) & s
|
||||
else
|
||||
-- (PL if I'd written this, I'd have n-floor(n/2) on the rhs)
|
||||
return repeat(' ', floor(n/2)) & s & repeat(' ', floor(n/2+0.5))
|
||||
end if
|
||||
end function
|
||||
|
||||
procedure AlignColumns()
|
||||
integer llij
|
||||
sequence lines, li
|
||||
sequence maxlens = {}
|
||||
lines = repeat(0,length(data))
|
||||
for i=1 to length(data) do
|
||||
li = split(data[i],'$')
|
||||
lines[i] = li
|
||||
if length(li)>length(maxlens) then
|
||||
maxlens &= repeat(0,length(li)-length(maxlens))
|
||||
end if
|
||||
for j=1 to length(li) do
|
||||
llij = length(li[j])
|
||||
if llij>maxlens[j] then maxlens[j] = llij end if
|
||||
end for
|
||||
end for
|
||||
|
||||
for a=-1 to 1 do -- (alignment = left/centre/right)
|
||||
for i=1 to length(lines) do
|
||||
for j=1 to length(lines[i]) do
|
||||
puts(1, align(lines[i][j],maxlens[j],a) & ' ')
|
||||
end for
|
||||
puts(1,'\n')
|
||||
end for
|
||||
puts(1,'\n')
|
||||
end for
|
||||
if getc(0) then end if
|
||||
end procedure
|
||||
|
||||
AlignColumns()
|
||||
Loading…
Add table
Add a link
Reference in a new issue