Advertisement
Ihmemies

Rivien tasaus

Oct 16th, 2022 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.21 KB | Source Code | 0 0
  1. # Justifies text rows to n chars
  2.  
  3. SEPARATOR = " "
  4.  
  5. def get_text() -> tuple:
  6.     """
  7.    Reads rows of text from user,
  8.    and reads row char limit
  9.    
  10.    param : none
  11.    return: (list, int) words in a list, line length
  12.    """
  13.     user_rows: list = []
  14.     wordlist: list = []
  15.     chars_per_row = int  
  16.          
  17.     print("Enter text rows. Quit by entering an empty row.")
  18.    
  19.     # read input from user row by row, add rows to list
  20.     while(True):
  21.         user_input = input()
  22.         if len(user_input) > 0:
  23.             user_rows.append(user_input.split(SEPARATOR))
  24.         else:
  25.             chars_per_row = int(input("Enter the number of characters per line: "))          
  26.             break
  27.    
  28.     # extract words from the row list and add them to a new wordlist
  29.     for row in user_rows:
  30.         for word in row:
  31.             if len(word) > 0:      
  32.                 wordlist.append(word.strip())
  33.                    
  34.     return (wordlist, chars_per_row)
  35.  
  36. def justify(wordlist:list, max_chars_per_row: int) -> list:
  37.     """
  38.    Justifies a list of words. Adds words to a row until row is full,
  39.    then justifies the row. Adds a justified row to a list.
  40.    
  41.    wordlist: contains all words in one list
  42.    max_chars_per_row: max line length to justify to
  43.    return: list of justified text rows
  44.    """
  45.     justified_text: list = []
  46.     words_in_row: list = []
  47.     chars_in_row: int = 0
  48.    
  49.     for word in wordlist:
  50.         # fill a list with words until the word count is full
  51.         if chars_in_row+len(word) <= max_chars_per_row:
  52.             words_in_row.append(word)
  53.             chars_in_row += len(word) + 1
  54.         # word count for a row is full, now justify the row
  55.         else:
  56.             # justify a row
  57.             justified_text.append(add_extra_separators(words_in_row, max_chars_per_row))
  58.             words_in_row.clear()
  59.            
  60.             # .. and start filling a new row    
  61.             words_in_row.append(word)
  62.             chars_in_row = 0 + len(word) + 1              
  63.    
  64.     # add last row as a special case without justifcation
  65.     last_row: str = ""
  66.     for word in words_in_row:
  67.         last_row += word + SEPARATOR
  68.    
  69.     last_row.rstrip()          
  70.     justified_text.append(last_row)
  71.        
  72.     return justified_text
  73.  
  74. def add_extra_separators(words_in_row: list, max_char_per_row: int) -> str:
  75.     """
  76.    Evenly distributes the amount of separators between two words.
  77.    Adds "leftover" separators as +1 separator between first words,
  78.    as long as they last.
  79.    
  80.    words_in_row: list of words in a row
  81.    max_char_per_row: char limit of a row
  82.    return: str, fully justified row
  83.    """
  84.     i: int = 0
  85.  
  86.     # if there is only one word in a row, don't justify it
  87.     spaces_between_words = len(words_in_row)-1    
  88.     if spaces_between_words == 0:        
  89.         return words_in_row[0]
  90.    
  91.     # subtract total length of all words from the char limit
  92.     amount_of_separators_required = max_char_per_row - sum(len(i) for i in words_in_row)      
  93.  
  94.     # how many separators is actually required for even spacing    
  95.     n_separators = amount_of_separators_required // spaces_between_words
  96.     # how many +1 leftover separators there are between words
  97.     plus_one_separator_times = amount_of_separators_required % spaces_between_words
  98.        
  99.     justified_row = ""      
  100.  
  101.     # goes through the wordlist, adding words to string one by one
  102.     while i <= spaces_between_words:
  103.         justified_row += words_in_row[i]
  104.        
  105.         # adds n separators after added word for even spacing
  106.         if i < spaces_between_words:
  107.             justified_row += n_separators*SEPARATOR
  108.            
  109.             # adds a leftover separator after a word
  110.             if plus_one_separator_times > 0:
  111.                 justified_row += SEPARATOR
  112.                 plus_one_separator_times -= 1
  113.            
  114.         i += 1
  115.        
  116.     return justified_row
  117.  
  118. def main():
  119.     # get input from user (text and row char limit)
  120.     result = get_text()    
  121.     justified_text = justify(result[0], result[1])
  122.    
  123.     # print the justified text
  124.     for row in justified_text:
  125.         print(row)
  126.  
  127. if __name__ == "__main__":
  128.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement