Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
19
Task/Align-columns/Python/align-columns-1.py
Normal file
19
Task/Align-columns/Python/align-columns-1.py
Normal 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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue