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

PythonPython
32
חלקPart 6: Python בעידן ה-AI וקריירהPython in the AI era & career כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

עבודה נכונה עם עוזר AI לקודWorking well with an AI coding assistant

מתי לסמוך, איך לבדוק, איך לבקש, ואיך לא לאבד את ההבנה.

When to trust it, how to verify, how to ask, and how not to lose understanding.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. עוזר AI הציע פונקציה. מה הצעד הראשון?An AI assistant suggested a function. What's the first step?

2. מה זו hallucination?What is a hallucination?

3. מה לא מדביקים לצ'אט עם עוזר?What don't you paste into a chat with an assistant?

תרגיליםExercises

תרגילExercise 1

לתקן קוד של עוזרFixing an assistant's code

הפונקציה average_rating "נכתבה על ידי עוזר" ונכשלת על ספרים בלי דירוג ועל רשימה ריקה. תקנו: מתעלמים מספרים בלי rating, ומחזירים None כשאין דירוגים.

The average_rating function was "written by an assistant" and fails on books without a rating and on an empty list. Fix it: ignore books without rating, and return None when there are no ratings.

איך הפלט אמור להיראות?What should the output look like?
4.5 None
main.py
רמזHint 1: ratings = [b["rating"] for b in books if "rating" in b]ratings = [b["rating"] for b in books if "rating" in b]
רמזHint 2: if not ratings: return Noneif not ratings: return None
לפתרוןSolution
תרגילExercise 2

בונה בקשותA prompt builder

כתבו build_prompt(task, examples, rules) שמחזירה מחרוזת: שורה Task: ..., אחריה Examples: ושורה לכל דוגמה - <input> => <output> (עם repr), ואחריה Rules: ושורה לכל כלל - <rule>. בלי כללים: בלי שורת Rules: בכלל.

Write build_prompt(task, examples, rules) returning a string: a Task: ... line, then Examples: and a line per example - <input> => <output> (with repr), then Rules: and a line per rule - <rule>. With no rules: no Rules: line at all.

איך הפלט אמור להיראות?What should the output look like?
Task: Slugify a title
Examples:
- 'Hello World' => 'hello-world'
Rules:
- lowercase
- no punctuation
main.py
רמזHint 1: בונים רשימת שורות ומחברים עם "\n".join.Build a list of lines and join with "\n".join.
רמזHint 2: f"- {inp!r} => {out!r}"f"- {inp!r} => {out!r}"
לפתרוןSolution
תרגילExercise 3

מפרט שאפשר להריץA spec you can run

כתבו check(fn, cases) שמקבלת פונקציה ומילון קלט → פלט צפוי, ומחזירה רשימת מחרוזות "<input>: expected <want>, got <got>" לכל מקרה שנכשל (ואם הפונקציה זורקת, got הוא שם השגיאה). השתמשו בה כדי לבחור בין שתי הצעות ל-initials.

Write check(fn, cases) taking a function and a dict input → expected output, and returning strings "<input>: expected <want>, got <got>" for every failing case (if the function raises, got is the error's name). Use it to choose between two suggestions for initials.

איך הפלט אמור להיראות?What should the output look like?
["'jane austen': expected 'J.A.', got 'j.a.'", "'': expected '', got '.'"]
[]
main.py
רמזHint 1: try/except סביב fn(inp) עם type(e).__name__.try/except around fn(inp) with type(e).__name__.
רמזHint 2: f"{inp!r}: expected {want!r}, got {got!r}"f"{inp!r}: expected {want!r}, got {got!r}"
לפתרוןSolution
תרגילExercise 4

גלאי סודותA secrets detector

כתבו find_secrets(text) שמחזירה רשימת זוגות (מספר שורה, סוג) לכל שורה שיש בה מפתח (sk- או sk_ ואחריו לפחות 12 תווים של אותיות/ספרות/-/_) או השמה של סיסמה (password בלי רגישות לאותיות, ואז = או : וערך). מספור שורות מ-1.

Write find_secrets(text) returning pairs (line number, kind) for every line holding a key (sk- or sk_ followed by at least 12 letters/digits/-/_) or a password assignment (password case-insensitive, then = or : and a value). Lines are numbered from 1.

איך הפלט אמור להיראות?What should the output look like?
[(2, 'key'), (3, 'password')]
main.py
רמזHint 1: re.search(r"\bsk[-_][A-Za-z0-9_-]{12,}", line)re.search(r"\bsk[-_][A-Za-z0-9_-]{12,}", line)
רמזHint 2: re.search(r"(?i)password\s*[:=]\s*\S+", line)re.search(r"(?i)password\s*[:=]\s*\S+", line)
לפתרוןSolution
תרגילExercise 5

מדף, צעד 32: שיפור עם AI מאומת בבדיקותShelf, step 32: improving with AI, verified by tests

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

ביקשתם מעוזר פונקציית reading_streak(days) (days: רשימת תאריכים YYYY-MM-DD שבהם קראתם, לא ממוינת ועם כפילויות) שמחזירה את הרצף הארוך ביותר של ימים רצופים. ההצעה שקיבלתם (בקוד ההתחלה) נכשלת בבדיקות. כתבו גרסה שעוברת: ריק → 0, כפילויות נספרות פעם אחת, ומעבר חודש או שנה הוא עדיין רצף.

You asked an assistant for reading_streak(days) (days: YYYY-MM-DD dates you read on, unsorted, with duplicates) returning the longest run of consecutive days. The suggestion you got (in the starter) fails the tests. Write a version that passes: empty → 0, duplicates count once, and crossing a month or year is still a streak.

איך הפלט אמור להיראות?What should the output look like?
3
2 0
streak.py
רמזHint 1: dates = sorted({date.fromisoformat(d) for d in days})dates = sorted({date.fromisoformat(d) for d in days})
רמזHint 2: רצף נמשך כש-(b - a).days == 1.A run continues when (b - a).days == 1.
לפתרוןSolution

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

אתגרChallenge 1

שופט הצעותA suggestion judge

כתבו rank(suggestions, cases) שמקבלת מילון שם → פונקציה, ומחזירה רשימת זוגות (name, passed) ממוינת לפי מספר המקרים שעברו (מהגבוה), ובשוויון לפי שם. פונקציה שזורקת נחשבת כנכשלת במקרה הזה בלבד.

Write rank(suggestions, cases) taking a dict name → function, and returning pairs (name, passed) sorted by cases passed (highest first), and on a tie by name. A function that raises counts as failing that case only.

איך הפלט אמור להיראות?What should the output look like?
[('upper_strip', 4), ('ascii_only', 3), ('upper_only', 3)]
main.py
רמזHint 1: פונקציית עזר passed(fn) שסופרת הצלחות עם try.A helper passed(fn) counting successes with try.
רמזHint 2: sorted(..., key=lambda p: (-p[1], p[0]))sorted(..., key=lambda p: (-p[1], p[0]))
לפתרוןSolution
אתגרChallenge 2

בודק המצאותA hallucination checker

כתבו verify_calls(code) שמקבלת קוד פייתון כמחרוזת, מוצאת בעזרת המודול ast כל קריאה בצורה module.name(...) עבור המודולים math, json, random, statistics, ומחזירה רשימה ממוינת של השמות שלא קיימים במודול האמיתי (בפורמט math.cube).

Write verify_calls(code) taking Python code as a string, using the ast module to find every call of the form module.name(...) for the modules math, json, random, statistics, and returning a sorted list of names that don't exist in the real module (formatted math.cube).

איך הפלט אמור להיראות?What should the output look like?
['json.save', 'math.cube', 'statistics.average']
main.py
רמזHint 1: for node in ast.walk(ast.parse(code)): ו-isinstance(node, ast.Call).for node in ast.walk(ast.parse(code)): and isinstance(node, ast.Call).
רמזHint 2: node.func הוא ast.Attribute עם .value.id (שם המודול) ו-.attr.node.func is an ast.Attribute with .value.id (the module name) and .attr.
לפתרוןSolution

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