Problem 1 --------- (a) [2, 5, 8] (b) [0, 1, 2, 3, 0, 1, 2, 0, 1] (c) oksure (d) [4, 2, 0] (e) 2 (f) cftu (g) True (h) 1 (i) ['hello', 'bye', [1, 2], [2, 3], [3, 4]] (j) [['ne', 'd'], ['nesting']] Problem 2(a) ------------ ['Thisisok', 'Butthisisnot'] ['Uijtjtpl', 'Cvuuijtjtopu'] Problem 2(b) ------------ [2, 5, 1, 4, 15] [2, 5, 1, 4, 15] [2, 5, 1, 4, 15] [2, 5, 1, 4, 15] [2, 5, 1, 4] Problem 3(a) ----------- def processFile(filename): # Open the given file f = open(filename) # Initialize masterList masterList = [] # Process each line in the file for line in f: # Extract all the integers in the line into a list L L = [int(x) for x in line.split(",")] # Use a list comprehension to construct a boolean list # corresponding to L indicating which elements are > 100. # For example if L = [10, 200, 11] then the constructed # boolean list will be [False, True, False]. discardList = [x > 100 for x in L] # If there are no True values in discardList then # append L to the masterList if not (True in discardList): masterList.append(L) return masterList Problem 3(b) ----------- # Returns True if s2 is obtained from s1 by substituting exactly one # character for another or by deleting exactly one character or by # inserting exactly one character. def similar(s1, s2): # If the difference in lengths of s1 and s2 is # more than 1, return False if abs(len(s1) - len(s2)) > 1: return False # Check if s2 is obtained from s1 with one substitution if len(s1) == len(s2): for i in range(len(s1)): # Check if s1 with the i-th character removed equals # s2 with the i-th charecter removed if s1[:i] + s1[i+1:] == s2[:i] + s2[i+1:]: return True # Check if s2 is obtained from s1 by one deletion if len(s1) > len(s2): for i in range(len(s1)): # Check if s1 with the i-th character removed equals s2 if s1[:i] + s1[i+1:] == s2: return True # Check if s1 is obtained from s2 by one deletion if len(s2) > len(s1): for i in range(len(s2)): # Check if s2 with the i-th character removed equals s1 if s2[:i] + s2[i+1:] == s1: return True return False Problem 4 ----------- # Returns a contiguous slice of L whose sum is maximum among # all contiguous slices. def maxSumSubsequence(L): # Initially we will assume that the slice with just the # element in position 0 is the slice with maximum sum. left = 0 right = 0 # Now we will generate all other contiguous slices for i in range(len(L)): for j in range(i+1, len(L)): # We assign to newSum the sum of the current slice # and then check if newSum is larger than maxSum and # update maxSum if this is the case if sum(L[i:j]) > sum(L[left:right]): left = i right = j return L[left:right]