כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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
תרגיליםExercises
תרגילExercise 1
טבלת סרטיםA movies table
צרו מסד בזיכרון עם טבלה movies (id, title, year), הכניסו שלושה סרטים עם executemany, והדפיסו את כמות הסרטים ואת הכותרות ממוינות לפי שנה.
Create an in-memory database with a movies (id, title, year) table, insert three movies with executemany, and print the count and the titles sorted by year.
איך הפלט אמור להיראות?What should the output look like?
3
['Cars', 'Up', 'Coco']
main.py
רמזHint 1:CREATE TABLE movies (id INTEGER PRIMARY KEY, title TEXT, year INTEGER)CREATE TABLE movies (id INTEGER PRIMARY KEY, title TEXT, year INTEGER)
רמזHint 2:[r[0] for r in con.execute("SELECT title FROM movies ORDER BY year")][r[0] for r in con.execute("SELECT title FROM movies ORDER BY year")]
כתבו set_pages(con, title, pages) שמעדכנת את מספר העמודים של ספר ומחזירה כמה שורות השתנו, ו-remove(con, title) שמוחקת ספר ומחזירה כמה נמחקו. השתמשו ב-with con:.
Write set_pages(con, title, pages) that updates a book's page count and returns how many rows changed, and remove(con, title) that deletes a book and returns how many were deleted. Use with con:.
איך הפלט אמור להיראות?What should the output look like?
רמזHint 1:cur = con.execute("UPDATE books SET pages = ? WHERE title = ?", (pages, title))cur = con.execute("UPDATE books SET pages = ? WHERE title = ?", (pages, title))
כתבו שאילתה אחת שמחזירה לכל סופר: שם, מספר ספרים וממוצע עמודים מעוגל (ROUND(AVG(pages))), ממוינת לפי מספר ספרים מהגבוה ואז לפי שם. החזירו אותה מ-author_stats(con) כרשימת טאפלים.
Write one query returning for each author: name, book count and rounded average pages (ROUND(AVG(pages))), sorted by count descending then by name. Return it from author_stats(con) as a list of tuples.
איך הפלט אמור להיראות?What should the output look like?
מדף, צעד 23: המדף עובר למסדShelf, step 23: the shelf moves to a database
פרויקט מלווהCompanion project
כתבו מחלקה ShelfDB(path) שמתחברת למסד ויוצרת (IF NOT EXISTS) טבלה books (id, title UNIQUE, author, pages, read DEFAULT 0). מתודות: add(title, author, pages) (מחזירה id), find(text) (חיפוש חלקי בכותרת או בסופר, מחזירה רשימת מילונים), mark_read(title, pages), ו-progress() שמחזירה אחוז כולל מעוגל של עמודים שנקראו. השתמשו ב-con.row_factory = sqlite3.Row כדי לקבל שורות שאפשר להפוך למילון.
Write a class ShelfDB(path) that connects and creates (IF NOT EXISTS) a table books (id, title UNIQUE, author, pages, read DEFAULT 0). Methods: add(title, author, pages) (returns the id), find(text) (partial match on title or author, returns a list of dicts), mark_read(title, pages), and progress() returning the overall rounded percent of pages read. Use con.row_factory = sqlite3.Row to get rows that convert to dicts.
איך הפלט אמור להיראות?What should the output look like?
הוסיפו טבלה reviews (book_id, stars) עם ביקורות, וכתבו top_rated(con, min_reviews=2) שמחזירה טאפלים (title, avg_stars, count) לספרים עם לפחות min_reviews ביקורות, ממוינים לפי ממוצע מהגבוה (ממוצע מעוגל ל-2). השתמשו ב-JOIN, GROUP BY ו-HAVING.
Add a reviews (book_id, stars) table with reviews, and write top_rated(con, min_reviews=2) returning tuples (title, avg_stars, count) for books with at least min_reviews reviews, sorted by average descending (rounded to 2). Use JOIN, GROUP BY and HAVING.
איך הפלט אמור להיראות?What should the output look like?
כתבו migrate(json_text, con) שמקבלת את קובץ ה-JSON של המדף הישן (רשימת מילונים עם title, author, pages ואולי read), יוצרת טבלה אם צריך, ומכניסה הכול בטרנזקציה אחת. ספר בלי title מדולג; כותרת שכבר קיימת לא מוכנסת שוב (INSERT OR IGNORE). מחזירה כמה ספרים נוספו בפועל.
Write migrate(json_text, con) taking the old shelf's JSON (a list of dicts with title, author, pages and maybe read), creating the table if needed, and inserting everything in one transaction. A book without a title is skipped; a title that already exists isn't inserted again (INSERT OR IGNORE). Return how many books were actually added.
איך הפלט אמור להיראות?What should the output look like?