Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from copy import deepcopy
- class LeftParagraph:
- def __init__(self, n):
- self.n = n
- self.words = []
- def add_word(self, word):
- self.words.append(word)
- def end(self):
- to_return = []
- words = deepcopy(self.words)
- while words:
- added = []
- added_string = words[0]
- words.pop(0)
- if not len(words):
- to_return.append(added_string)
- for word in words:
- if len(added_string) + len(word) + 1 <= self.n:
- added_string += " " + word
- added.append(word)
- if len(words) == 1:
- to_return.append(added_string)
- for added_word in added:
- words.remove(added_word)
- break
- else:
- to_return.append(added_string)
- for added_word in added:
- words.remove(added_word)
- break
- for line in to_return:
- print(line)
- self.words = []
- class RightParagraph(LeftParagraph):
- def __init__(self, n):
- super().__init__(n)
- def end(self):
- to_return = []
- words = deepcopy(self.words)
- while words:
- added = []
- added_string = words[0]
- words.pop(0)
- if not len(words):
- to_return.append((" " * (self.n - len(added_string))) + added_string)
- for word in words:
- if len(added_string) + len(word) + 1 <= self.n:
- added_string += " " + word
- added.append(word)
- if len(words) == 1:
- to_return.append((" " * (self.n - len(added_string))) + added_string)
- for added_word in added:
- words.remove(added_word)
- break
- else:
- to_return.append((" " * (self.n - len(added_string))) + added_string)
- for added_word in added:
- words.remove(added_word)
- break
- for line in to_return:
- print(line)
- self.words = []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement