Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,44 @@
quote | 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.|
var, raw-text
[] var, data
var width
: read-and-parse \ --
raw-text @
( "$" s:/ data @ swap a:push drop )
s:eachline ;
: find-widest \ -- n
data @ ( ( swap s:len nip n:max ) swap a:reduce ) 0 a:reduce ;
: print-data \ fmt --
width @ swap s:strfmt >r
data @
(
nip
(
nip
r@ s:strfmt .
) a:each drop
cr
) a:each drop rdrop ;
: app:main
read-and-parse
\ find widest column, and add one for the space:
find-widest n:1+ width !
\ print the data
cr "right:" . cr "%%>%ds" print-data
cr "left:" . cr "%%<%ds" print-data
cr "center:" . cr "%%|%ds" print-data
bye ;

View file

@ -0,0 +1,98 @@
' FB 1.05.0 Win64
Sub Split(s As String, sep As String, result() As String)
Dim As Integer i, j, count = 0
Dim temp As String
Dim As Integer position(Len(s) + 1)
position(0) = 0
For i = 0 To Len(s) - 1
For j = 0 To Len(sep) - 1
If s[i] = sep[j] Then
count += 1
position(count) = i + 1
End If
Next j
Next i
position(count + 1) = Len(s) + 1
Redim result(count)
For i = 1 To count + 1
result(i - 1) = Mid(s, position(i - 1) + 1, position(i) - position(i - 1) - 1)
Next
End Sub
Sub CSet(buffer As String, s As Const String)
Dim As Integer bLength = Len(buffer)
Dim As Integer sLength = Len(s)
Dim As Integer diff, lSpaces
If sLength >= bLength Then
LSet buffer, s
Else
diff = bLength - sLength
lSpaces = diff \ 2
LSet buffer, Space(lSpaces) + s
End If
End Sub
Dim lines() As String
Dim count As Integer = 0
Open "align_columns.txt" For Input As #1
While Not Eof(1)
Redim Preserve lines(count)
Line Input #1, lines(count)
count +=1
Wend
Close #1
Dim As Integer i,j, length, numColumns = 0
Dim As Integer numLines = UBound(lines) + 1
Dim fields() As String
' Work out the maximum number of columns
For i = 0 To numLines - 1
Erase fields
Split RTrim(lines(i), "$"), "$", fields()
length = UBound(fields) + 1
If length > numColumns Then numColumns = length
Next
' Split lines into fields and work out maximum size of each column
Dim matrix(numLines - 1, numColumns - 1) As String
Dim columnSizes(numColumns - 1) As Integer
For i = 0 To numLines - 1
Erase fields
Split RTrim(lines(i), "$"), "$", fields()
For j = 0 To UBound(fields)
matrix(i, j) = fields(j)
length = Len(fields(j))
If length > columnSizes(j) Then columnSizes(j) = length
Next j
Next i
Dim buffer As String
'Separate each column by 2 spaces
Open "align_left_columns.txt" For Output As #1
Open "align_right_columns.txt" For Output As #2
Open "align_center_columns.txt" For Output As #3
For i = 0 To UBound(matrix, 1)
For j = 0 To UBound(matrix, 2)
buffer = Space(columnSizes(j))
LSet buffer, matrix(i, j)
Print #1, buffer;
RSet buffer, matrix(i, j)
Print #2, buffer;
CSet buffer, matrix(i, j)
Print #3, buffer;
If j < UBound(matrix, 2) Then
Print #1, " "; : Print #2, " "; : Print #3, " ";
End If
Next j
Print #1, : Print #2, : Print #3,
Next i
Close #1 : Close #2 : Close #3

View file

@ -0,0 +1,67 @@
#!/usr/bin/lasso9
local(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.
")
define go_left(text::array, width::integer) => {
local(output = string)
with row in #text do {
with word in #row do {
#output -> append(string(#word) -> padtrailing(#width + 1)&)
}
#output -> append('\n')
}
return #output
}
define go_right(text::array, width::integer) => {
local(output = string)
with row in #text do {
with word in #row do {
#output -> append(string(#word) -> padleading(#width + 1)&)
}
#output -> append('\n')
}
return #output
}
define go_center(text::array, width::integer) => {
local(output = string)
with row in #text do {
with word in #row do {
local(
padlength = (#width + 1 - #word -> size),
padleft = (' ' * (#padlength / 2)),
padright = (' ' * (#padlength - #padleft -> size))
)
#output -> append(#padleft + string(#word) + #padright)
}
#output -> append('\n')
}
return #output
}
define prepcols(text::string) => {
local(
result = array,
maxwidth = 0
)
with row in #text -> split('\n') do {
#row -> removetrailing('$')
#result -> insert(#row -> split('$'))
}
with word in delve(#result) do {
#word -> size > #maxwidth ? #maxwidth = #word -> size
}
stdoutnl('Left aligned result: \n' + go_left(#result, #maxwidth))
stdoutnl('Right aligned result: \n' + go_right(#result, #maxwidth))
stdoutnl('Centered result: \n' + go_center(#result, #maxwidth))
}
prepcols(#text)

View file

@ -0,0 +1,22 @@
import strutils, sequtils, strfmt
let textinfile = """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."""
var words = textinfile.splitLines.mapIt(seq[string], it.split '$')
var maxs = newSeq[int](max words.mapIt(int, it.len))
for l in words:
for j,w in l:
maxs[j] = max(maxs[j], w.len+1)
for i, align in ["<",">","^"]:
echo(["Left", "Right", "Center"][i], " column-aligned output:")
for l in words:
for j,w in l:
stdout.write w.format align & $maxs[j]
stdout.write "\n"

View file

@ -0,0 +1,15 @@
String method: justify(n, just) // ( just size string -- string )
| 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 - ) ;
: align(just)
| lines maxsize |
File new("align.txt") map(#[ wordsWith('$') ]) ->lines
0 lines apply(#[ apply(#[ size max ]) ]) ->maxsize
lines apply(#[ apply(#[ justify(maxsize, just) . ]) printcr ]) ;

View 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()

View file

@ -0,0 +1,59 @@
Red [
Title: "Align Columns"
Original-Author: oofoe
]
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.}
; Parse specimen into data grid.
data: copy []
foreach line split text lf [
append/only data split line "$"
]
; Compute independent widths for each column.
widths: copy []
foreach line data [
forall line [
i: index? line
if i > length? widths [append widths 0]
widths/:i: max widths/:i length? line/1
]
]
pad: function [n] [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: function [n s] [
d: n - length? s
h: round/down d / 2
rejoin [pad h s pad d - h]
]
; 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 [newline "Align" i "..." newline] entable data get i]

View file

@ -0,0 +1,47 @@
class Format(text, width) {
method align(j) {
text.map { |row|
row.range.map { |i|
'%-*s ' % (width[i],
'%*s' % (row[i].len + (width[i]-row[i].len * j/2), row[i]));
}.join("");
}.join("\n") + "\n";
}
}
func Formatter(text) {
var textArr = [];
var widthArr = [];
text.each_line {
var words = .split('$');
textArr.append(words);
words.each_kv { |i, word|
if (i == widthArr.len) {
widthArr.append(word.len);
}
elsif (word.len > widthArr[i]) {
widthArr[i] = word.len;
}
}
}
return Format(textArr, widthArr);
}
enum |left, middle, right|;
const text = <<'EOT';
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.
EOT
var f = Formatter(text);
say f.align(left);
say f.align(middle);
say f.align(right);

View file

@ -0,0 +1,43 @@
# transpose a possibly jagged matrix
def transpose:
if . == [] then []
else (.[1:] | transpose) as $t
| .[0] as $row
| reduce range(0; [($t|length), (.[0]|length)] | max) as $i
([]; . + [ [ $row[$i] ] + $t[$i] ])
end;
# left/right/center justification of strings:
def ljust(width): . + " " * (width - length);
def rjust(width): " " * (width - length) + .;
def center(width):
(width - length) as $pad
| if $pad <= 0 then .
else ($pad / 2 | floor) as $half
| $half * " " + . + ($pad-$half) * " "
end ;
# input: a single string, which includes newlines to separate lines, and $ to separate phrases;
# method must be "left" "right" or anything else for central justification.
def format(method):
def justify(width):
if method == "left" then ljust(width)
elif method == "right" then rjust(width)
else center(width)
end;
# max_widths: input: an array of strings, each with "$" as phrase-separator;
# return the appropriate column-wise maximum lengths
def max_widths:
map(split("$") | map(length))
| transpose | map(max) ;
split("\n") as $input
| $input
| (max_widths | map(.+1)) as $widths
| map( split("$") | . as $line | reduce range(0; length) as $i
(""; . + ($line[$i]|justify($widths[$i])) ))
| join("\n")
;

View file

@ -0,0 +1,3 @@
"Center:", format("center"), "",
"Left:", format("left"), "",
"Right:", format("right")

View file

@ -0,0 +1,24 @@
$ jq -M -R -r -s -f Align_columns.jq Align_columns.txt
Center:
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.
Left:
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:
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.