95.0k views
3 votes
Implement the function get_stats The function get_stats calculates statistics on a sample of values. It does the following: its input parameter is a csv string a csv (comma separated values) string contains a list of numbers (the sample) return a tuple that has 3 values: (n, stdev, mean) tuple[0] is the number of items in the sample tuple[1] is the standard deviation of the sample tuple[2] is the mean of the sample

User Sonu Jha
by
5.5k points

1 Answer

0 votes

Answer:

Check the explanation

Step-by-step explanation:

PYTHON CODE: (lesson.py)

import statistics

# function to find standard deviation, mean

def get_stats(csv_string):

# calling the function to clean the data

sample=clean(csv_string)

# finding deviation, mean

std_dev=statistics.stdev(sample)

mean=statistics.mean(sample)

# counting the element in sample

count=len(sample)

# return the results in a tuple

return (count, std_dev, mean)

# function to clean the csv string

def clean(csv_string):

# temporary list

t=[]

# splitting the string by comma

data=csv_string.split(',')

# looping item in data list

for item in data:

# removing space,single quote, double quote

item=item.strip()

item= item.replace("'",'')

item= item.replace('"','')

# if the item is valid

if item:

# converting into float

try:

t.append(float(item))

except:

continue

# returning the list of float

return t

if __name__=='__main__':

csv = "a, 1, '-2', 2.35, None,, 4, True"

print(clean(csv))

print(get_stats('1.0,2.0,3.0'))

User Anat
by
5.1k points