Advertisement
WupEly

Untitled

Mar 21st, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 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. self.words = []
  41.  
  42.  
  43. class RightParagraph(LeftParagraph):
  44. def __init__(self, n):
  45. super().__init__(n)
  46.  
  47. def end(self):
  48. to_return = []
  49. words = deepcopy(self.words)
  50.  
  51. while words:
  52. added = []
  53. added_string = words[0]
  54. words.pop(0)
  55. if not len(words):
  56. to_return.append((" " * (self.n - len(added_string))) + added_string)
  57. for word in words:
  58.  
  59. if len(added_string) + len(word) + 1 <= self.n:
  60. added_string += " " + word
  61. added.append(word)
  62. if len(words) == 1:
  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. else:
  68. to_return.append((" " * (self.n - len(added_string))) + added_string)
  69. for added_word in added:
  70. words.remove(added_word)
  71. break
  72.  
  73. for line in to_return:
  74. print(line)
  75. self.words = []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement