כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-VS Code. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in VS Code. Progress is saved.
חימום: 3 שאלותWarm-up: 3 questions
תרגיליםExercises
תרגילExercise 1
להוסיף רמזיםAdd hints
הוסיפו רמזי טיפוס לכל הפרמטרים ולערכי ההחזרה: average(nums) מקבלת רשימת float ומחזירה float, ו-greet(name, times) מקבלת str ו-int ומחזירה str.
Add type hints to every parameter and return value: average(nums) takes a list of float and returns float, and greet(name, times) takes str and int and returns str.
איך הפלט אמור להיראות?What should the output look like?
כתבו dataclass Note עם text: str ו-tags: list[str] שמתחילה ריקה (בצורה הנכונה!), ומתודה tag(t) שמוסיפה תג רק אם הוא לא קיים כבר.
Write a Note dataclass with text: str and tags: list[str] starting empty (the right way!), and a tag(t) method that adds a tag only if it isn't there already.
איך הפלט אמור להיראות?What should the output look like?
כתבו dataclass Grade עם order=True ושדות score: int, name: str (בסדר הזה, כדי שהמיון יהיה לפי ציון). הדפיסו את שמות הסטודנטים מהציון הגבוה לנמוך.
Write a Grade dataclass with order=True and fields score: int, name: str (in that order, so sorting is by score). Print the student names from highest to lowest score.
איך הפלט אמור להיראות?What should the output look like?
מדף, צעד 18: Book כ-dataclassShelf, step 18: Book as a dataclass
פרויקט מלווהCompanion project
כתבו את Book מחדש כ-dataclass עם רמזים: title: str, author: str, pages: int, read: int = 0, tags: list[str] ריקה. הוסיפו progress() -> int (אחוז מעוגל). כתבו find(shelf: list[Book], title: str) -> Book | None. הדפיסו את asdict של הספר שנמצא.
Rewrite Book as a dataclass with hints: title: str, author: str, pages: int, read: int = 0, tags: list[str] empty. Add progress() -> int (rounded percent). Write find(shelf: list[Book], title: str) -> Book | None. Print asdict of the found book.
איך הפלט אמור להיראות?What should the output look like?
כתבו dataclass Item(name: str, price: float) עם frozen=True, ו-dataclass Cart עם items: dict[str, int] (שם → כמות), catalog: dict[str, Item], ומתודות add(name, qty=1) (ValueError אם השם לא בקטלוג), remove(name) ו-total() -> float מעוגל ל-2.
Write a frozen dataclass Item(name: str, price: float), and a Cart dataclass with items: dict[str, int] (name → qty), catalog: dict[str, Item], and methods add(name, qty=1) (ValueError if the name isn't in the catalog), remove(name) and total() -> float rounded to 2.
איך הפלט אמור להיראות?What should the output look like?
כתבו from_dict(cls, data) כללית: יוצרת אובייקט של dataclass כלשהי ממילון, תוך התעלמות ממפתחות שאינם שדות, והמרה של כל ערך לטיפוס של השדה (למשל "412" → 412). השתמשו ב-dataclasses.fields(cls) וב-f.type.
Write a generic from_dict(cls, data): creates an object of any dataclass from a dict, ignoring keys that aren't fields and converting each value to the field's type (e.g. "412" → 412). Use dataclasses.fields(cls) and f.type.
איך הפלט אמור להיראות?What should the output look like?
Book(title='Dune', pages=412, rating=4.5)
main.py
רמזHint 1:{f.name: f.type(data[f.name]) for f in fields(cls) if f.name in data}{f.name: f.type(data[f.name]) for f in fields(cls) if f.name in data}