מילונים, טאפלים וסטיםDicts, tuples & sets
מפתח-ערך, טאפלים בלתי משתנים, סטים בלי כפילויות.
Key-value pairs, immutable tuples, sets without duplicates.
מחירוןPrice list
תזכורת: מה ביקשוReminder: the task
נתון מילון מחירים. קלטו שם מוצר והדפיסו את המחיר שלו, או not found אם אין כזה.
Given a price dict, read a product name and print its price, or not found if missing.
הפתרון המלא והפלט שלוFull solution and its output
prices = {"milk": 6.9, "bread": 8, "eggs": 12.5}
item = input().strip()
print(prices.get(item, "not found"))eggs 12.5
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
ספירת מיליםWord count
תזכורת: מה ביקשוReminder: the task
קלטו משפט (באותיות קטנות) והדפיסו כמה פעמים מופיעה כל מילה, בסדר ההופעה הראשונה, בפורמט word: n.
Read a sentence (lowercase) and print how many times each word appears, in first-appearance order, as word: n.
הפתרון המלא והפלט שלוFull solution and its output
counts = {}
for w in input().split():
counts[w] = counts.get(w, 0) + 1
for w, n in counts.items():
print(f"{w}: {n}")the cat the dog and the cat the: 3 cat: 2 dog: 1 and: 1
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מרחק בין נקודותDistance between points
תזכורת: מה ביקשוReminder: the task
נתונים שני טאפלים 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).
הפתרון המלא והפלט שלוFull solution and its output
a = (1, 2)
b = (4, 6)
x1, y1 = a
x2, y2 = b
print(((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5)5.0
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
חברים משותפיםBooks in common
תזכורת: מה ביקשוReminder: the task
נתונים שני סטים של ספרים שקראו נועה ודן. הדפיסו (ממוינים) את הספרים ששניהם קראו, ואחר כך את אלה שרק נועה קראה.
Given two sets of books Noa and Dan read, print (sorted) the books both read, then those only Noa read.
הפתרון המלא והפלט שלוFull solution and its output
noa = {"Dune", "Emma", "Matilda", "Ulysses"}
dan = {"Dune", "Ulysses", "Beloved"}
print(sorted(noa & dan))
print(sorted(noa - dan))['Dune', 'Ulysses'] ['Emma', 'Matilda']
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מדף, צעד 8: כל ספר הוא מילוןShelf, step 8: each book is a dict
תזכורת: מה ביקשוReminder: the task
שדרגו את המדף: כל ספר נשמר כמילון עם 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.
הפתרון המלא והפלט שלוFull solution and its output
shelf = []
for _ in range(2):
title = input()
author = input()
genres = {g.strip() for g in input().split(",")}
shelf.append({"title": title, "author": author, "genres": genres})
all_genres = set()
for b in shelf:
all_genres |= b["genres"]
print(sorted(all_genres))
print(len(shelf))Dune Frank Herbert sci-fi, classic Matilda Roald Dahl kids, classic ['classic', 'kids', 'sci-fi'] 2
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
אנגרמותAnagrams
תזכורת: מה ביקשוReminder: the task
נתונה רשימת מילים. קבצו אנגרמות (מילים עם אותן אותיות) והדפיסו כל קבוצה עם יותר ממילה אחת, ממוינת, שורה לכל קבוצה, בסדר ההופעה.
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.
הפתרון המלא והפלט שלוFull solution and its output
words = ["listen", "google", "silent", "enlist", "banana", "elgoog"]
groups = {}
for w in words:
key = "".join(sorted(w))
groups.setdefault(key, []).append(w)
for g in groups.values():
if len(g) > 1:
print(sorted(g))['enlist', 'listen', 'silent'] ['elgoog', 'google']
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
היפוך מילוןInvert a dict
תזכורת: מה ביקשוReminder: the task
נתון 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.
הפתרון המלא והפלט שלוFull solution and its output
book_genre = {"Dune": "sci-fi", "Emma": "classic", "Neuromancer": "sci-fi"}
by_genre = {}
for book, genre in book_genre.items():
by_genre.setdefault(genre, []).append(book)
for genre in by_genre:
by_genre[genre].sort()
print(by_genre){'sci-fi': ['Dune', 'Neuromancer'], 'classic': ['Emma']}יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.