هياكل البيانات والخوارزميات (DSA27) · الأسبوع 3

Recursion · data structures and classes in Python (Lecture 03, Lab 03)

هذه اختبارات للتدريب الذاتي، تُحفظ إجاباتك على هذا الجهاز فقط ولا تُرسل إلى أحد. الأسئلة والشرح بالإنجليزية لغة المقرر.

النتيجة: 0 / 44 · أُجيب: 0

  1. W3-M01 تذكّر

    Every correct recursive function needs:

  2. W3-M02 تطبيق

    With def factorial(n): return 1 if n == 0 else n * factorial(n - 1), what does factorial(-1) do?

  3. W3-M03 تذكّر

    By default, CPython stops a recursion at a depth of about:

  4. W3-M04 تطبيق

    How much stack space does factorial(n) use?

  5. W3-M05 تطبيق

    T(n) = T(n/2) + c solves to:

  6. W3-M06 تطبيق

    T(n) = 2T(n-1) + c solves to:

  7. W3-M07 تطبيق

    T(n) = T(n-1) + cn solves to:

  8. W3-M08 تطبيق

    How many moves does Towers of Hanoi need for 5 disks?

  9. W3-M09 تطبيق

    How many subsets does a set of 4 items have (including the empty set and the whole set)?

  10. W3-M10 تطبيق

    How many orderings (permutations) do 4 distinct items have?

  11. W3-M11 تطبيق

    How many calls in total does the naive fib(5) make (counting the first)?

  12. W3-M12 تعليل

    Fast power written as half = power(x, n // 2) * power(x, n // 2) (then × x if n is odd). Its cost is:

  13. W3-M13 تعليل

    list_max(values) recurses on values[1:]. Its running time is:

  14. W3-M14 تذكّر

    Memoisation means:

  15. W3-M15 تعليل

    hanoi(20) makes over a million calls. At most how many frames are on the call stack at the same time?

  16. W3-M16 تطبيق

    What does f(3) print?

    def f(n):
        if n == 0:
            return
        print(n, end=" ")
        f(n - 1)
        print(n, end=" ")
  17. W3-M17 تعليل

    What should subsets([]) return?

  18. W3-M18 تذكّر

    Tail-call optimisation in Python:

  19. W3-M19 تطبيق

    marks = [3, 1, 2]; result = marks.sort(). What is result?

  20. W3-M20 تذكّر

    What is type({})?

  21. W3-M21 تذكّر

    The average cost of x in s for a Python set of n items is:

  22. W3-M22 تعليل

    What is wrong with this class?

    class Dog:
        tricks = []
        def __init__(self, name):
            self.name = name
        def add_trick(self, trick):
            self.tricks.append(trick)
  23. W3-M23 تذكّر

    Calling a function that contains yield:

  24. W3-M24 تعليل

    Why should a queue not be a Python list used with pop(0)?

  25. W3-M25 تطبيق

    What is counts after this?

    counts = {}
    for w in "a b a".split():
        counts[w] = counts.get(w, 0) + 1
  26. W3-M26 تطبيق

    The recurrence of naive Fibonacci, T(n) = T(n-1) + T(n-2) + c, grows like:

  27. W3-M27 تطبيق

    Lecture 03's to_binary:

    def to_binary(n):
        if n < 2:
            return str(n)
        return to_binary(n // 2) + str(n % 2)

    What does to_binary(6) return?

  28. W3-M28 تطبيق

    With the course gcd (return a if b == 0, else return gcd(b, a % b)), how many calls does gcd(48, 18) make in total, counting the first?

  29. W3-M29 تعليل

    In binary_strings, the base case is changed from return [""] to return []. What does binary_strings(2) return now?

    def binary_strings(n):
        if n == 0:
            return []
        shorter = binary_strings(n - 1)
        return ["0" + s for s in shorter] + ["1" + s for s in shorter]
  30. W3-M30 تعليل

    flatten descends only into items for which isinstance(item, list) is true, not into everything that is iterable. Why?

  31. W3-M31 تذكّر

    With the course's fast power (one call on exponent // 2, base case exponent == 0), at most how many frames of power are on the call stack at once during power(2, 1000)?

  32. W3-M32 تذكّر

    The memoised fib_memo(30) of Lecture 03 (memo in an Array) makes how many calls in total, counting the first?

  33. W3-M33 تعليل

    The course merge_sorted is called on two sorted lists of 600 numbers each, and raises RecursionError. Why?

  34. W3-M34 تعليل

    merge_sorted compares the heads with left[0] <= right[0]. What would change if it used < instead?

  35. W3-M35 تطبيق

    sum_digits(n) makes one call on n // 10 and does O(1) work besides. Its running time, in terms of the number n, is:

  36. W3-M36 تطبيق

    With Lecture 03's depth (0 for a non-list; otherwise 1 + the deepest item, where an empty list has depth 1), what is depth([1, [2, [3]], []])?

  37. W3-M37 تذكّر

    In the recursive solution of Towers of Hanoi with n disks, how many times does the largest disk move?

  38. W3-M38 تطبيق

    In the moves returned by hanoi(n), how many times does the smallest disk move?

  39. W3-M39 تعليل

    Why is gcd easy to rewrite as a plain while loop, while hanoi would need an explicit stack?

  40. W3-M40 تعليل

    Lecture 03's array_max(arr, i=0) recurses on i + 1 and never slices. What happens when it is called on a course Array of 5,000 numbers?

  41. W3-M41 تطبيق

    The course subsets:

    def subsets(items):
        if not items:
            return [[]]
        rest = subsets(items[1:])
        return rest + [[items[0]] + subset for subset in rest]

    What does subsets([1, 2]) return, in this order?

  42. W3-M42 تذكّر

    Which of these can be used as a dict key or a set member?

  43. W3-M43 تعليل

    What does this print?

    t = (1, [2])
    t[1].append(3)
    print(t)
  44. W3-M44 تطبيق

    What does this print?

    def countdown(n):
        while n > 0:
            yield n
            n -= 1
    
    g = countdown(3)
    next(g)
    print(list(g))

36 درجات

أسئلة المقال

10 أسئلة مقالية (36 درجة). اكتب إجابتك أولًا، ثم اعرض الإجابة النموذجية ونقاط التصحيح، وقدّر درجتك بأمانة: كل نقطة تساوي درجة تقريبًا.

مجموع درجات المقال: 0 / 36

  1. W3-E1 تطبيق 4 درجات

    Draw the call stack at every step of factorial(3), from the first call until the final value is returned. Label winding, the base case, and unwinding, and show the value each frame returns.

  2. W3-E2 تعليل 4 درجات

    Explain why the naive recursive Fibonacci takes exponential time, using a recursion tree. Show how memoisation fixes it, and state the new time and space costs.

  3. W3-E3 تعليل 3 درجات

    When should you prefer recursion, and when a loop? Include two concerns that are specific to Python.

  4. W3-E4 تعليل 4 درجات

    Describe the recursive strategy for Towers of Hanoi. Write its recurrence, solve it, and explain why no algorithm can use fewer moves.

  5. W3-E5 تعليل 3 درجات

    Explain "trust the recursion" and how it relates to mathematical induction.

  6. W3-E6 تعليل 4 درجات

    For each job, choose list, tuple, set or dict and justify by cost: (a) the marks of a class, in the order entered; (b) checking whether a student ID has already been registered; (c) looking up a student's name by ID; (d) one student's (ID, name, mark), used as a key elsewhere.

  7. W3-E7 تعليل 4 درجات

    hanoi(20) makes over a million calls, yet it never has more than about 20 frames on the call stack. Explain why the space a recursion needs is set by its depth and not by its number of calls. Give the depth of factorial(n), of the fast power(x, n) and of hanoi(n), and explain why raising sys.setrecursionlimit is not the right fix for a recursion that must go 100,000 deep.

  8. W3-E8 تعليل 4 درجات

    Three versions of power(x, n): (i) x * power(x, n - 1); (ii) half = power(x, n // 2), then half * half (× x if n is odd); (iii) power(x, n // 2) * power(x, n // 2) (× x if n is odd). For each, write the recurrence and its solution, and say what happens for n = 4,096.

  9. W3-E9 تعليل 3 درجات

    Rewrite gcd as a loop, and explain why that rewrite is easy. Explain why rewriting hanoi without recursion is harder, and what the loop version must keep for itself.

  10. W3-E10 تعليل 3 درجات

    Explain why binary_strings(0) must return [""], and subsets([]) must return [[]], rather than []. What goes wrong with [], and how many results do binary_strings(n) and subsets of n items return?