Docsity

CS 1301 Fall 2007 Homework 8: Sorting, Profiling, and Baseball, Assignments of Computer Science

Toggle sidebar
bg1
Homework 8 CS 1301 – Fall 2007 Page 1/7
CS 1301 – Fall 2007
Homework 8 – Python is a Sporting Sorter
Due: Friday, November 2, 2007 at 6 PM EST
Files to submit: hw8.py
Functions to include in hw8.py:
1. Profile calls and questions
2. Sorting Questions
3. staggeredAverage()
4. getWeather()
5. homeRunsInput()
6. homeRunsSort()
7. getPosition()
8. getPlayer()
For Help:
Newsgroups – see course syllabus for more detailed information
TA Helpdesk – schedule is posted on class website
Email TAs
Notes:
Think through the logic of each of the problems before you start coding anything
Don’t forget to include the required comments and collaboration statement (as
outlined on the course syllabus)
Do not wait until the last minute to do this assignment in case you run into
problems
If you find an error in the homework assignment, let a TA know immediately
Part 1 – Questions on Sorting
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.
1) Which sort compares two items, swaps them if they are in the wrong order, and
continues to compare two items until no more swaps are needed and the array is
in the correct order?
2) Which sort creates a pivot variable and splits the array into two different
arrays—one with all numbers less than the pivot and the other with all numbers
greater than the pivot?
3) Briefly explain the selectionSort algorithm.
4) Using big O notation, how fast is the quickSort algorithm?
5) Using big O notation, how fast is the selectionSort algorithm?
pf3
pf4
pf5

Partial preview of the text

Download CS 1301 Fall 2007 Homework 8: Sorting, Profiling, and Baseball and more Assignments Computer Science in PDF only on Docsity!

CS 1301 – Fall 2007

Homework 8 – Python is a Sporting Sorter

Due: Friday, November 2, 2007 at 6 PM EST

Files to submit: hw8.py Functions to include in hw8.py:

**1. Profile calls and questions

  1. Sorting Questions
  2. staggeredAverage()
  3. getWeather()
  4. homeRunsInput()
  5. homeRunsSort()
  6. getPosition()
  7. getPlayer()** For Help:  Newsgroups – see course syllabus for more detailed information  TA Helpdesk – schedule is posted on class website  Email TAs Notes:  Think through the logic of each of the problems before you start coding anything  Don’t forget to include the required comments and collaboration statement (as outlined on the course syllabus)  Do not wait until the last minute to do this assignment in case you run into problems  If you find an error in the homework assignment, let a TA know immediately

Part 1 – Questions on Sorting

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.

  1. Which sort compares two items, swaps them if they are in the wrong order, and continues to compare two items until no more swaps are needed and the array is in the correct order?
  2. Which sort creates a pivot variable and splits the array into two different arrays—one with all numbers less than the pivot and the other with all numbers greater than the pivot?
  3. Briefly explain the selectionSort algorithm.
  4. Using big O notation, how fast is the quickSort algorithm?
  5. Using big O notation, how fast is the selectionSort algorithm?

Part 2 – Profile Calls and Questions

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:

  1. import profile at the top of your file with any other import statements
  2. profile.run(“function(parameters)”) Example: You have a function bisweptual(port, starboard) that does something (it doesn’t matter what for this example). To see what the running time of bisweptual is, define port and starboard and then call profile.run like so: port = 4 starboard = 8 profile.run(“bisweptual(port, starboard)”) It should output something like this to the console: 4 function calls in 0.003 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.003 0.003 0.003 0.003 :0(setprofile) 1 0.000 0.000 0.000 0.000 :1(bisweptual) 1 0.000 0.000 0.000 0.000 :1() 1 0.000 0.000 0.003 0.003 profile:0(bisweptual(port, starboard)) 0 0.000 0.000 profile:0(profiler) Define two test arrays in your file like so: testarr1 = [4, 7, 2, 8, 3, 0, -1, 5, -15]

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.)

Part 5 – The World Series

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):

since the total gross is located at index 2, return that element

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.

Final thoughts

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