import re
import logging
Configure logging
logging.basicConfig(level=logging.DEBUG, format=‘%(asctime)s - %(levelname)s - %(message)s’)
class ArticleManager:
def init(self, article_text, options=None):
if not isinstance(article_text, str):
raise ValueError(“article_text must be a string.”)
if options is None:
options = {}
self.article_text = article_text
self.pages = []
self.words_per_line = options.get('words_per_line', 12)
self.lines_per_page = options.get('lines_per_page', 20)
self.payment_structure = options.get('payment_structure', {
0: 0, # Fewer than 1 page
1: 30, # 1-2 pages
2: 30,
3: 60, # 3-4 pages
4: 60,
'default': 100 # More than 4 pages
})
def split_into_pages(self):
# Split the article text into words
words = re.split(r'\s+', self.article_text.strip())
total_words = len(words)
total_pages = -(-total_words // (self.words_per_line * self.lines_per_page)) # equivalent to math.ceil
logging.debug(f"Total words: {total_words}, Total pages: {total_pages}")
for i in range(total_pages):
page_words = words[i * self.words_per_line * self.lines_per_page:(i + 1) * self.words_per_line * self.lines_per_page]
page_lines = []
# Split the page into lines
for j in range(0, len(page_words), self.words_per_line):
page_lines.append(' '.join(page_words[j:j + self.words_per_line]))
# Join the lines into a single string
page = '\n'.join(page_lines)
# Add the page to the list of pages
self.pages.append(page)
logging.debug(f"Pages created: {len(self.pages)}")
def calculate_payment(self):
total_pages = len(self.pages)
# Payment calculation based on the number of pages
if total_pages < 1 or self.lines_per_page < 20:
payment = self.payment_structure[0]
elif total_pages <= 2:
payment = self.payment_structure[1]
elif total_pages <= 4:
payment = self.payment_structure[3]
else:
payment = self.payment_structure['default']
logging.debug(f"Calculated payment for {total_pages} pages: ${payment}")
return payment
def display_pages(self):
payment = self.calculate_payment()
print(f"Paid Pages: {len(self.pages)}") # Display the number of pages
print(f"Payment Due: ${payment}") # Display the payment due
for index, page in enumerate(self.pages):
print(f"\nPage {index + 1}:\n{page}\n")
def process_article(self):
self.split_into_pages()
self.display_pages()
Example usage
if name == “main”:
# Set the article text to an empty string to ensure 0 pages and 0 payment
article_text = “Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.” # or use a string with fewer than 240 words
options = {
‘words_per_line’: 12,
‘lines_per_page’: 20,
‘payment_structure’: {
0: 0, # Fewer than 1 page
1: 30, # 1-2 pages
2: 30,
3: 60, # 3-4 pages
4: 60,
‘default’: 100 # More than 4 pages
}
}
try:
article_manager = ArticleManager(article_text)
article_manager.process_article()
except ValueError as e:
logging.error(e)
The value of the output should be
Paid Pages: 0
Payment Due: $0
but it is showing as
Paid Pages: 1
Payment Due: $30
PLease help me with this