Problem 1: Split CSV string

10 points

We have seen comma-separated value (csv) strings in class, and we have parsed them to get the indidual values. For this problem, you will write a function to parse a csv string directly. For example: split_csv('abc,def,ghi') produces the list: ['abc', 'def', 'ghi']

You might think, that's easy -- I'll use the split() method on the string.

But, we also want to allow commas in our values, as well, so we can't use that method. What we will do is this:

Here is an example. Suppose we have the following four values:

abc
def
ghi,jkl
mno
    

In a string, they would be: 'abc,def,"ghi,jkl",mno'

split_csv('abc,def,"ghi,jkl",mno') produces the list:

['abc', 'def', 'ghi,jkl', 'mno']

Note that if you find a value within double quotes, you also have to remove the double quotes from the value. There will not be additional quotes inside the quoted strings (in other words, none of the values themselves will have quotes -- that would require more work!)

Hint: You should probably go through the string one character at a time, and keep track of the important information, such as whether you have seen a double-quote yet, or not. Also start building a string one character at a time, until you find a comma that ends the value, at which point you can add the value to your list.

def split_csv(s):
    """
    Splits a comma-separated-value string into a list of values. If a value
    itself has a comma, the entire value is enclosed in double-quotes.
    :param s: An input string in the form of comma separated values,
    with double-quotes around values that contain commas.
    (e.g., 'abc123,def456,"mno,pqr",stu789')
    :return: a list of the values
    >>> split_csv('abc,def,ghi')
    ['abc', 'def', 'ghi']

    >>> split_csv('"a,b","c,d","e,f"')
    ['a,b', 'c,d', 'e,f']

    >>> split_csv('abc123,def456,"mno,pqr",stu789')
    ['abc123', 'def456', 'mno,pqr', 'stu789']
    """
    split_list = []
    partial_str = ''
    # TODO: Your code here
    

Mean and Standard Deviation

10 points

For this problem, you will write two functions: one to calculate the mean value in a list, and the other to calculate the standard deviation of a list. Your standard deviation function should call your mean function.

The mean value of a list is defined as the sum of the values divided by the number of values in the list.

The standard deviation of a list is defined mathematically as follows:

In regular English, this is how to calculate the standard deviation of a population (in our case, a list):

  1. Calculate the mean of the list values.
  2. Calculate sum of the square of each value in the list minus the mean (see below for an example).
  3. Divide the resulting sum by the number of values in the list.
  4. Take the square root of the result.

If the values in a list were [5, 12, 18, 3, 9], this is how the standard deviation would be calculated:

  1. Calculate the mean of the list values.

    (5 + 12 + 18 + 3 + 9) / 5 = 9.4

  2. Calculate sum of the square of each value in the list minus the mean.

    (5 - 9.4)**2 + (12 - 9.4)**2 + (18 - 9.4)**2 + (3 - 9.4)**2 + (9 - 9.4)**2 = 141.2

  3. Divide the resulting sum by the number of values in the list.

    141.2 / 5 = 28.24

  4. Take the square root of the result.

    sqrt(28.24) = 5.31

Problem 3: Nested Structures

10 points

Write a function that has a user type in one number at a time. After each number typed, print the mean and standard deviation of the entire list of numbers so far. When the user enters a blank line, return a dictionary with the following keys / value pairs:

Assume that you have correct versions of the two functions you wrote for problem (2) (E.g., do not rewrite those functions).

Here is an example program, and its output:

def main():
    result = statistics() # the function you will write
    print(f"numbers list: {result['numbers_list']}")
    print(f"mean: {result['mean']}")
    print(f"stdev: {result['stdev']}")

if __name__ == "__main__":
    main()
    

Output example:

Please enter a number: 5
mean: 5.0
standard deviation: 0.0
Please enter a number: 12
mean: 8.5
standard deviation: 3.5
Please enter a number: 18
mean: 11.666666666666666
standard deviation: 5.312459150169743
Please enter a number: 3
mean: 9.5
standard deviation: 5.937171043518958
Please enter a number: 9
mean: 9.4
standard deviation: 5.314132102234569
Please enter a number:
numbers list: [5.0, 12.0, 18.0, 3.0, 9.0]
mean: 9.4
stdev: 5.314132102234569
    

Problem 4: Classes

10 points

For this problem, write a Dice class that represents a single n-sided die. We have pre-written the function headers for the functions you will need to write. Here are the details:

Here is an example program and its output:

from dice import Dice

def main():
    my_dice = []
    for i in range(5):
        my_dice.append(Dice(i + 6))

    for i in range(10):
        for die in my_dice:
            print(f"{die.num_sides} sides: {die.roll()}, ", end='')
        print()
   
    print()
    for die in my_dice:
        print(f"{die.num_sides} sides, all rolls: {die.all_rolls()}")

if __name__ == "__main__":
    main()
    

Output:

6 sides: 1, 7 sides: 6, 8 sides: 2, 9 sides: 4, 10 sides: 1,
6 sides: 2, 7 sides: 2, 8 sides: 8, 9 sides: 9, 10 sides: 5,
6 sides: 4, 7 sides: 1, 8 sides: 3, 9 sides: 3, 10 sides: 6,
6 sides: 6, 7 sides: 3, 8 sides: 2, 9 sides: 5, 10 sides: 10,
6 sides: 5, 7 sides: 2, 8 sides: 5, 9 sides: 1, 10 sides: 5,
6 sides: 1, 7 sides: 6, 8 sides: 3, 9 sides: 8, 10 sides: 8,
6 sides: 2, 7 sides: 3, 8 sides: 8, 9 sides: 2, 10 sides: 3,
6 sides: 5, 7 sides: 2, 8 sides: 1, 9 sides: 5, 10 sides: 8,
6 sides: 2, 7 sides: 1, 8 sides: 6, 9 sides: 7, 10 sides: 5,
6 sides: 2, 7 sides: 2, 8 sides: 5, 9 sides: 2, 10 sides: 4,

6 sides, all rolls: [1, 2, 4, 6, 5, 1, 2, 5, 2, 2]
7 sides, all rolls: [6, 2, 1, 3, 2, 6, 3, 2, 1, 2]
8 sides, all rolls: [2, 8, 3, 2, 5, 3, 8, 1, 6, 5]
9 sides, all rolls: [4, 9, 3, 5, 1, 8, 2, 5, 7, 2]
10 sides, all rolls: [1, 5, 6, 10, 5, 8, 3, 8, 5, 4]