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
'abc,def,"ghi,jkl",mno'
['abc', 'def', 'ghi,jkl', 'mno']split_csv('abc,def,"ghi,jkl",mno') produces the list:
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
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):
If the values in a list were [5, 12, 18, 3, 9], this is how the standard deviation would be calculated:
(5 + 12 + 18 + 3 + 9) / 5 = 9.4
(5 - 9.4)**2 + (12 - 9.4)**2 + (18 - 9.4)**2 + (3 - 9.4)**2 + (9 - 9.4)**2 = 141.2
141.2 / 5 = 28.24
sqrt(28.24) = 5.31
numbers_list / value: A list of all the numbers typedmean / value: the mean of the liststdev / value: the standard deviation of the listAssume 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
roll function returns a random roll of the die.num_sides function returns the number of sides on the die.all_rolls function returns a list of all the rolls of the die since the creation of the object.
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]