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

import string

def main():

    print """\

This program is a solution to lab 06: Marked 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()

    n = len(x)

    # Convert strings to floats
    for i in range(n):
        x[i] = float(x[i])
        y[i] = float(y[i])
        
    # Compute means
    xbar = 0.0
    ybar = 0.0
    for i in range(n):
        xbar = xbar + x[i]
        ybar = ybar + y[i]
    xbar = xbar/n
    ybar = ybar/n

    # Compute intercept
    sxy = 0.0
    sxsq = 0.0
    for i in range(n):
        sxy = sxy + (x[i]-xbar)*(y[i]-ybar)
        sxsq = sxsq + (x[i]-xbar)**2
    b1 = sxy/sxsq
    b0 = ybar - b1*xbar
    
    # 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 value of b0 (y intercept) is= ", b0
    print ""

main()

# Design Question
# 
# No, one cannot solve for b1 with one loop.  The
# formula to calculate b1 which involves a loop for
# summations, depends on xbar and ybar and the
# calculation of these values requires a separate pass
# through the data values.
#
# Testing Question
# 
# 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: 0.0

#
# Test: 2
# Input:    Lx = [0.0, 1.0, 2.0, 3.0, 4.0]
#           Ly = [1.0, 1.1, 1.2, 1.3, 1.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
