Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def output_current_line_data(current_line_length, line_width, current_line):
- number_of_spaces = line_width - current_line_length
- if len(current_line) == 1:
- return current_line[0] + (" " * number_of_spaces)
- number_of_slots = len(current_line) - 1
- spaces_per_slot = int(number_of_spaces / number_of_slots)
- extra_spaces = line_width - current_line_length - (number_of_slots * spaces_per_slot)
- active_output = ""
- for i, current_word in enumerate(current_line):
- active_output += current_word
- if i < len(current_line) - 1:
- active_output += " " * spaces_per_slot
- if extra_spaces:
- active_output += " "
- extra_spaces -= 1
- assert extra_spaces == 0
- return active_output
- def print_full_justified(line_width: int, input: str):
- words = input.split(" ")
- current_line = []
- current_line_length = 0
- # our list of full justified lines
- output = []
- for word in words:
- min_spaces_required = max(0, len(current_line) - 1)
- if len(word) + current_line_length + min_spaces_required < line_width:
- current_line.append(word)
- current_line_length += len(word)
- else:
- line_output = output_current_line_data(current_line_length, line_width, current_line)
- output.append(line_output)
- current_line = []
- current_line_length = 0
- current_line.append(word)
- current_line_length += len(word)
- if current_line:
- line_output = output_current_line_data(current_line_length, line_width, current_line)
- output.append(line_output)
- for line in output:
- print(line)
- if __name__ == '__main__':
- lorem_ipsum = "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."
- # test_data = "officia deserunt mollit anim id"
- print_full_justified(100, lorem_ipsum)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement