From 0674387c2b6fe7574c7b8ec6fe217a6f4e6ed1cd Mon Sep 17 00:00:00 2001 From: "Bradley M. Small" <bradley_small@hotmail.com> Date: Fri, 25 Oct 2019 20:03:40 -0400 Subject: [PATCH] make monolith into sepatate functions --- justify.py | 76 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 26 deletions(-) mode change 100644 => 100755 justify.py diff --git a/justify.py b/justify.py old mode 100644 new mode 100755 index 559cf7a..96503d9 --- a/justify.py +++ b/justify.py @@ -1,3 +1,4 @@ +#!/usr/bin/python3 """ Write a program in Python, that can accept a paragraph string and and page width and return an array of left AND right justified strings. NOTE: No words @@ -11,7 +12,9 @@ Example: Sample input: Paragraph = "This is a sample text but a complicated problem to be solved, so -we are adding more text to see that it actually works." Page Width = 20 +we are adding more text to see that it actually works." + +Page Width = 20 Output should look like this: @@ -24,21 +27,20 @@ Array [5] = "are adding more text" Array [6] = "to see that it" Array [7] = "actually works.†""" +import sys +def justify_text(text, width): + """Justify text to width and return a list of strings.""" + wordlist = text.split() -def main(): - paragraph = """This is a sample text but a complicated problem to be - solved, so we are adding more text to see that it actually works.""" - page_width = 20 - - wordlist = paragraph.split() - + # Build a list of lines that will fit in justified format with room for + # at least one space between each word. lines = [] line = [] length = 0 for word in wordlist: wordlen = len(word) - if length + wordlen <= page_width: + if length + wordlen <= width: line.append(word) length += wordlen else: @@ -47,34 +49,56 @@ def main(): line.append(word) length = wordlen - if length < page_width: + # Allows at least one space between words + if length < width: length += 1 + + # Handles last line, and single word lines if line: lines.append(line) - report = {} - for num, line in enumerate(lines): - llen = 0 - for word in line: - llen += len(word) + apply_spacing(lines, width) + return lines + + +def apply_spacing(lines, width): + """Space out each line to meet the width requirements.""" + for i, line in enumerate(lines): + llen = sum([len(word) for word in line]) - joinspaces = '' - extraspace = '' if len(line)-1: - joinspaces = ' ' * ((page_width - llen) // (len(line)-1)) - extraspace = ' ' * ((page_width - llen) % (len(line)-1)) + joinspaces = ' ' * ((width - llen) // (len(line)-1)) + extraspace = ' ' * ((width - llen) % (len(line)-1)) else: - extraspace = ' ' * (page_width - llen) + joinspaces = '' + extraspace = ' ' * (width - llen) # line[0] += extraspace line[-1] = extraspace + line[-1] - arrstr = "Array [%s]" % str(num+1) - report[arrstr] = joinspaces.join(line) - # print('Array [',num+1,'] = "', joinspaces.join(line), '"', sep='') + lines[i] = joinspaces.join(line) - for key, val in report.items(): - print(key, ' = "', val, '"', sep='') +def show_justified_text_as_array_lines(justified_text): + """Show the justified with 'array number' headers.""" + for i, e in enumerate(justified_text): + print(f'Array [{i+1}] = "{e}"') -main() + +def main(): + """Accept text string and page width and output array list of justified + text.""" + if len(sys.argv) == 3: + paragraph = sys.argv[1] + page_width = int(sys.argv[2]) + else: + paragraph = """This is a sample text but a complicated problem to be + solved, so we are adding more text to see that it actually works.""" + page_width = 20 + + justified_text = justify_text(paragraph, page_width) + show_justified_text_as_array_lines(justified_text) + + +if __name__ == '__main__': + main() -- GitLab