CS100: Project 0

Revision Date: January 8, 2017

Printable Version


Preamble

Imitation is the sincerest form of flattery - Aesop

Real Computer Scientists abhor reinventing the wheel. So when given the task of writing a program, they search out and find programs they feel are similar to the one they need to write. They then modify the program, with permission, so that it processes the input and produces the output they desire. They accomplish this feat even if they do not completely understand the original program upon which they are basing their new programs, via analogies and pattern matching.

Your task is to take an existing program that computes NFL passer rating and turn it into a program that computes MLB on-base percentage.

Where to start

The first thing you should do is to setup a directory in which you will work. Run the following commands, one at a time:

    cd
    cd cs100
    mkdir project0
    cd project0

The first two commands move you into your cs100 directory, while the next two create and move into a project directory. Now, retrieve the existing program for computing passer rating with the command:

    wget troll.cs.ua.edu/cs100/python/projects/nfl.py

Run the program, using the command:

    python3 nfl.py

In another terminal window, move into your project directory and run the command:

    vim nfl.py

This will allow you to look at the program while it is running in the other window. Try to figure out what part of the code does what by running the program in the other window over and over.

How to proceed

Now, in the first window, copy the existing program over into a new file named mlb.py with the command:

    cp nfl.py mlb.py

In the second window, quit vim and open up the new file, mlb.py. Now begin modifying mlb.py so that it stops computing NFL passer rating. and starts computing MLB on-base percentage Make one change at a time and test after each change. Don't forget the vim's undo button in case you introduce an error into the code. Once a change is working, make sure you save the file.

You can see the formula for computing MLB on-base percentage at http://en.wikipedia.org/wiki/On-base_percentage and the formula for computing NFL passer ratings at http://en.wikipedia.org/wiki/Passer_rating. Note, the program nfl.py computes an uncapped version of the NFL passer rating, so the formula used in the program is a little simpler than the one shown on Wikipedia.

Compliance Instructions

Create a file named test0 that contains five numbers. The numbers should be in the same order as those shown on the Wikipedia page: hits, walks, hits by pitch, at bats, and sacrifice flies.

Now, make sure that you have implemented your program correctly by running this command:

    cat test0 | python3 mlb.py

This method of running the program is called "piping in the input from a file." When you actually do this, the prompts your program makes for information will all be strung together on a single line. Don't worry about it; it's a natural consequence of the way the program was run.

Your program should print out clearly the results consistent with the five inputs. If your code fails with a runtime error while running this test, then you will receive a zero for this assignment.

Note that your answers do not have to be correct for your program to be graded, only that the program does not crash. Of course, correct answers will yield a much higher grade.

Note also that a prerequisite for receiving any credit is that your new program be based off the original program.

Challenges

Try to get numbers to print like 1.324 instead of 1.3235294117647058. You will need to learn about formatting numbers to do this challenge.

Try to get quantity agreement between numbers and their labels. For example, the mlb program, when displaying the input data, might output information this way:

    ...
    hit by pitches: 1
    at bats: 23
    sacrifice flies: 1

Instead, try outputting it this way instead:

     ...
     1 hit by pitch
    23 at bats
     1 sacrifice fly

Note the use of "pitch" instead of "pitches" and "fly" instead of "flies". You will need to learn about if statements to do this challenge.

Submission Instructions

Change to the project0 directory containing your assignment. Do an ls command. You should see something like this:

    mlb.py   nfl.py  test.dat

Extra files are OK, as long as you are in the correct directory. (Submissions from the wrong directory that include many extra files will be penalized.) You will not be penalized if you submit files unrelated to this project.

Submit your program like this:

    submit cs100 YYY project0

Remember to replace YYY with your instructor name. Note that project0 ends in a zero, not a capital Oh.

lusth@cs.ua.edu