# Spencer Smith
# macid: smiths
# student number: #######
# February 06, 2011
#
# This is a solution for Lab 06: Major Assignment 2.

import string

def main():

    print """\

This program is a solution to Lab 06: Major Assignment 2.
"""

    # Read input files into lists of strings
    inputFileName = raw_input("Enter the name of the input file of x values: ")
    Fx = open(inputFileName, "r")
    x = string.split(Fx.read())
    Fx.close()

    inputFileName = raw_input("Enter the name of the input file of y values: ")
    Fy = open(inputFileName, "r")
    y = string.split(Fy.read())
    Fy.close()

    # Compute input list of floats and the slope
    sxy = 0.0
    sx = 0.0
    sy = 0.0
    sxsq = 0.0
    n = len(x)
    for i in range(n):
        # Convert i-th strings to floats
        x[i] = float(x[i])
        y[i] = float(y[i])
        # accumulators
        sxy = sxy + x[i]*y[i]
        sx = sx + x[i]
        sy = sy + y[i]
        sxsq = sxsq + x[i]**2

    m = (n*sxy - sx*sy)/(n*sxsq - sx**2)

    # Print output
    print ""
    print "The input list Lx of floats is=", x
    print ""
    print "The input list Ly of floats is=", y
    print ""
    print "The slope of the best fit line is=", m
    print ""

main()

# Design Question
#
# There are four different summations used to calculate
# m.  There is a summations over xi*yi, xi, yi, and xi**2.
# All of these summations can be calculated with one loop.
#
# Test Plan
#

# Plan
# Test: 1
# Input:    Lx = [0.0, 1.0, 2.0, 3.0, 4.0]
#           Ly = [0.0, -2.0, -4.0, -6.0, -8.0]
# Expected Output: -2.0
#
# Test: 2
# Input:    Lx = [0, 1, 2, 3, 4]
#           Ly = [0, 1, 2, 3, 4]
# Expected Output: 1.0

# Test: 3
# Input:    Lx = []
#           Ly = []
# Expected Output: Error - division by zero
#
# Test: 4
# Input: Invalid File Name   
# Expected Output: IOError

