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.
Go to gradescope and answer attendance question.
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 "*".
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()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()Given code for bubble sort:
How many sweeps and swaps of [30, 13, 5, 1, 14]?
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]
3rd sweep:
[1, 5, 13, 14, 30]4th sweep:
List already sorted, no more swaps
4 sweeps and 7 swaps
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.
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], []]