Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
|
|
@ -1,45 +1,19 @@
|
|||
from StringIO import StringIO
|
||||
from itertools import zip_longest
|
||||
|
||||
textinfile = '''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.'''
|
||||
justified,$right$justified,$or$center$justified$within$its$column."""
|
||||
|
||||
j2justifier = dict(L=str.ljust, R=str.rjust, C=str.center)
|
||||
parts = [line.rstrip("$").split("$") for line in txt.splitlines()]
|
||||
widths = [max(len(word) for word in col)
|
||||
for col in zip_longest(*parts, fillvalue='')]
|
||||
|
||||
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])
|
||||
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue