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

Complexity and the Array · control flow, functions, errors (Lecture 02, Lab 02)

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

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

  1. W2-M01 تطبيق

    f(n) = 3n2 + 5n + 2 is:

  2. W2-M02 تعليل

    Which statement is true?

  3. W2-M03 تذكّر

    f(n) = O(g(n)) means:

  4. W2-M04 تطبيق

    In the worst case, how many comparisons does linear search make on an array of n elements?

  5. W2-M05 تطبيق

    The target is present and equally likely to be at any of the n positions. On average, how many comparisons does linear search make?

  6. W2-M06 تطبيق

    What is the running time of this loop?

    i = n
    while i > 1:
        i = i // 2
  7. W2-M07 تطبيق

    What is the running time?

    for i in range(n):
        for j in range(i):
            count += 1
  8. W2-M08 تطبيق

    What is the running time?

    for i in range(n):
        for j in range(5):
            count += 1
  9. W2-M09 تذكّر

    For a Python list of n items, x in values costs:

  10. W2-M10 تذكّر

    Which Python list operation is O(1) (amortised)?

  11. W2-M11 تعليل

    Why is reading a[i] from an array O(1)?

  12. W2-M12 تطبيق

    A partly filled array has size 6. How many elements must move to insert a new value at index 2?

  13. W2-M13 تطبيق

    When inserting into an array, the elements after the gap must be shifted right. In which order?

  14. W2-M14 تطبيق

    a = Array(4), then a[-1]. What happens?

  15. W2-M15 تطبيق

    a = Array(5), then a[0] = 7. What is len(a)?

  16. W2-M16 تطبيق

    A 3 × 4 matrix (3 rows, 4 columns) is stored flat in row-major order. At which index is element (2, 1)?

  17. W2-M17 تطبيق

    What extra space does reverse_in_place(arr) need (swap from both ends, no second array)?

  18. W2-M18 تعليل

    Under the course storage rule (Lecture 02), which of these is not allowed?

  19. W2-M19 تذكّر

    Python's built-in list is best described as:

  20. W2-M20 تطبيق

    An O(n) loop is followed by an O(n2) loop. The whole is:

  21. W2-M21 تطبيق

    values is a Python list of n items. The cost of:

    for i in range(len(values)):
        if values[i] in values[i + 1:]:
            return True
  22. W2-M22 تذكّر

    When does the else block of a for loop run?

  23. W2-M23 تطبيق

    Given

    def f(a, L=[]):
        L.append(a)
        return L

    what does f(1) then print(f(2)) print?

  24. W2-M24 تطبيق

    What is the value of 0 or 'x' and ''?

  25. W2-M25 تطبيق

    list(range(2, 11, 3)) is:

  26. W2-M26 تطبيق

    What does print(g()) print?

    def g():
        try:
            return 1
        finally:
            print("bye")
  27. W2-M27 تذكّر

    In the RAM model of Lecture 02, which of these is not one basic step?

  28. W2-M28 تطبيق

    An algorithm does n2 basic steps and takes 1 second at n = 10,000. About how long will it take at n = 100,000, on the same machine?

  29. W2-M29 تعليل

    Algorithm A does 1000n steps; algorithm B does n2 steps. Which statement is true?

  30. W2-M30 تعليل

    Which sentence about linear search is correct?

  31. W2-M31 تطبيق

    What is the running time of this loop?

    i = 0
    while i * i < n:
        i += 1
  32. W2-M32 تعليل

    A recursive total(values) makes one call per element and creates no new data. Its extra space is:

  33. W2-M33 تطبيق

    A partly filled Array has size 7. How many elements move during remove_at(arr, 7, 2)?

  34. W2-M34 تعليل

    remove_at ends with arr[size - 1] = None. Why?

  35. W2-M35 تذكّر

    In C, after int a[6];, the statement a[6] = 0;:

  36. W2-M36 تعليل

    One Python array can hold an int, a str and a list, and still index in O(1). Why?

  37. W2-M37 تذكّر

    a = Array(3), then a[0:2]. What happens?

  38. W2-M38 تطبيق

    arr holds [1, 2, 3, 4, 5, 6]. What does it hold after rotate_left(arr, 8)?

  39. W2-M39 تعليل

    merge_sorted(a, b) takes the front of a when a[i] <= b[j]. Why <= rather than <?

  40. W2-M40 تطبيق

    A 3 × 4 matrix holding the values 0 to 11 row by row is stored flat in row-major order. transpose_flat(arr, 3, 4) returns the 4 × 3 transpose, also row-major. At which index of the result is the value 1?

  41. W2-M41 تعليل

    "Any sort that works only by comparing elements needs Ω(n log n) comparisons in the worst case." This means:

  42. W2-M42 تطبيق

    How many pairs does is_sorted(arr) compare when arr holds [5, 1, 2, 3, 4, 6, 7, 8]?

  43. W2-M43 تطبيق

    What happens when increment() is called?

    count = 0
    
    def increment():
        count = count + 1
  44. W2-M44 تعليل

    What does print(users) show?

    users = ['guest1', 'guest2', 'ali', 'guest3']
    for u in users:
        if u.startswith('guest'):
            users.remove(u)
    print(users)

37 درجات

أسئلة المقال

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

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

  1. W2-E1 تعليل 4 درجات

    Big-O throws away constant factors and lower-order terms. Explain why each is thrown away, give one situation where doing so is misleading, and explain how measuring complements counting.

  2. W2-E2 تذكّر 4 درجات

    Define O, Ω and Θ precisely. For f(n) = 3n2 + 2n, give one true statement using each, and one true but uninformative O statement.

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

    Describe the best, worst and average case of linear search, with the number of comparisons for each. Which one does "linear search is O(n)" usually refer to, and why is that the default?

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

    Explain why indexing an array is O(1) but inserting into the middle is O(n). What does the fact that an array has a fixed size force you to do when it fills up, and what does that cost?

  5. W2-E5 تعليل 4 درجات

    State the course storage rule from Lecture 02. Give the reason for it, list what remains allowed, and show how two structures from later in the course follow it.

  6. W2-E6 تذكّر 3 درجات

    Compare Python's list, array.array, numpy.ndarray and a ctypes array on: can it grow, what it holds, and whether it hides costs. Why is the course Array built on ctypes?

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

    values is a Python list of n numbers. Give the running time of each numbered part of this function, justify each with one of the five rules, and give the total. Why is part (3) not O(n2), although it hides a slice and an in inside a loop?

    def report(values):
        total = 0
        for v in values:                 # (1)
            total += v
        i = len(values)
        while i > 1:                     # (2)
            i //= 2
        for v in values:                 # (3)
            if v in values[:10]:
                total -= v
        return total
  8. W2-E8 تعليل 4 درجات

    Define the auxiliary space of an algorithm. Give the extra space of reverse_in_place, resized(arr, 2n), merge_sorted(a, b) and a recursive total(values), with a reason for each. Why does recursion use space even when it creates no data, and what limit does Python put on it?

  9. W2-E9 تطبيق 3 درجات

    Explain row-major order. A 5 × 7 matrix (5 rows, 7 columns) is stored flat in row-major order: at which flat index is element (3, 4), and which (row, column) is at flat index 30? Why is reaching (r, c) still O(1)?

  10. W2-E10 تعليل 4 درجات

    The course Array refuses negative indices, slicing and append. For each refusal, explain why it is deliberate and what you write instead. Then explain the difference between len(a) and the size of a partly filled array, and who keeps track of each.