# Ryan Martin
# macid: abcd123
# student number: #######
# July 12, 2011
#
# This is a solution for Marked Assignment 2 (Lab 06).

def main():

    print """\

This program is a solution to Marked Assignment 2 (Lab 06).

"""

    #INPUTS
    
    FList = raw_input("Enter the name of three files containing words, separated by spaces: ")
    FList = FList.split()

    searchWord = raw_input("Enter the word to search for in each file: ")
    print
   
    #COMPUTATION
   
    inputFile1 = open(FList[0], "r")
    wordList1 = inputFile1.read().split()
    inputFile1.close()
    inputFile2 = open(FList[1], "r")
    wordList2 = inputFile2.read().split()
    inputFile2.close()
    inputFile3 = open(FList[2], "r")
    wordList3 = inputFile3.read().split()
    inputFile3.close()
    
    file1Results = wordList1.count(searchWord)
    file2Results = wordList2.count(searchWord)
    file3Results = wordList3.count(searchWord)
    
    '''Alternative method (one of many):
    
    wordLists = []
    for inputFileName in FList:
        inputFile = open(inputFileName, "r")
        wordList.append(inputFile.read().split())
        inputFile.close()
    
    file1Results = wordLists[0].count(searchWord)
    file2Results = wordLists[1].count(searchWord)
    file3Results = wordLists[2].count(searchWord)

    '''

    #OUTPUT
    
    print "Matches in", FList[0], "=", file1Results
    print "Matches in", FList[1], "=", file2Results
    print "Matches in", FList[2], "=", file3Results
    print ""

main()

'''
Design Question

Adapting this program to handle an unknown number of files
is just a matter of storing results in a list instead.
Using a loop to iterate through each given filename,
a variable number of files can then be processed:

results = []
for inputFileName in FList:
    inputFile = open(inputFileName, "r")
    wordList = inputFile.read().split()
    inputFile.close()
    matches = wordList.count(searchWord)
    results.append(matches)
    print "Matches in", inputFileName, "=", matches

Test Plan

Contents F1.txt
The
Football
End
Friendly
Harvest
cos
the
Bill
		
Contents F2.txt
Math
The
Trig
Sin
Cos

Contents F3.txt
the
capital
pop
origami 
rain
speaker


Plan

Test: 1
Input: [F1.txt F2.txt F3.txt, the]
Expected Output: [1 0 1]


Test: 2
Input: [F1.txt F2.txt F3.txt, The]
Expected Output: [1 1 0]


Test: 3
Input: [Invalid File Name, The]
Expected Output: [IOError]


Test: 4
Input: [F1.txt F2.txt F3.txt, ""]
Expected Output: [0 0 0]

'''
