Skip to content

Commit

Permalink
refactor(inflow.py): move parse lines to function
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-huan committed Jan 20, 2024
1 parent 0ab68ce commit ad7f842
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions inflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,25 @@ def parse_prefix(lines: list[str]) -> tuple[list[str], str]:
return par, prefix


if __name__ == "__main__":
# read command line arguments - one parameter, width
width = int(sys.argv[1]) if len(sys.argv) > 1 else 79

# read input into paragraph blocks, making empty lines []
pars, lines = [], []
for line in sys.stdin:
def parse_lines(lines, width: int) -> list[tuple[list[str], int, str]]:
"""Read input lines into paragraphs, making empty lines []."""
pars, par = [], []
for line in lines:
if line != "\n":
lines.append(line)
par.append(line)
else:
if len(lines) > 0:
par, prefix = parse_prefix(lines)
if len(par) > 0:
par, prefix = parse_prefix(par)
pars.append((par, width - len(prefix), prefix))
pars.append(([], width, ""))
par, lines = [], []
if len(lines) > 0:
par, prefix = parse_prefix(lines)
par, par = [], []
if len(par) > 0:
par, prefix = parse_prefix(par)
pars.append((par, width - len(prefix), prefix))
return pars

print("\n".join(map(lambda x: process(*x), pars)))

if __name__ == "__main__":
# read command line arguments - one parameter, width
width = int(sys.argv[1]) if len(sys.argv) > 1 else 79
print("\n".join(map(lambda x: process(*x), parse_lines(sys.stdin, width))))

0 comments on commit ad7f842

Please sign in to comment.