Author: Darin Brezeale, Modified by Alexandra Stefan, May 2013 for code compatible with Python 3.2 Sample exam problems for CSE 1310 that a student should be capable of completing after studying file I/O and dictionaries. Of course, anything covered prior to these topics you should also know. April 23, 2012 1. What does the following program print? def clean( line ): newline = [ ] t = line.strip().split(',') for x in t : newline.append( int(x) ) return newline def fx( d ) : rows = len( d ) cols = len( d[0] ) c = 0 while c < cols : sum = 0 r = 0 while r < rows : sum = sum + d[r][c] d[r][c] = sum r += 1 c += 1 def main(): data = [ ] fp = open("file3-1.txt", "r") for line in fp : data.append( clean(line) ) fp.close() fx( data ) for line in data : print(line) main() """ file3-1.txt 2, 3, 9, 8 7, 4, 6, 2 6, 3, 5, 15 """ 2. What does the following program print? cars = { } fp = open("cars.txt", "r") for line in fp : make, model, cost = line.strip().split(',') if make in cars : totalCost = cars[make][0] + int(cost) totalCount = cars[make][1] + 1 cars[make] = [totalCost, totalCount] print(make, totalCost, totalCount) else : cars[make] = [int(cost), 1] print(make) fp.close() mymax = 0 keys = list(cars.keys()) keys.sort() for k in keys : avg = cars[k][0]/cars[k][1] if avg > mymax : mymax = avg winner = k print("\n{:s} wins with {:9.2f}".format(winner, avg) ) """ content of cars.txt Ford, Mustang, 30000 Honda, Civic, 16000 Honda, Accord, 25000 Chevy, Malibu, 20000 Ford, Taurus, 21000 Ford, Explorer, 25000 Chevy, Tahoe, 30000 """ 3. What does the following program print? def fx( d ) : rows = len( d ) cols = len( d[0] ) i=1 first = d[0][0] second = d[0][0] third = d[0][0] print("{:d}: {:.1f}, {:.1f}, {:.1f}".format(i, first, second, third) ) r=0 while r < rows : c=0 while c < cols : if d[r][c] > first : third = second second = first first = d[r][c] i += 1 print("{:d}: {:.1f}, {:.1f}, {:.1f}".format(i, first, second, third) ) c += 1 r += 1 return first, second, third def main(): d = [ [1.0, 1.5, 2.0], [1.5, 4.0, 2.5], [2.0, 2.5, 3.0], [2.5, 2.0, 1.0] ] f, s, t = fx( d ) print("final: ", f, s, t) main() 4. What does the following program print? d = ["fish cat dog rabbit", "ant horse elephant", "giraffe mouse rhino" ] rows = len( d ) r = 0 while r < rows : counter = 0 ns = "" t = d[r].split() size = len( t ) i = 0 while i < size-1 : if t[i] < t[i+1] : ns += t[i] + " " counter += 1 i += 1 if counter > 1 : d.append(ns) else : d[r] = ns r += 1 for line in d : print(line) 11. Complete the program below to count the number of occurrences of each word in the string text. The counts should be stored in a dictionary and printed after the counting is complete. Don’t hard-code your program to this specific string; you can assume that the string will never have punctuation and will always be lower case. text = "cat dog horse cat bear dog mouse rabbit cat horse" # start here 12. Write a program that can read a file, addresses.txt, that consists of a person’s full name, a comma, a street address, a comma, and the zip code. Assume that each person’s full name is unique. Store this information in a dictionary. Prompt the user for a zip code and then print the names and street addresses of everyone in that zip code. 13. Write a program that can read the file numbers.txt, which consists of two columns of integers. The columns are separated by a comma. The program should prompt the user for a column index and then produce the average of this column. 14. Write a function that when given a 2D list of integers, returns a list consisting of the column sums. Assume that each row of the list passed to the function has the same number of elements. 15. Complete the program below by writing the code to prompt the user for a type of feline and an amount. Assume that the type of feline the user enters is not already in the dictionary. Add the type of feline and its amount. Then print out the types of felines and their amounts for which the amount is greater than 50. d = {"feline" : {"lion" : 22, "tiger" : 88 }, "canine" : {"dog" : 12, "wolf" : 53, "coyote" : 77} } # start here 16. Write a program that can read the file colors.txt, the first few lines of which are red,23 blue,664 green,109 The program should use a dictionary to store the sums of the amounts associated with each color in colors.txt. After processing the file, print each color and its associated sum. 17. Write a function that when given a string of words (separated by spaces), returns the longest word in the string. If more than one word has the maximum number of characters, return the first of these. Assume that the string doesn’t have any punctuation. 18. Write a function that receives an integer, n, and then prints a sideways pyramid of asterisks that is n asterisks in height. Example: if the user enters 4, then the function would print * ** *** **** *** ** * 19. Write a program that reads a comma-delimited file, holidays2011.txt, in which each line represents a 2011 U.S. holiday. A sample of the file is January 1, New Year’s Day January 17, MLK Day . . . November 11, Veterans’s Day November 24, Thanksgiving December 25, Christmas After storing this information, the program should prompt the user for a month and then print the holidays taking place in that month. Don’t hard-code your program to the specific holiday values. 20. Write a program that prompts the user for two integers, lower and upper; we assume that lower < upper. The program will then read a file, integers.txt, which consists of an unknown number of lines, each of which consists of a single integer. The program should produce and print three counts with regards to the integers in integers.txt: (a) number of integers in integers.txt < lower (b) lower <= number of integers in integers.txt <= upper (c) upper < number of integers in integers.txt 21. Write a function that will receive a 2D list of integers. The function should return the count of how many rows of the list have even sums and the count of how many rows have odd sums. 22. Write a function that will receive a 2D array and return the minimax value (i.e., minimum of the maximums) of the columns of the array. That is, find the minimum values of each column and then the maximum of these minimums. The function should return the minimax value. 23. Write a program that can read two files, first.txt and second.txt, and count how many of the lines in first.txt are in second.txt. The program should print the final count. Each file has exactly 1000 lines; each line is unique within that file and consists of at most 49 characters. There is no delimiter. 24. Write a program that can read a comma-delimited file, movies.txt, storing each line in a dictionary. Each line consists of • title – represented by a string of at most 99 characters • category – represented by a string of at most 29 characters • release year – represented by a four-digit integer The title will be the key. After storing the movie information, prompt the user for a string, cat, and then print the titles of the movies whose category matches cat and whose year is an even number.