Binary search, sorting, Big-O and choosing a data structure.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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
חיפוש בינאריBinary search
כתבו binary_search(nums, target) שמחזירה את האינדקס של target ברשימה ממוינת, או -1. בלי index ובלי bisect.
Write binary_search(nums, target) returning target's index in a sorted list, or -1. Without index or bisect.
איך הפלט אמור להיראות?What should the output look like?
3 -1
main.py
רמזHint 1:lo, hi = 0, len(nums) - 1 ו-while lo <= hi.lo, hi = 0, len(nums) - 1 and while lo <= hi.
רמזHint 2:mid + 1 או mid - 1, אחרת נתקעים.mid + 1 or mid - 1, otherwise it hangs.
common_slow(a, b) מחזירה את הפריטים של a שמופיעים גם ב-b (בסדר של a, בלי כפילויות), אבל היא O(n²). כתבו common_fast(a, b) שמחזירה אותה תוצאה ב-O(n) בעזרת set.
common_slow(a, b) returns a's items that also appear in b (in a's order, no duplicates), but it's O(n²). Write common_fast(a, b) returning the same result in O(n) using a set.
איך הפלט אמור להיראות?What should the output look like?
['Dune', 'Matilda'] True
main.py
רמזHint 1:in_b = set(b) פעם אחת לפני הלולאה.in_b = set(b) once before the loop.
רמזHint 2:set נוסף seen כדי לא להוסיף פעמיים.Another set seen to avoid adding twice.
כתבו selection_sort(items) שמחזירה רשימה ממוינת חדשה בלי sort/sorted: בכל סיבוב מוצאים את הקטן ביותר מהחלק שעוד לא מוין ומחליפים אותו לתחילת החלק.
Write selection_sort(items) returning a new sorted list without sort/sorted: each round, find the smallest in the unsorted part and swap it to the start of that part.
איך הפלט אמור להיראות?What should the output look like?
[96, 240, 412, 604] [412, 96, 604, 240]
main.py
רמזHint 1:a = list(items) כדי לא לשנות את המקור.a = list(items) so the original isn't changed.
רמזHint 2:לולאה פנימית שמוצאת smallest = i ומשווה.An inner loop finding smallest = i and comparing.
תיקייה מיוצגת כמילון: מפתח שהוא שם קובץ ממופה לגודל (int), ומפתח שהוא שם תיקייה ממופה למילון נוסף. כתבו total_size(folder) רקורסיבית שמחזירה את סך הגדלים, ו-deepest(folder) שמחזירה את העומק המקסימלי (תיקייה ריקה = 1).
A folder is a dict: a file name maps to a size (int), and a sub-folder name maps to another dict. Write a recursive total_size(folder) returning the total size, and deepest(folder) returning the maximum depth (an empty folder = 1).
איך הפלט אמור להיראות?What should the output look like?
3050 3
main.py
רמזHint 1:isinstance(v, dict) מבדיל תיקייה מקובץ.isinstance(v, dict) tells a folder from a file.
רמזHint 2:1 + max((deepest(v) for v in ... if dict), default=0)1 + max((deepest(v) for v in ... if dict), default=0)
מדף, צעד 34: חיפוש מהיר בספרייה גדולהShelf, step 34: fast search in a big library
פרויקט מלווהCompanion project
כתבו מחלקה Index(books) שבונה פעם אחת: מילון ISBN → ספר, ומילון מילה (באותיות קטנות) → set של ISBN, מכל מילה בכותרת ובשם הסופר. by_isbn(isbn) היא O(1) (או None), ו-search(query) מחזירה כותרות ממוינות של ספרים שמכילים את כל מילות השאילתה. בדיקה על 50,000 ספרים צריכה לעבור מהר.
Write an Index(books) class that builds once: a dict ISBN → book, and a dict word (lowercase) → set of ISBNs, from every word in the title and author. by_isbn(isbn) is O(1) (or None), and search(query) returns sorted titles of books containing all query words. A test on 50,000 books must be fast.
איך הפלט אמור להיראות?What should the output look like?
כתבו merge_sort(items) רקורסיבית: רשימה באורך 0 או 1 כבר ממוינת; אחרת מחלקים לשניים, ממיינים כל חצי, וממזגים שתי רשימות ממוינות בפונקציה merge(a, b). זה O(n log n).
Write a recursive merge_sort(items): a list of length 0 or 1 is already sorted; otherwise split in two, sort each half, and merge two sorted lists with merge(a, b). This is O(n log n).
איך הפלט אמור להיראות?What should the output look like?
[96, 96, 208, 240, 412, 474, 604]
main.py
רמזHint 1:merge: שני אינדקסים, ובכל צעד לוקחים את הקטן מבין השניים.merge: two indexes, each step take the smaller of the two.
רמזHint 2:בסוף מוסיפים את מה שנשאר: out + a[i:] + b[j:].At the end add the leftovers: out + a[i:] + b[j:].
כתבו top_k(books, k) שמחזירה את k הספרים הארוכים ביותר (לפי pages, מהארוך לקצר, ובשוויון לפי כותרת) בלי למיין את כל הרשימה: השתמשו ב-heapq.nlargest, שהוא O(n log k). ואז top_k_manual שעושה אותו דבר עם ערימה (heapq.heappush/heappushpop) בגודל k.
Write top_k(books, k) returning the k longest books (by pages, longest first, ties by title) without sorting the whole list: use heapq.nlargest, which is O(n log k). Then top_k_manual doing the same with a heap (heapq.heappush/heappushpop) of size k.
איך הפלט אמור להיראות?What should the output look like?