Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,19 @@
from itertools import zip_longest
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."""
parts = [line.rstrip("$").split("$") for line in txt.splitlines()]
widths = [max(len(word) for word in col)
for col in zip_longest(*parts, fillvalue='')]
for justify in "<_Left ^_Center >_Right".split():
j, jtext = justify.split('_')
print(f"{jtext} column-aligned output:\n")
for line in parts:
print(' '.join(f"{wrd:{j}{wdth}}" for wdth, wrd in zip(widths, line)))
print("- " * 52)

View file

@ -0,0 +1,45 @@
from StringIO import StringIO
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.'''
j2justifier = dict(L=str.ljust, R=str.rjust, C=str.center)
def aligner(infile, justification = 'L'):
''' \
Justify columns of textual tabular input where the row separator is the newline
and the field separator is a 'dollar' character.
justification can be L, R, or C; (Left, Right, or Centered).
Return the justified output as a string
'''
assert justification in j2justifier, "justification can be L, R, or C; (Left, Right, or Centered)."
justifier = j2justifier[justification]
fieldsbyrow= [line.strip().split('$') for line in infile]
# pad to same number of fields per row
maxfields = max(len(row) for row in fieldsbyrow)
fieldsbyrow = [fields + ['']*(maxfields - len(fields))
for fields in fieldsbyrow]
# rotate
fieldsbycolumn = zip(*fieldsbyrow)
# calculate max fieldwidth per column
colwidths = [max(len(field) for field in column)
for column in fieldsbycolumn]
# pad fields in columns to colwidth with spaces
fieldsbycolumn = [ [justifier(field, width) for field in column]
for width, column in zip(colwidths, fieldsbycolumn) ]
# rotate again
fieldsbyrow = zip(*fieldsbycolumn)
return "\n".join( " ".join(row) for row in fieldsbyrow)
for align in 'Left Right Center'.split():
infile = StringIO(textinfile)
print "\n# %s Column-aligned output:" % align
print aligner(infile, align[0])

View file

@ -0,0 +1,18 @@
'''
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.
EOF
'''
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('')

View file

@ -0,0 +1,21 @@
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."""
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]), end=' ')
print()
print("- " * 52)

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