SHARE
TWEET

Untitled

a guest Nov 13th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. def output_current_line_data(current_line_length, line_width, current_line):
  4.     number_of_spaces = line_width - current_line_length
  5.  
  6.     if len(current_line) == 1:
  7.         return current_line[0] + (" " * number_of_spaces)
  8.  
  9.     number_of_slots = len(current_line) - 1
  10.  
  11.     spaces_per_slot = int(number_of_spaces / number_of_slots)
  12.  
  13.     extra_spaces = line_width - current_line_length - (number_of_slots * spaces_per_slot)
  14.  
  15.     active_output = ""
  16.  
  17.     for i, current_word in enumerate(current_line):
  18.         active_output += current_word
  19.  
  20.         if i < len(current_line) - 1:
  21.             active_output += " " * spaces_per_slot
  22.             if extra_spaces:
  23.                 active_output += " "
  24.                 extra_spaces -= 1
  25.  
  26.     assert extra_spaces == 0
  27.  
  28.     return active_output
  29.  
  30.  
  31. def print_full_justified(line_width: int, input: str):
  32.     words = input.split(" ")
  33.  
  34.     current_line = []
  35.     current_line_length = 0
  36.  
  37.     # our list of full justified lines
  38.     output = []
  39.  
  40.     for word in words:
  41.         min_spaces_required = max(0, len(current_line) - 1)
  42.  
  43.         if len(word) + current_line_length + min_spaces_required < line_width:
  44.             current_line.append(word)
  45.             current_line_length += len(word)
  46.         else:
  47.             line_output = output_current_line_data(current_line_length, line_width, current_line)
  48.             output.append(line_output)
  49.  
  50.             current_line = []
  51.             current_line_length = 0
  52.  
  53.             current_line.append(word)
  54.             current_line_length += len(word)
  55.  
  56.     if current_line:
  57.         line_output = output_current_line_data(current_line_length, line_width, current_line)
  58.         output.append(line_output)
  59.  
  60.     for line in output:
  61.         print(line)
  62.  
  63.  
  64. if __name__ == '__main__':
  65.     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."
  66.     # test_data = "officia deserunt mollit anim id"
  67.     print_full_justified(100, lorem_ipsum)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top