From be986cbc172bf92858239a0f6abbeed0b9b94b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B0=D1=81=D1=8B=D0=BF=D0=BA=D0=B8=D0=BD=20=D0=90?= =?UTF-8?q?=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= Date: Fri, 27 Mar 2026 15:26:56 +0300 Subject: [PATCH] fix: add check line and rating, find_longest_review returns first longest rewiew --- reviews.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/reviews.py b/reviews.py index 47d358a..a41e76f 100644 --- a/reviews.py +++ b/reviews.py @@ -10,11 +10,24 @@ def load_reviews(filepath: str) -> list: with open(filepath, "r", encoding="utf-8") as file: for line in file: line = line.strip() + if not line: + continue parts = line.split("|") + if len(parts) != 3: + print("Пропущена строка с неверным форматом:", line) + continue username, rating, text = parts + try: + rating_int = int(rating.strip()) + except ValueError: + print("Неверный рейтинг:", line) + continue + if not (1 <= rating_int <= 5): + print("Рейтинг вне диапазона [1–5]:", line) + continue reviews.append({ "username": username.strip(), - "rating": int(rating), + "rating": rating_int, "text": text.strip() }) return reviews @@ -47,8 +60,14 @@ def count_words_in_review(review: dict) -> int: def find_longest_review(reviews: list) -> dict: if not reviews: return {} - - return max(reviews, key=count_words_in_review) + longest = reviews[0] + 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: