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

PythonPython
10
חלקPart 2: פונקציות וקוד אמיתיFunctions & real code כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

Scope וערכי החזרהScope & return values

return מול print, משתנים מקומיים וגלובליים, ופונקציות טהורות.

return vs print, local and global variables, and pure functions.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. מה ידפיס הקוד?What does this print?
python
def f():
    print(5)

print(f())

2. מה יקרה כשנריץ print(y) אחרי הקריאה?What happens on print(y) after the call?
python
def g(x):
    y = x * 2
    return y

g(5)
print(y)

3. איזו פונקציה טהורה?Which function is pure?

תרגיליםExercises

תרגילExercise 1

מ-print ל-returnFrom print to return

הפונקציה square מדפיסה במקום להחזיר, ולכן החישוב בשורה האחרונה נופל. תקנו אותה כך שתחזיר, והקוד ידפיס 26.

square prints instead of returning, so the last line crashes. Fix it to return, and the code prints 26.

איך הפלט אמור להיראות?What should the output look like?
26
main.py
רמזHint 1: מחליפים את print(...) ב-return ....Replace print(...) with return ....
לפתרוןSolution
תרגילExercise 2

מינימום ומקסימוםMin and max

כתבו 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).

איך הפלט אמור להיראות?What should the output look like?
(96, 1036)
main.py
רמזHint 1: return מוקדם לרשימה ריקה.Early return for an empty list.
רמזHint 2: מתחילים מ-lo = hi = nums[0] ועוברים בלולאה.Start with lo = hi = nums[0] and loop.
לפתרוןSolution
תרגילExercise 3

בלי גלובלייםNo globals

הקוד משתמש במשתנה גלובלי ונופל עם 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.

איך הפלט אמור להיראות?What should the output look like?
150
main.py
רמזHint 1: def add_xp(xp, bonus): return xp + bonusdef add_xp(xp, bonus): return xp + bonus
רמזHint 2: xp = add_xp(xp, 100)xp = add_xp(xp, 100)
לפתרוןSolution
תרגילExercise 4

עצרת ברקורסיהRecursive factorial

כתבו 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).

איך הפלט אמור להיראות?What should the output look like?
720
main.py
רמזHint 1: תנאי עצירה: if n == 0: return 1.Stop condition: if n == 0: return 1.
לפתרוןSolution
תרגילExercise 5

מדף, צעד 10: חיפוש שמחזיר תוצאותShelf, step 10: a search that returns results

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

כתבו 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".

איך הפלט אמור להיראות?What should the output look like?
Matilda
shelf.py
רמזHint 1: t = text.lower()t = text.lower()
רמזHint 2: comprehension עם or.A comprehension with or.
לפתרוןSolution

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

אתגרChallenge 1

פיבונאצ'יFibonacci

כתבו 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).

איך הפלט אמור להיראות?What should the output look like?
23416728348467685
main.py
רמזHint 1: שני משתנים: a, b = 0, 1.Two variables: a, b = 0, 1.
רמזHint 2: בכל צעד: a, b = b, a + b.Each step: a, b = b, a + b.
לפתרוןSolution
אתגרChallenge 2

סכום ספרות ברקורסיהRecursive digit sum

כתבו 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.

איך הפלט אמור להיראות?What should the output look like?
10
main.py
רמזHint 1: תנאי עצירה: if n < 10: return n.Stop: if n < 10: return n.
רמזHint 2: return n % 10 + digit_sum(n // 10)return n % 10 + digit_sum(n // 10)
לפתרוןSolution

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