Skip to content

Instantly share code, notes, and snippets.

@ksylvan
Created March 28, 2020 06:46
Show Gist options
  • Save ksylvan/4097c0c96e8bb4ff906b5609b814230f to your computer and use it in GitHub Desktop.
Save ksylvan/4097c0c96e8bb4ff906b5609b814230f to your computer and use it in GitHub Desktop.
Justified Text question
# Question Description: Write a function that takes, as arguments, an integer representing line length in
# characters and a (long) string of text and returns the text with spaces and line breaks to produce a block of
# text justified to the given line length. For example, if the function were called as
# justify(25, "This is some sample text, really just enough to generate a few lines in the output to show what the text justify function is supposed to do.")
# This is some sample text,
# really just enough to
# generate a few lines in
# the output to show what
# the text justify function
# is supposed to do.
class JustifiedFormatter:
class Line:
def __init__(self, n):
self.__l = []
self.__strLength = 0
self.__n = n
def __len__(self):
add_to_len = 0
if len(self.__l) > 1:
add_to_len = len(self.__l) - 1
return self.__strLength + add_to_len
def append(self, s):
self.__l.append(s)
self.__strLength += len(s)
def justified(self):
if len(self.__l) == 0:
return ""
if len(self.__l) == 1:
return self.__l[0]
# At this point, we know the Line has at least 2 words.
spaces = self.__n - self.__strLength
d, r = divmod(spaces, (len(self.__l) - 1))
s = ""
for w in self.__l[:-1]:
s += (w + d*" ")
if r:
s += " "
r -= 1
s += self.__l[-1]
return s
def notJustified(self):
return " ".join(self.__l)
def __init__(self, n, s):
self.__a = s.split()
self.__n = n
def __iter(self):
# iterable generator function
n = self.__n
l = JustifiedFormatter.Line(n)
for w in self.__a:
if len(w) <= (n - len(l)):
l.append(w)
continue
yield l.justified()
l = JustifiedFormatter.Line(n)
l.append(w)
if len(l):
yield l.notJustified()
def __iter__(self):
return self.__iter()
def justify(n, s):
return "\n".join(JustifiedFormatter(n, s))
if __name__ == "__main__":
tests = [
{
'n': 25,
'i': "This is some sample text, really just enough to " +
"generate a few lines in the output to show what " +
"the text justify function is supposed to do.",
'o': """This is some sample text,
really just enough to
generate a few lines in
the output to show what
the text justify function
is supposed to do."""
},
{
'n': 10, 'i': "", 'o': ""
},
{
'n': 20, 'i': "JustOneWord", 'o': "JustOneWord"
}
]
for t in tests:
o = justify(t['n'], t['i'])
if o == t['o']:
print("OK.")
else:
print("FAILED!")
print(f"Input: {t['i']}")
print(f"Output was:\n\"{o}\"")
print(f"Expected:\n\"{t['o']}\"")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment