Scope וערכי החזרהScope & return values
return מול print, משתנים מקומיים וגלובליים, ופונקציות טהורות.
return vs print, local and global variables, and pure functions.
מ-print ל-returnFrom print to return
תזכורת: מה ביקשוReminder: the task
הפונקציה square מדפיסה במקום להחזיר, ולכן החישוב בשורה האחרונה נופל. תקנו אותה כך שתחזיר, והקוד ידפיס 26.
square prints instead of returning, so the last line crashes. Fix it to return, and the code prints 26.
הפתרון המלא והפלט שלוFull solution and its output
def square(x):
return x * x
print(square(5) + 1)26
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מינימום ומקסימוםMin and max
תזכורת: מה ביקשוReminder: the task
כתבו min_max(nums) שמחזירה טאפל (הקטן, הגדול) בלי להשתמש ב-min וב-max המובנות. אם הרשימה ריקה החזירו (None, None).
Write min_max(nums) returning a tuple (smallest, largest) without using the built-in min and max. For an empty list return (None, None).
הפתרון המלא והפלט שלוFull solution and its output
def min_max(nums):
if not nums:
return None, None
lo = hi = nums[0]
for n in nums:
if n < lo:
lo = n
if n > hi:
hi = n
return lo, hi
print(min_max([412, 96, 1036, 280]))(96, 1036)
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
בלי גלובלייםNo globals
תזכורת: מה ביקשוReminder: the task
הקוד משתמש במשתנה גלובלי ונופל עם UnboundLocalError. שכתבו את add_xp כך שתקבל את ה-XP הנוכחי ואת התוספת ותחזיר את החדש. הקוד הראשי צריך להדפיס 150.
The code uses a global and crashes with UnboundLocalError. Rewrite add_xp to take the current XP and the bonus and return the new value. The main code should print 150.
הפתרון המלא והפלט שלוFull solution and its output
def add_xp(xp, bonus):
return xp + bonus
xp = 50
xp = add_xp(xp, 100)
print(xp)150
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
עצרת ברקורסיהRecursive factorial
תזכורת: מה ביקשוReminder: the task
כתבו factorial(n) רקורסיבית: factorial(0) היא 1, ו-factorial(n) היא n * factorial(n - 1). הדפיסו את factorial(6).
Write a recursive factorial(n): factorial(0) is 1, and factorial(n) is n * factorial(n - 1). Print factorial(6).
הפתרון המלא והפלט שלוFull solution and its output
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
print(factorial(6))720
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מדף, צעד 10: חיפוש שמחזיר תוצאותShelf, step 10: a search that returns results
תזכורת: מה ביקשוReminder: the task
כתבו search(shelf, text) טהורה שמחזירה רשימה של מילוני ספרים שהכותרת או המחבר שלהם מכילים את text (בלי רגישות לאותיות גדולות). הדפיסו את הכותרות של התוצאות עבור "da".
Write a pure search(shelf, text) returning a list of book dicts whose title or author contains text (case-insensitive). Print the titles of the results for "da".
הפתרון המלא והפלט שלוFull solution and its output
def search(shelf, text):
t = text.lower()
return [b for b in shelf if t in b["title"].lower() or t in b["author"].lower()]
shelf = [
{"title": "Dune", "author": "Frank Herbert"},
{"title": "Matilda", "author": "Roald Dahl"},
{"title": "Emma", "author": "Jane Austen"},
]
for b in search(shelf, "da"):
print(b["title"])Matilda
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
פיבונאצ'יFibonacci
תזכורת: מה ביקשוReminder: the task
כתבו fib(n) שמחזירה את מספר פיבונאצ'י ה-n (fib(0)=0, fib(1)=1). כתבו גרסה עם לולאה (לא רקורסיה, כדי שתהיה מהירה גם ל-n=80) והדפיסו את fib(80).
Write fib(n) returning the nth Fibonacci number (fib(0)=0, fib(1)=1). Use a loop (not recursion, so it's fast even for n=80) and print fib(80).
הפתרון המלא והפלט שלוFull solution and its output
def fib(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
print(fib(80))23416728348467685
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
סכום ספרות ברקורסיהRecursive digit sum
תזכורת: מה ביקשוReminder: the task
כתבו digit_sum(n) רקורסיבית שמחזירה את סכום הספרות של מספר שלם חיובי, בלי להמיר למחרוזת. רמז: n % 10 היא הספרה האחרונה ו-n // 10 הוא השאר.
Write a recursive digit_sum(n) returning the digit sum of a positive integer, without converting to a string. Hint: n % 10 is the last digit and n // 10 is the rest.
הפתרון המלא והפלט שלוFull solution and its output
def digit_sum(n):
if n < 10:
return n
return n % 10 + digit_sum(n // 10)
print(digit_sum(2026))10
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.