כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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
מחלקת כלבA Dog class
כתבו מחלקה Dog עם name ו-sound (ברירת מחדל "Woof"), ומתודה speak() שמחזירה "<name> says <sound>". צרו את Rex והדפיסו את מה שהוא אומר.
Write a Dog class with name and sound (default "Woof"), and a speak() method returning "<name> says <sound>". Create Rex and print what he says.
איך הפלט אמור להיראות?What should the output look like?
כתבו Account עם owner ו-balance שמתחיל ב-0. deposit(x) מוסיפה, withdraw(x) מורידה רק אם יש מספיק ומחזירה True, אחרת מחזירה False ולא משנה דבר.
Write Account with owner and balance starting at 0. deposit(x) adds, withdraw(x) subtracts only if there's enough and returns True, otherwise returns False and changes nothing.
איך הפלט אמור להיראות?What should the output look like?
True False 70
main.py
רמזHint 1:ב-withdraw: if x > self.balance: return False.In withdraw: if x > self.balance: return False.
כתבו ShoppingList עם add(item, qty=1) (אם הפריט קיים, מגדילים כמות), remove(item), ו-total_items() שמחזירה את סך הכמויות. שמרו את הפריטים במילון self.items.
Write ShoppingList with add(item, qty=1) (if it exists, increase the quantity), remove(item), and total_items() returning the sum of quantities. Keep items in a dict self.items.
איך הפלט אמור להיראות?What should the output look like?
מדף, צעד 15: Book ו-LibraryShelf, step 15: Book and Library
פרויקט מלווהCompanion project
הפכו את המדף למחלקות. Book(title, author, pages) עם read שמתחיל ב-0 ומתודה read_pages(n) (לא עוברים את pages). Library עם add(book), find(title) (בלי רגישות לאותיות גדולות, או None), ו-unread() שמחזירה רשימת כותרות של ספרים שלא התחילו.
Turn the shelf into classes. Book(title, author, pages) with read starting at 0 and a read_pages(n) method (never past pages). Library with add(book), find(title) (case-insensitive, or None), and unread() returning titles of books not started.
איך הפלט אמור להיראות?What should the output look like?
['Emma']
shelf.py
רמזHint 1:self.read = min(self.read + n, self.pages)self.read = min(self.read + n, self.pages)
רמזHint 2:[b.title for b in self.books if b.read == 0][b.title for b in self.books if b.read == 0]
כתבו Stack עם push(x), pop() (מחזירה את האחרון ומוציאה; על מחסנית ריקה מחזירה None), peek() (מחזירה בלי להוציא), is_empty() ו-size(). השתמשו בה כדי לבדוק אם הסוגריים במחרוזת מאוזנים: balanced("(a[b]{c})") → True.
Write Stack with push(x), pop() (returns and removes the last; on an empty stack returns None), peek() (returns without removing), is_empty() and size(). Use it to check whether brackets in a string are balanced: balanced("(a[b]{c})") → True.
איך הפלט אמור להיראות?What should the output look like?
True False False
main.py
רמזHint 1:פותחים → push. סוגרים → pop ובדיקה שהזוג מתאים.Opening → push. Closing → pop and check the pair matches.
רמזHint 2:בסוף המחסנית חייבת להיות ריקה.At the end the stack must be empty.
כתבו Task(name) עם log(minutes) שמוסיפה זמן, ו-Tracker שמחזיקה משימות: start(name) יוצרת משימה אם אין (ומחזירה אותה), total() מחזירה את כל הדקות, ו-top() מחזירה את שם המשימה עם הכי הרבה דקות.
Write Task(name) with log(minutes) adding time, and Tracker holding tasks: start(name) creates the task if missing (and returns it), total() returns all minutes, top() returns the name of the task with the most minutes.
איך הפלט אמור להיראות?What should the output look like?
100 read
main.py
רמזHint 1:שמרו מילון name → Task.Keep a dict name → Task.