Created
September 27, 2017 23:59
-
-
Save dfinzer/423f3a3d1f31a448a315241451df2b30 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def transform_text(text, desired_width): | |
words = text.split(" ") | |
current_line = [] | |
for word in words: | |
current_line_width = sum([len(x) for x in current_line]) | |
num_spaces = len(current_line) - 1 | |
current_line_width_with_spaces = current_line_width + num_spaces | |
if (current_line_width_with_spaces + 1 + len(word)) > desired_width: | |
# Ready to print line. | |
width_diff = desired_width - current_line_width | |
num_spaces = len(current_line) - 1 | |
space_len = width_diff / num_spaces | |
remainder = width_diff % num_spaces | |
spaces = [" " * space_len for s in range(num_spaces)] | |
spaces = [((s + " ") if i < remainder else s) for (i, s) in enumerate(spaces)] | |
output = "" | |
for i, w in enumerate(current_line): | |
output += w | |
if i < len(spaces): | |
output += spaces[i] | |
print output | |
print desired_width * "*" | |
current_line = [word] | |
else: | |
current_line.append(word) | |
if current_line: | |
current_line_width = sum([len(x) for x in current_line]) | |
num_spaces = len(current_line) - 1 | |
current_line_width_with_spaces = current_line_width + num_spaces | |
# Ready to print line. | |
width_diff = desired_width - current_line_width | |
num_spaces = len(current_line) - 1 | |
space_len = width_diff / num_spaces | |
remainder = width_diff % num_spaces | |
spaces = [" " * space_len for s in range(num_spaces)] | |
spaces = [((s + " ") if i < remainder else s) for (i, s) in enumerate(spaces)] | |
output = "" | |
for i, w in enumerate(current_line): | |
output += w | |
if i < len(spaces): | |
output += spaces[i] | |
print output | |
print desired_width * "*" | |
test_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." | |
# 4 gaps to accomodate 7 desired spaces | |
# 1 remainder 3 | |
# 7 / 4 rounded up => 2 | |
# use 2 until using 1 makes sense | |
# implies 2, 2, 2, 1 | |
# 4 gaps to accomodate 25 spaces | |
# 25 / 4 => first gaps will be the answer, last gap will be the remainder | |
# 6, 6, 6, 7 | |
# desired width = 30 | |
# current width = 23 | |
# num words = 5 | |
# expected output = | |
# Loremipsum | |
# Lorem ipsum | |
transform_text(test_text, 30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment