# # Question 1 # # # int 0 # bool True # int 10 # str 'bc' # str 'z' # list [ 'aa', 6, [0, 0] ] # str 'COMPSCI 101' # float 256.0 # # Question 2 # # Part A # # Testcases 2 and 3 fail because of an extra space before & # Duvall, LLC # Terrible & Great, LLC # Pearson Hardman & Specter, LLC # Part B # # Testcase 1 fails because an & is always added, even for just one name # & Duvall, LLC # Terrible & Great, LLC # Pearson Hardman & Specter, LLC # Part C # # Testcase 1 fails because the name is not included when there is only one # , LLC # Terrible & Great, LLC # Pearson Hardman & Specter, LLC # # Question 3 # # Part A # def sumPairs (values): if len(values) == 1: return values result = [] for i in range(len(values)-1): result.append(values[i] + values[i+1]) return result # ALTERNATE SOLUTION def sumPairs (values): if len(values) == 1: return values else: return [ sum(values[i-1:i+1]) for i in range(1, len(values)) ] # Part B # def pascalTriangle (level): result = [] row = [ 1 ] for k in range(level): result.append(row) row = sumPairs([0] + row + [0]) return result # ALTERNATE SOLUTION def pascalTriangle (level): result = [ [1] ] for k in range(level - 1): result.append(sumPairs([0] + result[k] + [0])) return result # Part C # def numCombinations (n, k): return pascalTriangle(n + 1)[n][k] # # Question 4 # # Part A # # given 'hello' and 'ohell', checks for 'hello' in 'ohellohell' # by concatenating rotated string, the start should line up with the end, # meaning the original string must be there if no letters are out of order # because all possible rotations can be found in there # Part B # # given 'hello' and 'ohell', find rotation point, i.e., 'ello' and 'h' # checks every index, i.e., every possible rotation, to see where the point # of rotation occurs # Part C # # given 'hello' and 'ohell', compares the two lists # ['elloh', 'hello', 'llohe', 'lohel', 'ohell'] # ['elloh', 'hello', 'llohe', 'lohel', 'ohell'] # checks if both strings produce exactly the same rotations for all possible # indices, sorts them to guarantee rotations they appear in the same order no # matter the order in which they were generated # # Question 5 # # Part A # def isValid (ccard): total = 0 for k in range(len(ccard)): digit = int(ccard[k]) if k % 2 != 0: digit *= 2; if digit >= 10: digit -= 9; total += digit return total % 10 == 0 # Part B # def isExpired (useDate, expireDate): expired = expireDate.split("/") used = useDate.split("/") if expired[1] < used[1]: return True elif expired[1] > used[1]: return False elif expired[0] < used[0]: return True return False # ALTERNATE SOLUTION def isExpired (useDate, expireDate): (eMonth, eYear) = expireDate.split("/") (uMonth, uYear) = useDate.split("/") return eYear < uYear or (eYear == uYear and eMonth < uMonth) # ALTERNATE SOLUTION def isExpired3 (useDate, expireDate): uDate = useDate[3:] + useDate[:2] eDate = expireDate[3:] + expireDate[:2] return eDate < uDate # Part C # def checkTransaction (cardInfo, useDate): cardValues = cardInfo.split(":") if isValid(cardValues[0]): if isExpired(useDate, cardValues[1]): return "EXPIRED" else: return "APPROVED" else: return "INVALID" # ALTERNATE SOLUTION def checkTransaction (cardInfo, useDate): cardValues = cardInfo.split(":") if not isValid(cardValues[0]): return "INVALID" elif isExpired(useDate, cardValues[1]): return "EXPIRED" else: return "APPROVED"