Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

15 ### 1 #Write a function called st dev. st dey should have one 2 #parameter, a

ID: 3708308 • Letter: 1

Question

15 ### 1 #Write a function called st dev. st dey should have one 2 #parameter, a filename. The file will contain one integer on 3 #each line. The function should return the population standard 4 #deviation of those numbers. 5 # 6 #The formula for population standard deviation can be found here: 7 #edge.edx.org/asset-v1:GTx+gt-mooc-staging1+2018_T1+type asset+block@stdev.PNG 8 # 9 #The formula is a bit complex, though, and since this is a 10 #cs class and not a math class, here are the steps you would 11 #take to calculate it manually: 12 # # 1. Find the mean of the list. 14 # 2. For each data point, find the difference between that point and the mean. Square that difference, and add it 16 to a running sum of differences. 17 # 4. Divide the sum of differences by the length of the 18 # list. 19 # 5. Take the square root of the result. 20 # 21 #You may assume for this problem that the file will contain 22 #only integers -- you don't need to worry about invalid 23 #files or lines. The easiest way to take the square root is 24 #to raise it to the 0.5 power (e.g. 2 ** 0.5 will give the 25 #square root of 2). 26 # 27 #HINT: You might find this easier if you load all of the 28 #numbers into a list before trying to calculate the average. 29 #Either way, you're going to need to loop over the numbers 30 #at least twice: once to calculate the mean, once to 31 #calculate the sum of the differences. 32 33 #Add your function here! ## 34 35 36 #Below are some lines of code that will test your function. 37 #You can change the value of the variable(s) to test your 38 #function with different inputs. 39 #If your function works correctly, this will originally 40 #print 27.796382658340438 (or something around there). 41 print (st_dev ("some_numbers.txt"))

Explanation / Answer

def st_dev(filename): lst = [] mean = 0 with open(filename, 'r') as f: for line in f: num = float(line.strip()) mean += num lst.append(num) mean /= len(lst) sd = 0 for num in lst: sd += (num - mean)**2 sd /= len(lst) return sd

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote