CSCI201, Test #3Name_____________________________



CSCI201, Test #3Name_____________________________Arithmetic (6 points)Write a function called mealTotal that will accept two numbers (1) the cost of a meal (float), and (2) the tax rate (a float). The function should compute the total cost of the meal item including tax and a tip. Compute the meal total by adding on the tax (computed by multiplying the meal cost by the tax rate) and the tip (computed by multiplying the meal cost by 0.15). Example: meal cost is $50, tax rate is 5.00%. The tax is 50*0.05 = 2.50. The tip is 50*0.15 = 7.50. Return 50+2.50 +7.50= 60.0. Do not compute the tip based on the meal cost plus the tax; compute it only on the meal cost.Example calls:meal = mealTotal(50.00, 0.05)print(f"Should be $60.00: ${meal:10.2f}")meal = mealTotal(45.00, 0.07)print(f"Should be $54.90: ${meal:10.2f}")2. Selection (10 points)Write a function called discountRate which will accept one argument representing a quantity purchased (an int). The function will return the discount rate, computed using the following table:QuantityDiscount Rate<100.00<1000.10<10000.201000+0.30Example calls:print(f"Should be 0.00: {discountRate(9):10.2f}")print(f"Should be 0.10: {discountRate(10):10.2f}")print(f"Should be 0.10: {discountRate(99):10.2f}")print(f"Should be 0.20: {discountRate(100):10.2f}")print(f"Should be 0.20: {discountRate(999):10.2f}")print(f"Should be 0.30: {discountRate(1000):10.2f}")Repetition (14 points)Write a function called sumPowers that will accept 2 integers. If the first argument is less than or equal to 0, return the value 0. If the second number is less than the first number, return the value 0. If neither of those conditions is true, step through the numbers from the first argument to the last argument (including both) and compute the total of the squares of the even numbers, and the cubes of the odd numbers. Return this total.Example: Arguments are -10 and 10: return 0. Arguments are 5 and 4: return 0. Arguments are 5 and 5: return 125 (5*5*5). Arguments are 1 and 5: return 1*1*1 + 2*2 + 3*3*3 + 4*4 + 5*5*5 = 173.Example calls:print("Should be 0:", sumPowers(-10, 10))print("Should be 0:", sumPowers(5, 4))print("Should be 125:", sumPowers(5,5))print("Should be 173:", sumPowers(1,5))Repetition (10 points)Write a function called fixList that will accept a single list of integers. The function should return a new list (do not change the existing list!) whose elements have all been replaced by the last item on the list. Example: If the list passed in is: [1, 2, 3, 4] you should return the list [4, 4, 4, 4] because 4 is the last item on the list. If the list passed in is [1, 9, 3, 4, 3, 2] you should return [2, 2, 2, 2, 2, 2] because 2 is the last item on the list.Example calls:list1 = [1, 2, 3, 4]print("Should print [4, 4, 4, 4]: ", fixList(list1))print("Original list should still be [1, 2, 3, 4]: ", list1)list1 = [1, 9, 3, 4, 3, 2]print("Should print [2, 2, 2, 2, 2, 2]: ", fixList(list1))print("Original list should still be [1, 9, 3, 4, 3, 2]: ", list1)Lists (11 points)Write a function called inRange that will (1) accept a list of integers (2) a lower bound (integer), and (3) an upper bound (integer) and do the following: return a new list (do not change the original list!) whose elements are only the elements between the lower and upper bound, including both.Example calls:list3 = [1, 2, 3, 4]print("Should print [3, 4]: ", inRange(list3, 3, 4))print("Original list should still be [1, 2, 3, 4]: ", list3)list4 = [1, 9, 3, 4, 3, 2]print("Should print [1, 3, 4, 3, 2]: ", inRange(list4, 0, 5))print("Original list should still be [1, 9, 3, 4, 3, 2]: ", list4)Dictionaries and Files (14 points)For this last question, you do not have to write a function. Open a file called phone.txt. Each line has the following format: contact first name followed by a blank, followed by the contact's phone number. You do not know how many lines are in the file. Step through the file, one line at a time and create a dictionary where the key is the name of the contact (a string) and the value is the phone number (a string).After reading the entire file into a dictionary, enter a loop that will continue to ask the user for a contact's name until the user enters an empty string (presses Enter). Read the name and, if the name is in the dictionary, display the phone number for that name. If the name is not in the dictionary, display a "Contact not found." error message. You do not have to worry about the case of the text. You do not have to worry about leading or trailing blanks in the name (although this is trivial to fix). If the case of the name requested is not an exact match for the dictionary key, reply that the name is not in the dictionary (see first example below).The file looks like this:Bob 712-555-1111Joe 712-555-2222Tim 712-555-3333Mary 712-555-4444Karen 712-555-5555Jim 712-555-6666Charlie 712-555-7777Example execution:Enter a name: bob#"Bob" is in, "bob" is notbob is not in the dictionary.Enter a name: KarenPhone number is: 712-555-5555Enter a name:End of program ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download