final exam review (class slides)

CSC – Final exam review

Announcements

  • No lecture this Wednesday, me and TAs will be here (Gittings 129b) to answer questions.

  • Final exam will be on this Friday May 03, 6:00 - 8:00pm in ENR2 N120.

  • Bring your photo ID

  • Last day of office hour is May 1st.

Attendance

Go to gradescope and answer attendance question.

Midterm 3 - question 5

Write a function called matches that takes two string arguments, s1 and s2, which you may assume have equal length. The function should return a string that is the same length as s1 and s2.

If the corresponding characters in s1 and s2 are the same, then the corresponding character in the result is that character. If they are different, then the corresponding character in the result is "*".

assert matches("abc", "abc") == "abc"
assert matches("abc", "xyz") == "***"
assert matches("abcd", "cbxe") == "*b**"
assert matches("", "") == ""

Midterm 3 - question 5 wrong solution

def matches(s1, s2):
    result = ""
    for char in s1:
        if char in s2:
            result += char
        else:
            result += "*"
    return result

def main():
  assert matches("abc", "abc") == "abc"
  assert matches("abc", "xyz") == "***"
  assert matches("abcd", "cbxe") == "*b**" # Will fail this test case
  assert matches("", "") == ""

main()

Midterm 3 - question 5 solution

def matches(s1, s2):
    result = ""
    for index in range(len(s1)):
        if s1[index] == s2[index]:
            result += s1[index]
        else:
            result += "*"
    return result

def main():
  assert matches("abc", "abc") == "abc"
  assert matches("abc", "xyz") == "***"
  assert matches("abcd", "cbxe") == "*b**"
  assert matches("", "") == ""

main()

Midterm 3 - question 6

Given code for bubble sort:

def bubble_sort(items):
  swapped = False
  end = len(items)-1
  while not swapped:
    swapped = True
    for i in range(end):
      if items[i] > items[i+1]:
        items[i], items[i+1] = items[i+1], items[i]
        swapped = False
    end -= 1

How many sweeps and swaps of [30, 13, 5, 1, 14]?

Midterm 3 - question 6 solution

1st sweep:

  • 1st swap: [13, 30, 5, 1, 14]

  • 2nd swap: [13, 5, 30, 1, 14]

  • 3rd swap: [13, 5, 1, 30, 14]

  • 4th swap: [13, 5, 1, 14, 30]

2nd sweep:

  • 5th swap: [5, 13, 1, 14, 30]

  • 6th swap: [5, 1, 13, 14, 30]

Midterm 3 - question 6 solution (continued)

3rd sweep:

  • 7th swap: [1, 5, 13, 14, 30]

4th sweep:

List already sorted, no more swaps


4 sweeps and 7 swaps

Midterm 3 - question 8 (Bonus)

Write python code (multiple functions) that given a list of lists of integers as argument, it mutates the list by removing all prime numbers from each sublist.

if __name__ == "__main__":
  original_list = [[2, 3, 4, 4], [5, 7, 40, 50, 11, 13, 20], [17, 19, 23]]
  remove_primes(original_list)
  assert original_list == [[4, 4], [40, 50, 20], []]

Midterm 3 - question 8 solution

def is_prime(number):
    if number == 1:
         return False
    # If num is divisible by any number between 2 and n / 2, it is not prime
    for denominator in range(2, int(number/2 + 1)):
          if number % denominator == 0:
               return False
    return True

def remove_primes(lists):
    for sublist in lists:
        for i in range(len(sublist)-1, -1, -1):
            if is_prime(sublist[i]):
                sublist.pop(i)
    return lists
  
if __name__ == "__main__":
  original_list = [[2, 3, 4, 4], [5, 7, 40, 50, 11, 13, 20], [17, 19, 23]]
  remove_primes(original_list)
  assert original_list == [[4, 4], [40, 50, 20], []]