Advertisement
WupEly

Untitled

Mar 21st, 2023
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. from copy import deepcopy
  2.  
  3.  
  4. class LeftParagraph:
  5. def __init__(self, n):
  6. self.n = n
  7. self.words = []
  8.  
  9. def add_word(self, word):
  10. self.words.append(word)
  11.  
  12. def end(self):
  13. to_return = []
  14. words = deepcopy(self.words)
  15.  
  16. while words:
  17. added = []
  18. added_string = words[0]
  19. words.pop(0)
  20. for word in words:
  21.  
  22. if len(added_string) + len(word) + 1 <= self.n:
  23. added_string += " " + word
  24. added.append(word)
  25. if len(words) == 1:
  26. to_return.append(added_string)
  27. for added_word in added:
  28. words.remove(added_word)
  29. break
  30. else:
  31. to_return.append(added_string)
  32. for added_word in added:
  33. words.remove(added_word)
  34. break
  35.  
  36. for line in to_return:
  37. print(line)
  38.  
  39.  
  40. class RightParagraph(LeftParagraph):
  41. def __init__(self, n):
  42. super().__init__(n)
  43.  
  44. def end(self):
  45. to_return = []
  46. words = deepcopy(self.words)
  47.  
  48. while words:
  49. added = []
  50. added_string = words[0]
  51. words.pop(0)
  52. for word in words:
  53.  
  54. if len(added_string) + len(word) + 1 <= self.n:
  55. added_string += " " + word
  56. added.append(word)
  57. if len(words) == 1:
  58. to_return.append((" " * (self.n - len(added_string))) + added_string)
  59. for added_word in added:
  60. words.remove(added_word)
  61. break
  62. else:
  63. to_return.append((" " * (self.n - len(added_string))) + added_string)
  64. for added_word in added:
  65. words.remove(added_word)
  66. break
  67.  
  68. for line in to_return:
  69. print(line)
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement