fix: add check line and rating, find_longest_review returns first longest rewiew

This commit is contained in:
Александр Засыпкин 2026-03-27 15:26:56 +03:00
parent bb5b9ac705
commit be986cbc17
Signed by: stud203800
GPG Key ID: 53539BEF3FC5FD39

View File

@ -10,11 +10,24 @@ def load_reviews(filepath: str) -> list:
with open(filepath, "r", encoding="utf-8") as file: with open(filepath, "r", encoding="utf-8") as file:
for line in file: for line in file:
line = line.strip() line = line.strip()
if not line:
continue
parts = line.split("|") parts = line.split("|")
if len(parts) != 3:
print("Пропущена строка с неверным форматом:", line)
continue
username, rating, text = parts username, rating, text = parts
try:
rating_int = int(rating.strip())
except ValueError:
print("Неверный рейтинг:", line)
continue
if not (1 <= rating_int <= 5):
print("Рейтинг вне диапазона [15]:", line)
continue
reviews.append({ reviews.append({
"username": username.strip(), "username": username.strip(),
"rating": int(rating), "rating": rating_int,
"text": text.strip() "text": text.strip()
}) })
return reviews return reviews
@ -47,8 +60,14 @@ def count_words_in_review(review: dict) -> int:
def find_longest_review(reviews: list) -> dict: def find_longest_review(reviews: list) -> dict:
if not reviews: if not reviews:
return {} return {}
longest = reviews[0]
return max(reviews, key=count_words_in_review) max_words = count_words_in_review(longest)
for rev in reviews[1:]:
words = count_words_in_review(rev)
if words > max_words:
max_words = words
longest = rev
return longest
def build_word_frequency(reviews: list) -> dict: def build_word_frequency(reviews: list) -> dict: