Created
May 22, 2015 21:11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import * | |
#get the input numbers and returns them as a list | |
def inputget(): | |
#get a string of input, and separate it into numbers | |
inputs = input("Enter numbers separated by spaces: ").split(' ') | |
#create an empty list | |
output_int = [] | |
#iterate through the string, casting each character as an | |
#integer and appending the list each time | |
for letter in inputs: | |
#if it's a digit cast it as a number and append the list | |
if letter.isdigit(): | |
output_int.append(int(letter)) | |
return output_int | |
#sums the values in the list passed in | |
def getlistsum(listin): | |
total = 0 | |
#iterate through list adding the values as they come | |
for number in listin: | |
total += number | |
return total | |
#returns a list of the differences between the items in 'listin' and the 'mean' | |
def getMeanDiff(listin, mean): | |
diffsList = [] | |
for num in listin: | |
diffsList.append(num-mean) | |
return diffsList | |
#squares each item in 'listin' and creates a new list of the result | |
def squareList(listin): | |
sqList = [] | |
for num in listin: | |
sqList.append(pow(num, 2)) | |
return sqList | |
#input numbers | |
numbers = inputget() | |
#amount of them | |
numNums = len(numbers) | |
#sum of the input | |
sumNums = getlistsum(numbers) | |
#mean of the inputs | |
meanNums = float(sumNums/numNums) | |
#the diferences between thi inputs and the mean | |
diffList = getMeanDiff(numbers,meanNums) | |
#the squares of he differences | |
sqList = squareList(diffList) | |
#the sum of the squares | |
sumSq = getlistsum(sqList) | |
#finally the std deviation | |
deviation = pow(sumSq/numNums,0.5) | |
#show result | |
print("Standard Deviation: {0:.4f}".format(deviation)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment