CS 100  Programming I
Project 0

Revision Date: January 8, 2017

Printable Version


Preamble

You may develop your code anywhere, but you must ensure it runs correctly under a Linux distribution before submission.

Statistics!

There are lies, damned lies, and statistics - Benjamin Disraeli

Your task is to prompt the user of your program to enter five distinct numbers and report the following statistics about those five numbers:

At this point, you are to ask the user of your program if he or she wishes to play a game. If the user answers in the affirmative, ask the user to write down five random (or not) digits down on a piece of paper. Then ask the user to write down the digits in a different order and to subtract the smaller number from the larger number. Tell the user to circle one of the digits in the result, but not to circle a zero, since a zero is already a circle (this trick depends on the user not selecting a zero). Now ask the user to enter the remaining digits in the answer, in any order. Inform the user that he or she made your task too easy and announce the circled number.

To calculate the circled number, simply add up the entered digits and determine what number added to the sum would make the sum divisible by nine. That additional number is the circled number.

Program Organization

Create a proj0 directory off of your cs100 directory and move into proj0.

Name your main python file statistics.py. Provide two more files named stats.py and game.py, respectively. The statistics.py file should look something like this:

    from stats import *
    from game import *

    def main():
        print('Welcome to the Statistician Magician!')

        a,b,c,d,e = getNumbers()

        print('the mean is ',mean(a,b,c,d,e))
        print('the median is ',median(a,b,c,d,e))
        # ...
        # ...

        answer = input('Say, would you like to play a game? ')

        if isAffirmative(answer):
            game()

        print('Have a nice day!')

    main()

The file stats.py should contain your mean, median, etc. functions. The file game.py should have your game function and other supporting functions.

For example, here is the mean function:

    def mean(a,b,c,d,e):
        return (a + b + c + d + e) / 5

which should be placed in your stats.py file. You are free to make up your own greeting and dialog.

Getting the numbers

Here is how to get a number and make the variable m point to it:

    m = eval(input("Give me a number: "))

To have a function return multiple values, simply separate the values by commas:

    return m,n,o,p,q

Finding the Median

The median value of a collection of distinct values is that value for which the number of items in the collection less than the value are equal to the number of items greater than that value.

A simple way to find the median value of five numbers is to collect those values into something known as a list, sort the list, and then pull out and return the middle value.

For example, to put two values a and b into a list named items, one would issue the command:

    items = [a,b]

To sort the items, pointing the variable sItems to the sorted list, one would issue the command:

    sItems = sorted(items);

To pull out the first and second values in the list pointed to by sItems, one would use bracket notation:

    first = sItems[0]
    second = sItems[1]

Your task is to define a function that takes five arguments, place the arguments into a list, sorts the list, and return the middle value of the sorted list.

Finding the Standard Deviation

To calculate the standard, you will need to total up a series of calculations. Here's how to total up the first five squares:

total = 12 + 22 + 32 + 42 + 52

In python, this could be written as:

    def squareSum():
        total = 0
        total = total + pow(1,2)
        total = total + pow(2,2)
        total = total + pow(3,2)
        total = total + pow(4,2)
        total = total + pow(5,2)
        return total 

You should model your standard deviation-calculating function after squareSum.

Even and odd numbers

Here is a function that tells you whether a given number is odd. It return True if the number was odd and False otherwise.

    def isOdd(x):
        return x % 2 != 0

For this project, only integers can be even or odd. Real numbers are neither.

Integers versus Reals

Here is a function that tells you whether a given number is an integer or not:

    def isInteger(x):
        return type(x) == type(0)

A Little More Math

To find out what to add to a number x to make it divisible by nine, use the modulus operator:

    remainder = x % 9

Now take the remainder and subtract it from 9. That gives you what you need to add to x to make it divisible by nine. Here's an example. Suppose x is 22. Then x % 9 is 4, since 9 goes into 22 evenly twice with a remainder of 4. Subtracting 4 from 9 leaves 5. So 22 + 5 should be divisible by 9, which it is.

Compliance Instructions

To make sure that you have implemented your program correctly, save this file: test0 in your project directory. You should then be able to run the following command:

    cat test0 | python3 statistics.py

and see output similar to the following:

    Welcome to the Statistician Magician!

    Enter the first number:
    Enter the second number:
    Enter the third number:
    Enter the fourth number:
    Enter the fifth number:

    The mean is 3.0
    The median is 3
    The std dev is 1.41421356237
    The number of integers is 5
    The number of reals is 0
    The number of even numbers is 2
    The number of odd numbers is 3
    The number of positive numbers is 5
    The number of negative numbers is 0
    Say, would you like to play a game?
    Enter the first digit:
    Enter the second digit:
    Enter the third digit:
    Enter the fourth digit:
    Too easy! The missing digit is 4
    Have a nice day!

When you actually do this, the prompts will all be strung together on a single line, like this:

    Enter the first number: Enter the second number: Enter the ...

Don't worry about it; it's a natural consequence of the way you ran the program. DO NOT OUTPUT BLANK LINES IN YOUR PROGRAM TO MAKE THIS TEST LOOK BETTER.

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 it not fail.

Submission Instructions

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

    game.py   statistics.py   stats.py   test0

Then submit your program like this:

    submit cs100 xxxxx project0

Remember to replace xxxxx with your instructor name.

Note that project0 ends in a zero, not a capital O.

lusth@cs.ua.edu