



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Files to submit: hw8.py Functions to include in hw8.py:
**1. Profile calls and questions
First, to test your understanding of sorting, answer the following questions. Comment out your answers in your homework file as you have done on past assignments.
Here is the code for bubble sort, taken from the class calendar: def bubblesort(array): for i in range( len(array) ): for j in range( len(array) - 1 - i ): if array[j] > array[j+1]: temp = array[j] array[j] = array[j + 1] array[j + 1] = temp Go ahead and read through the code so that you understand what it is doing. Copy this function into your homework file. We will be comparing its efficiency to the built in sort in Python. Let’s find out which one is faster! We will be using profile to tell us the running time of the two sorts. How to use profile:
http://www.weather.com/outlook/recreation/outdoors/local/30332?lswe=30332&lwsa=W eather36HourOutdoorsCommand&from=whatwhere Write a function getWeather() that takes in no parameters and prints out what the current temperature and conditions for Georgia Tech are. Here is an example: >>> getWeather() The temperature outside is 69 and looking quite clear_sunny (You won’t actually get “Sunny” to appear as it is not on the page.)
In honor of the 2008 World Series, we’ll write a few functions dealing with baseball. Part 5A – Make a roster! Write a function called homeRunsInput() that takes no parameters and asks the user for a player’s name. Once a name is entered, it should then ask the user for the number of home runs that player has hit. It should store both in an array. Be sure that the name is stored as a string and the number of home runs as an integer. Next, ask the user if he or she would like to enter another player; if they enter “y” or “Y”, ask them again for a name and home runs. Once the user enters anything other than “y” or “Y”, return a single array with each smaller array as its entries, so that you are returning an n x 2 matrix where n is the number of players entered by the user. Sample Output: >>>homeRunsInput() Please enter the player’s name: Derrek Lee Please enter the number of home runs this player has: 32 Would you like to enter another? Y Please enter the player’s name: Alfonso Soriano
Please enter the number of home runs this player has: 36 Would you like to enter another? N [[“Derrek Lee”, 32], [“Alfonso Soriano”, 36]] Part 5B – Sort the roster! Next, write a function called homeRunsSort() that takes in one parameter, an array of names and home runs – that is, like the output of the previous function you wrote – and sorts it according to the number of home runs each player has from largest to smallest. Once the array is sorted, print out on a new line each name and number of home runs with a tab between the name and home runs. Use any sort algorithm you would like. Sample output: >>>homeRunsSort([[“Derrek Lee”, 32], [“Alfonso Soriano”, 36], [“Aramis Ramirez”, 34], [“Sean Marshall”, 1]]) Alfonso Soriano 36 Aramis Ramirez 34 Derrek Lee 32 Sean Marshall 1 Note: Don’t worry about the home runs being lined up. Just make sure there is a tab between the name and home runs. Part 5C – Sorting with Python’s built in tricks! Write a function homeRunKey that takes in a list x and returns the element we want to sort by. Example: Say you have an array of movies that has the title, weekly gross and total gross of the film. topMovies = [["TMNT", "25", "25"], ["300", "20", "162"], ["Shooter", "14", "14"]] You want to put these movies in order of highest total gross to lowest so you create a key function to help you along. def totalGrossKey(x):
return int(x[2]) >>> topMovies.sort(key = totalGrossKey) >>> print topMovies [['Shooter', '14', '14'], ['TMNT', '25', '25'], ['300', '20', '162']] But these are sorted in ascending order—how can we make it in descending order? Recall that the built in sort can also take in a “reverse” parameter.
Be sure to name your file and functions exactly as indicated. READ THROUGH THE INSTRUCTIONS CAREFULLY. If you need help, be sure to come to office hours. Good Luck! Written by Kyle Tait and Beth York, Fall 2007