feat: complete task 1
This commit is contained in:
28
task1.py
28
task1.py
@@ -1,28 +1,44 @@
|
||||
from dataclasses import dataclass
|
||||
import json
|
||||
from typing import Iterable
|
||||
|
||||
|
||||
@dataclass
|
||||
class Poem:
|
||||
name: str
|
||||
author: str | list[str]
|
||||
authors: str | list[str]
|
||||
body: str
|
||||
|
||||
|
||||
def load_data() -> list[Poem]:
|
||||
raise NotImplementedError
|
||||
with open("task1_poems.json", "r") as file:
|
||||
return [
|
||||
Poem(**obj)
|
||||
for obj in json.load(file)
|
||||
]
|
||||
|
||||
def ask_author_to_search() -> str:
|
||||
return input("Имя автора для поиска: ")
|
||||
|
||||
def search_by_author(poems: list[Poem], author: str) -> list[Poem]:
|
||||
raise NotImplementedError
|
||||
return [
|
||||
poem
|
||||
for poem in poems
|
||||
if isinstance(poem.authors, str) and poem.authors == author\
|
||||
or author in poem.authors
|
||||
]
|
||||
|
||||
def get_sorted_poems(poems: list[Poem]) -> list[Poem]:
|
||||
raise NotImplementedError
|
||||
return sorted(poems, key=lambda poem: len(poem.body))
|
||||
|
||||
def print_poems(poems: Iterable[Poem]) -> None:
|
||||
raise NotImplementedError
|
||||
for poem in poems:
|
||||
authors: str = poem.authors if isinstance(poem.authors, str) \
|
||||
else ", ".join(poem.authors)
|
||||
print(f"{authors} - {poem.name} ({len(poem.body)} символов)")
|
||||
|
||||
poems = load_data()
|
||||
author: str = input()
|
||||
author: str = ask_author_to_search()
|
||||
poems = search_by_author(poems, author)
|
||||
poems = get_sorted_poems(poems)
|
||||
print_poems(poems)
|
||||
|
||||
Reference in New Issue
Block a user