נבנה על ידיBuilt by Ariel Yohimovich

PythonPython
8
חלקPart 1: יסודותFoundations כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

מילונים, טאפלים וסטיםDicts, tuples & sets

מפתח-ערך, טאפלים בלתי משתנים, סטים בלי כפילויות.

Key-value pairs, immutable tuples, sets without duplicates.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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

1. מה ידפיס d.get("x", 0) אם אין מפתח x?What does d.get("x", 0) give if there's no key x?

2. מה len({1, 2, 2, 3, 3, 3})?What is len({1, 2, 2, 3, 3, 3})?

3. איזה מבנה הכי מתאים לשמירת "שם תלמיד → ציון"?Which structure best stores "student name → score"?

תרגיליםExercises

תרגילExercise 1

מחירוןPrice list

נתון מילון מחירים. קלטו שם מוצר והדפיסו את המחיר שלו, או not found אם אין כזה.

Given a price dict, read a product name and print its price, or not found if missing.

איך הפלט אמור להיראות?What should the output look like?
eggs
12.5
main.py
רמזHint 1: if item in prices:if item in prices:
רמזHint 2: או prices.get(item, "not found").Or prices.get(item, "not found").
לפתרוןSolution
תרגילExercise 2

ספירת מיליםWord count

קלטו משפט (באותיות קטנות) והדפיסו כמה פעמים מופיעה כל מילה, בסדר ההופעה הראשונה, בפורמט word: n.

Read a sentence (lowercase) and print how many times each word appears, in first-appearance order, as word: n.

איך הפלט אמור להיראות?What should the output look like?
the cat the dog and the cat
the: 3
cat: 2
dog: 1
and: 1
main.py
רמזHint 1: counts[w] = counts.get(w, 0) + 1counts[w] = counts.get(w, 0) + 1
רמזHint 2: מילון שומר סדר הכנסה.A dict keeps insertion order.
לפתרוןSolution
תרגילExercise 3

מרחק בין נקודותDistance between points

נתונים שני טאפלים a = (1, 2) ו-b = (4, 6). פרקו אותם למשתנים והדפיסו את המרחק ביניהם (פיתגורס).

Given two tuples a = (1, 2) and b = (4, 6), unpack them and print the distance between them (Pythagoras).

איך הפלט אמור להיראות?What should the output look like?
5.0
main.py
רמזHint 1: x1, y1 = ax1, y1 = a
רמזHint 2: ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
לפתרוןSolution
תרגילExercise 4

חברים משותפיםBooks in common

נתונים שני סטים של ספרים שקראו נועה ודן. הדפיסו (ממוינים) את הספרים ששניהם קראו, ואחר כך את אלה שרק נועה קראה.

Given two sets of books Noa and Dan read, print (sorted) the books both read, then those only Noa read.

איך הפלט אמור להיראות?What should the output look like?
['Dune', 'Ulysses']
['Emma', 'Matilda']
main.py
רמזHint 1: & למשותף.& for common.
רמזHint 2: - להפרש.- for difference.
לפתרוןSolution
תרגילExercise 5

מדף, צעד 8: כל ספר הוא מילוןShelf, step 8: each book is a dict

פרויקט מלווהCompanion project

שדרגו את המדף: כל ספר נשמר כמילון עם title, author ו-genres (סט). הוסיפו שני ספרים מהקלט (כותרת, מחבר, ז'אנרים מופרדים בפסיק), והדפיסו את כל הז'אנרים הקיימים ממוינים, ואחריהם כמה ספרים יש.

Upgrade the shelf: each book is a dict with title, author and genres (a set). Add two books from input (title, author, comma-separated genres), then print all genres on the shelf sorted, then how many books there are.

איך הפלט אמור להיראות?What should the output look like?
Dune
Frank Herbert
sci-fi, classic
Matilda
Roald Dahl
kids, classic
['classic', 'kids', 'sci-fi']
2
shelf.py
רמזHint 1: set(g.strip() for g in line.split(","))set(g.strip() for g in line.split(","))
רמזHint 2: איחוד של כל הסטים: all_genres |= b["genres"].Union all sets: all_genres |= b["genres"].
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

אנגרמותAnagrams

נתונה רשימת מילים. קבצו אנגרמות (מילים עם אותן אותיות) והדפיסו כל קבוצה עם יותר ממילה אחת, ממוינת, שורה לכל קבוצה, בסדר ההופעה.

Given a word list, group anagrams (same letters) and print each group with more than one word, sorted, one group per line, in order of appearance.

איך הפלט אמור להיראות?What should the output look like?
['enlist', 'listen', 'silent']
['elgoog', 'google']
main.py
רמזHint 1: מפתח: "".join(sorted(word)).Key: "".join(sorted(word)).
רמזHint 2: מילון: מפתח → רשימת מילים.Dict: key → list of words.
לפתרוןSolution
אתגרChallenge 2

היפוך מילוןInvert a dict

נתון book_genre = {"Dune": "sci-fi", "Emma": "classic", "Neuromancer": "sci-fi"}. בנו מילון הפוך: ז'אנר → רשימת ספרים (ממוינת), והדפיסו אותו.

Given book_genre = {"Dune": "sci-fi", "Emma": "classic", "Neuromancer": "sci-fi"}, build the inverse: genre → sorted list of books, and print it.

איך הפלט אמור להיראות?What should the output look like?
{'sci-fi': ['Dune', 'Neuromancer'], 'classic': ['Emma']}
main.py
רמזHint 1: עוברים עם .items().Loop with .items().
רמזHint 2: setdefault(genre, []).append(book)setdefault(genre, []).append(book)
לפתרוןSolution

נבנה על ידיBuilt by Ariel Yohimovich