רשימותLists
יצירה, הוספה, מחיקה, מיון, ומה זה mutable.
Creating, adding, removing, sorting, and what mutable means.
ממוצע ציוניםAverage score
תזכורת: מה ביקשוReminder: the task
נתונה הרשימה scores = [88, 92, 79, 95, 70]. הדפיסו את הממוצע מעוגל לספרה אחת, ואת הציון הגבוה והנמוך.
Given scores = [88, 92, 79, 95, 70], print the average rounded to one decimal, then the highest and lowest score.
הפתרון המלא והפלט שלוFull solution and its output
scores = [88, 92, 79, 95, 70]
print(round(sum(scores) / len(scores), 1))
print(max(scores), min(scores))84.8 95 70
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
רשימת קניותShopping list
תזכורת: מה ביקשוReminder: the task
קלטו פריטים עד שמקלידים שורה ריקה. בסוף הדפיסו כמה פריטים יש, ואת הרשימה ממוינת, פריט בשורה.
Read items until an empty line is typed. Then print how many items there are, and the list sorted, one per line.
הפתרון המלא והפלט שלוFull solution and its output
items = []
while True:
item = input()
if not item:
break
items.append(item)
print(len(items))
for it in sorted(items):
print(it)milk bread eggs 3 bread eggs milk
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
ללא כפילויותNo duplicates
תזכורת: מה ביקשוReminder: the task
נתונה tags = ["sci-fi", "classic", "sci-fi", "kids", "classic"]. בנו רשימה חדשה בלי כפילויות, בסדר ההופעה הראשונה, והדפיסו אותה.
Given tags = ["sci-fi", "classic", "sci-fi", "kids", "classic"], build a new list without duplicates, in first-appearance order, and print it.
הפתרון המלא והפלט שלוFull solution and its output
tags = ["sci-fi", "classic", "sci-fi", "kids", "classic"]
unique = []
for t in tags:
if t not in unique:
unique.append(t)
print(unique)['sci-fi', 'classic', 'kids']
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
ספרים ארוכיםLong books
תזכורת: מה ביקשוReminder: the task
נתונות שתי רשימות מקבילות: titles ו-pages. בעזרת zip הדפיסו רק ספרים עם יותר מ-400 עמודים, בפורמט Dune (412).
Given two parallel lists, titles and pages, use zip to print only books over 400 pages, as Dune (412).
הפתרון המלא והפלט שלוFull solution and its output
titles = ["Dune", "Matilda", "Emma", "Ulysses"]
pages = [412, 240, 474, 730]
for t, p in zip(titles, pages):
if p > 400:
print(f"{t} ({p})")Dune (412) Emma (474) Ulysses (730)
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מדף, צעד 7: רשימת ספרים אמיתיתShelf, step 7: a real list of books
תזכורת: מה ביקשוReminder: the task
שדרגו את התפריט: 1 מוסיף שם ספר לרשימה, 2 מדפיס את כל הספרים ממוספרים (1. Dune), 3 מוחק ספר לפי שם (אם הוא קיים, אחרת not found), q יוצא.
Upgrade the menu: 1 adds a title to a list, 2 prints all books numbered (1. Dune), 3 removes a book by title (if it exists, otherwise not found), q quits.
הפתרון המלא והפלט שלוFull solution and its output
shelf = []
while True:
choice = input("> ")
if choice == "1":
shelf.append(input("title: "))
elif choice == "2":
for i, t in enumerate(shelf, start=1):
print(f"{i}. {t}")
elif choice == "3":
t = input("remove: ")
if t in shelf:
shelf.remove(t)
else:
print("not found")
elif choice == "q":
break> 1 title: Dune > 1 title: Emma > 2 1. Dune 2. Emma > 3 remove: Ulysses not found > 3 remove: Dune > q
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
סיבוב רשימהRotate a list
תזכורת: מה ביקשוReminder: the task
קלטו מספר k. סובבו את nums = [1, 2, 3, 4, 5] ימינה k מקומות (k יכול להיות גדול מאורך הרשימה) והדפיסו. ל-k=2: [4, 5, 1, 2, 3].
Read k. Rotate nums = [1, 2, 3, 4, 5] right by k places (k may exceed the length) and print it. For k=2: [4, 5, 1, 2, 3].
הפתרון המלא והפלט שלוFull solution and its output
nums = [1, 2, 3, 4, 5]
k = int(input()) % len(nums)
print(nums[-k:] + nums[:-k] if k else nums)7 [4, 5, 1, 2, 3]
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
המילה הארוכה ביותרLongest word
תזכורת: מה ביקשוReminder: the task
קלטו משפט. הדפיסו את המילה הארוכה ביותר (אם יש כמה באותו אורך, הראשונה) ואת אורכה.
Read a sentence. Print its longest word (the first if several tie) and its length.
הפתרון המלא והפלט שלוFull solution and its output
words = input().split()
longest = max(words, key=len)
print(longest, len(longest))a truly wonderful planet indeed wonderful 9
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.