מבחן 6: אלגוריתמים ומתקדםExam 6: Algorithms and advanced
סיבוכיות, חיפוש ומיון, רקורסיה, גנרטורים ו-BFS
Complexity, searching and sorting, recursion, generators and BFS
90 דקותminutes4 שאלות בחירהmultiple choice3 שאלות קודcoding questions100 נקודותpoints
90:00השעון רק עוזר להתאמן. אפשר גם בלי.The timer is just for practice. You can skip it.
חלק א: שאלות בחירה (5 נקודות לשאלה)Part A: multiple choice (5 points each)
התשובות מתגלות רק כשמסיימים את המבחן.Answers are revealed only when you finish the exam.
חלק ב: שאלות קודPart B: coding questions
שאלהQuestion 5
מיזוג רשימות ממוינותMerging sorted lists
כתבו merge(a, b) שממזגת שתי רשימות ממוינות לרשימה ממוינת אחת ב-O(n+m), בלי sorted ובלי sort.
Write merge(a, b) merging two sorted lists into one sorted list in O(n+m), without sorted or sort.
איך הפלט אמור להיראות?What should the output look like?
[1, 2, 3, 4, 9, 10, 11]
main.py
רמזHint 1:שני מצביעים i, j.Two pointers i, j.
רמזHint 2:בסוף מוסיפים את מה שנשאר.At the end, add whatever's left.
הפתרון שלנוOur solution
main.py
def merge(a, b):
out, i, j = [], 0, 0while i <len(a) and j <len(b):if a[i] <= b[j]:
out.append(a[i])
i +=1else:
out.append(b[j])
j +=1return out + a[i:] + b[j:]
print(merge([1, 4, 9], [2, 3, 10, 11]))
פלטOutput
[1, 2, 3, 4, 9, 10, 11]
יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.
מחווןRubric (25 נקודותpoints)
כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.
שאלהQuestion 6
תמורותPermutations
כתבו perms(items) רקורסיבית שמחזירה רשימה של כל הסידורים של רשימה (כרשימות), בסדר שבו בוחרים כל פעם את האיבר הבא לפי הסדר המקורי. בלי itertools.
Write a recursive perms(items) returning a list of all orderings of a list (as lists), in the order you get by picking each next item in the original order. No itertools.
איך הפלט אמור להיראות?What should the output look like?
רמזHint 1:תנאי עצירה: רשימה ריקה → [[]].Base case: an empty list → [[]].
רמזHint 2:לכל i: items[i] + כל התמורות של השאר.For each i: items[i] + every permutation of the rest.
הפתרון שלנוOur solution
main.py
def perms(items):ifnot items:return [[]]
out = []
for i, first inenumerate(items):for rest in perms(items[:i] + items[i +1:]):
out.append([first] + rest)
return out
print(perms([1, 2, 3]), len(perms(list("abcd"))))
יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.
מחווןRubric (30 נקודותpoints)
כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.
שאלהQuestion 7
גנרטור חלונותA window generator
כתבו גנרטור windows(items, size) שמניב (yield) טאפלים של חלונות רצופים בגודל size, וגם moving_average(items, size) שמחזירה רשימת ממוצעים מעוגלים לספרה אחת בעזרת הגנרטור.
Write a generator windows(items, size) yielding tuples of consecutive windows of size size, and moving_average(items, size) returning a list of averages rounded to one decimal using the generator.
איך הפלט אמור להיראות?What should the output look like?
[(1, 2), (2, 3), (3, 4)] [20.0, 30.0, 40.0]
main.py
רמזHint 1:for i in range(len(items) - size + 1):for i in range(len(items) - size + 1):
הפתרון שלנוOur solution
main.py
def windows(items, size):for i inrange(len(items) - size +1):yieldtuple(items[i:i + size])
def moving_average(items, size):return [round(sum(w) / size, 1) for w in windows(items, size)]
print(list(windows([1, 2, 3, 4], 2)), moving_average([10, 20, 30, 40, 50], 3))
פלטOutput
[(1, 2), (2, 3), (3, 4)] [20.0, 30.0, 40.0]
יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.
מחווןRubric (25 נקודותpoints)
כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.