Problem 1 (a) ['levels', 'of'] (b) ['This', ['is', 'a'], 'n', 'e', 's'] (c) ['nested', 'nesting'] (d) 21 (e) [['n', 'st', 'd'], ['n', 'sting']] (f) 0 (g) 'nestingnested' (h) [1, 5] (i) 'if' (j) ['This', 'list', 'several', 'levels'] Problem 2(a) ----------- (i) ['help', 'hlep', 'hple', 'help', 'hepl', 'help'] (ii) False True False False Problem 2(b) ----------- (i) 0 4 9 5 7 9 5 5 6 6 6 6 7 6 (ii) 0 5 11 0 2 4 3 3 4 (iii) 0 5 11 0 2 4 3 3 4 4 4 4 5 4 Problem 3 --------- (a) f = open("presidents.txt", "r") line = f.readline().rstrip() presidentList = [] while line: [firstName, lastName, lifeSpan] = line.split() [birthYear, deathYear] = lifeSpan.split("-") presidentList.append([int(deathYear) - int(birthYear), firstName + " " + lastName]) line = f.readline() presidentList.sort(reverse = True) for president in presidentList: print president[1] f.close() (b) f = open("matrix.txt", "r") mat = [] zeroes = 0 for line in f: mat.append([0]*zeroes + map(int, line.split())) zeroes = zeroes + 1 print mat for i in range(len(mat)): for j in range(i+1, len(mat)): mat[j][i] = mat[i][j] print mat Problem 4 --------- def findBitonicMax(L): left = 0 right = len(L)-1 while left <= right: # Special Case 1: problem is of size 1 if left == right: return left # Special Case 2: problem is of size 2 if left == right-1: if L[left] > L[right]: return left else: return right # Typical case, when the list to be searched L[left..right] # consists of 3 or more elements. Note that mid-1 and mid+1 # are valid indices in this case. mid = (left + right)/2 if L[mid] > L[mid-1] and L[mid] > L[mid+1]: #mid is max return mid elif L[mid] < L[mid+1]: # max is to the right of mid left = mid + 1 elif L[mid] < L[mid-1]: # max is to the left of mid right = mid - 1