user3102945
user3102945

Reputation: 1

Write a program that computes mean, medium, maximum, and standard derivation

Been trying to solve this for the past few hours and I cannot find a solution! Essentially we have to write a program that opens a file, takes the numbers from it, and calculates the aforementioned things (mean, min, max, standard derivation). Below is what I currently have:

def get_numbers():

 # Open a file for reading.
infile = open('inNumbers.txt', 'r')

# Read the numbers from the file.
line = infile.readline()
while line != '':
    print(line)
    line = infile.readline()
infile.close()


def mean(nums):
    sum = 0.0
    for num in nums:
        sum = sum + num
    return sum / len(nums)


def stdDev(nums, xbar):
    sumDevSq = 0.0
    for num in nums:
        dev = xbar - num
        sumDevSq = sumDevSq + dev * dev
    return sqrt(sumDevSq/(len(nums)-1))


def min():
    showFile = open("inNumbers.txt", 'r')
    lowest = None
    for line in showFile:
        tokens = line.split(',')
        value = min(tokens[:2])
        if lowest == None:
            lowest = value
        if value < lowest:
                lowest = value


def main():
    print("This program computes mean, maximum, minimum and standard deviation.")

    data = get_numbers()
    xbar = mean(data)
    std = stdDev(data, xbar)

    print("\nThe mean is", xbar)
    print("The standard deviation is", std)
    print("The minimum is", value)

main()

Upvotes: 0

Views: 2024

Answers (2)

skamsie
skamsie

Reputation: 2726

It's very important to know how the file containing the numbers looks like. But I will assume from your example code that there is one float on each line. It would be easier if you had numpy already installed. I guess it is by default on Mac. So step by step:

import numpy

#define a function that will just open your file containing numbers and get 
#all data into a list of floats
def getNumbers(file):
  numbers = []
  with open(file, 'r') as numbers_file:
    for line in numbers_file.readlines():
      numbers.append(float(line.strip('\n').strip()))
  return numbers


#get data from 'numbers.txt' into a list named 'num_list'
num_list = getNumbers('numbers.txt')

#print what you need
print 'Maximum is %g' %max(num_list)
print 'Minimum is %g' %min(num_list)
print 'Average is %g' %numpy.mean(num_list)
print 'Standard deviation is %g' %numpy.std(num_list)

#if you do not have numpy
my_avg = sum(num_list)/len(num_list)
print 'Average is %g' %my_avg
#you need to do some reading for the std-dev formula :)

Upvotes: 0

Hyperboreus
Hyperboreus

Reputation: 32429

As I pointed out, you should first read some basic primers on programming before trying to solve your current problem, because otherwise any answer won't help you and will just create a new disciple of the cargo-cult.

Nevertheless, here goes your code (although I am not sure about the formula for the std-dev):

#open file
with open('inNumbers.txt', 'r') as f:
    #read lines, strip trailing newlines, and convert to float if not empty
    numbers = [float(x) for x in (x.strip() for x in f) if x]

avg = sum(numbers) / len(numbers)
sdv = (sum((n - avg) ** 2 for n in numbers) / len(numbers)) ** .5
print('The arithmetic mean is {}'.format(avg))
print('The standard deviation is {}'.format(sdv))
print('The minimum is {}.'.format(min(numbers)))

Maybe you can take something useful out of it.

Upvotes: 1

Related Questions