Advertisement
WupEly

Untitled

Mar 21st, 2023
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 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. if not len(words):
  21. to_return.append(added_string)
  22. for word in words:
  23.  
  24. if len(added_string) + len(word) + 1 <= self.n:
  25. added_string += " " + word
  26. added.append(word)
  27. if len(words) == 1:
  28. to_return.append(added_string)
  29. for added_word in added:
  30. words.remove(added_word)
  31. break
  32. else:
  33. to_return.append(added_string)
  34. for added_word in added:
  35. words.remove(added_word)
  36. break
  37.  
  38. for line in to_return:
  39. print(line)
  40.  
  41.  
  42. class RightParagraph(LeftParagraph):
  43. def __init__(self, n):
  44. super().__init__(n)
  45.  
  46. def end(self):
  47. to_return = []
  48. words = deepcopy(self.words)
  49.  
  50. while words:
  51. added = []
  52. added_string = words[0]
  53. words.pop(0)
  54. if not len(words):
  55. to_return.append(added_string)
  56. for word in words:
  57.  
  58. if len(added_string) + len(word) + 1 <= self.n:
  59. added_string += " " + word
  60. added.append(word)
  61. if len(words) == 1:
  62. to_return.append((" " * (self.n - len(added_string))) + added_string)
  63. for added_word in added:
  64. words.remove(added_word)
  65. break
  66. else:
  67. to_return.append((" " * (self.n - len(added_string))) + added_string)
  68. for added_word in added:
  69. words.remove(added_word)
  70. break
  71.  
  72. for line in to_return:
  73. print(line)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement