Problem 1


In [ ]:
Explain the

In [ ]:
from Tkinter import *     #Import Radiobutton package
from __future__ import division #Import division that makes sense. This took far too long to solve

# Initialize counters
games = [0]
wins = [0]
losses = [0]
ties = [0]
r = 0
p = 0
s = 0
#Dictionary for comp_move to convert to points
comp_convert = {"rock": 1 , "paper": 2 ,"scissors": 3}
num_convert = {1:"rock",2:"paper",3:"scissors",4:"quit"}

In [ ]:
#Update counts for computer prediction and record keeping
def printstatus():
    """This function prints the counts for all user moves and wins,ties and losses when called"""
    
    print "Games: " + str(games[0]) 
    print "Wins: " + str(wins[0])
    print "Losses: " + str(losses[0])
    print "Ties: " + str(ties[0])
    print "Rocks: " + str(r)
    print "Paper: " + str(p)
    print "Scissors: " + str(s)

In [ ]:
#Computer makes random first move, and uses intelligence after first move
    def computer_moves(i):
        """Computer determines move using past user moves"""
        import random
        x = random.random() 
        if i == 1:  
            if x <= (1/3): 
                comp_move = "rock"
            elif (1/3) < x <= (2/3):
                comp_move = "paper"
            else:
                comp_move = "scissors"
                
        else:
            intel= [r,p,s]
            moves=["rock","paper","scissors"]
            countermove=["paper","scissors","rock"]
            
            if (intel[0] == intel[1] > intel[2]):
                temp = random.randint(0,1)             #2 way tie
                temp2 = countermove[temp]
                comp_move = temp2                      #2 way tie 
                #print "0=1"           
            elif (intel[0] == intel[2] > intel[1]):
                temp = random.randint(0,1)*2
                temp2 = countermove[temp]
                comp_move = temp2
                #print "0=2"                              #2 way tie
            elif (intel[1] == intel[2] > intel[0]):
                temp = random.randint(1,2)
                temp2 = countermove[temp]
                comp_move = temp2
                #print "1=2"
            elif (intel[1] == intel[2] == intel[0]):     #3-way tie
                comp_move = countermove[random.randint(0,2)]
                #print "3way"
            else:
                temp = max(intel[:])                   #no tie, pick randomly
                temp2 = intel.index(temp)
                comp_move = countermove[temp2]
                #print "max used"
            
        return comp_convert[comp_move]

In [ ]:
#Initialize radio button    
    def initialize_rb():
        """Initialize radio button and prompt user for answer, onclick() event call sel() to return value"""
       
        #Collect user's move, defined before move because command calls the sel() fx
        def sel():
            """Called after a radio button is selected"""
            #selection =  "You selected the option " + str(rps.get()) #Debugging aid, not functional really
            #print selection
            user_move = rps.get()   #Determine the user's radio button selection to
            if user_move == 4:
                print "Games played: " + str(games[0]) + "; User Wins: " + str(wins[0])
                printstatus()
            elif user_move == 1:
                global r 
                r+= 1
            elif user_move == 2:
                global p 
                p+=1
            elif user_move == 3:
                global s 
                s+=1
            root.destroy()
            return user_move
        
        root = Tk()          #Root is the form object, and parent of the radio buttons
        root.title("What would you like to do?")
        rps = IntVar()
        Label(root, 
            text='Rock, Paper, Scissors OR Quit:',
            justify = LEFT,
            padx = 20).pack()

        R1 = Radiobutton(root, text="Rock", variable=rps, value=1, command=sel)   #command calls select on user input(click)
        R1.pack(anchor=W)
        R2 = Radiobutton(root, text="Paper", variable=rps, value=2, command=sel)
        R2.pack(anchor=W)
        R3 = Radiobutton(root, text="Scissors", variable=rps, value=3, command=sel)
        R3.pack(anchor=W)
        R4 = Radiobutton(root, text="Quit", variable=rps, value=4, command=sel)
        R4.pack(anchor=W)
        root.mainloop()
        return rps.get()

In [ ]:
def winner(comp_move_number,user_move,play):
    """Takes user's move and decides who wins OR quits""" 
    if user_move == 4:
        play = "False"
    else:
        if comp_move_number - user_move in [-1,2]:
            wins[0] += 1
            print "You won!"
            #Update game counter        
            games[0] +=1
        elif comp_move_number - user_move == 0:
            ties[0] += 1
            print "You tied :/ "
            #Update game counter        
            games[0] +=1
        else:
            losses[0] +=1
            print "You lost silly"
            #Update game counter        
            games[0] +=1
        return play

In [ ]:
i=1
play = "True"
while play == "True":
    #printstatus() #check status
    comp = computer_moves(i) #call computer move
    usr = initialize_rb() #get user move
    print "User: " + num_convert[usr]
    print "Computer: " + num_convert[comp]
    play = winner(comp,usr,play) #decide winner
    i+=1

Problem 2


In [ ]:
#import sys
import re

# This function read the input information from the "FloridaVoters.html" file
# and print out all the countries' name with their number of democratic
# and publician that sorted by number of democratic voters

def extract_names(filename): # pass in the input file as parameter
    """This function imports a file, creates a list to record all countries, then reads the file into a list of strings.\
    Thenhen finds all <td> tags, and does some processing to isolate republican, democratic votes and put them into a tuple\
    It then sorts the votes by democratic votes"""
    
    names = [] # create a list to record all the countries' name
    f = open(filename, 'r') # open the input file 
    text = f.read() # read the input file into a list of string

    # find all table entries
    data = re.findall(r'<td>(.*)</td>', text)

    for i in range(len(data) // 6):
        repub = int(data[i * 6 + 1].replace(',','')) # the second string for every six string is the number of republician
        democ = int(data[i * 6 + 2].replace(',','')) # the third string for every six string is the number of democratic 
        single_entry = (data[i * 6], repub, democ) # put each each county's name, democratic, republician into a tuple
        voters_data.append(single_entry) # append the tuple into a list

voters_data = [] 
extract_names('FloridaVoters.html') # the file we used as input file is 'FloridaVoters.html'
voters_data = sorted(voters_data, key=lambda tup: tup[2]) # sorted the voters_data list by the number of democratic
# print each countries name with their number of democratic and republician
for entry in voters_data:
    print entry[0], str(entry[1]), str(entry[2])

Problem 3


In [ ]:
fhand= open('quotes.txt')
quote = []
n = 0
for line in fhand: 
    line=line.rstrip()
    n+=1
    if n % 2 != 0: a = line
    else: quote.append(a + '-'+ line )

print quote[0] # test quote[0] result

fhand.close

3.b


In [ ]:
import re

def quote2word(quote):
    """transfer a quote into words"""
    
    word=re.split('\W+', quote) # using regular expression '/W+' to choose non-number and non-letter charactors as splitpoint
    return [x.lower() for x in word]
    
    
print quote2word(quote[0]) # test quote[0] result

3.c


In [ ]:
from collections import Counter

def postinglist(quotelist): 
    """get a dictionary whose keys are full quotes,and whoes value are themselves dictionaries,\
    with key being a word and value being the number of times the word appears in that full quote"""
    
    postlist = {}
    for quote in quotelist:
        word=quote2word(quote) # change each quote into words
        wordCount = dict(Counter(word)) #count the number of times a word appears in a quote
        postlist[quote]= wordCount
    return postlist

postlist=postinglist(quote)
print postlist  # test the result of postings-list

3.d


In [ ]:
def reverse_postinglist(quotelist):
    """get a dictionary whose keys are the words, and the values are themselves dictionary, with the key\
    being a full quote and the value being the number of times the word appears in the full quote"""
    
    Repostlist = dict()
    for quote,wordcount in postlist.items():
        for word,count in wordcount.items():
            if word in Repostlist : # if a word is added in Repostlist, then add quote and count to the values of the Repostlist[word].
                Repostlist[word][quote]=count
            else:                   # if word is not added in Repostlist, build a Repostlist[word] first, then add quote and count to the values of Repostlist[word].
                Repostlist[word] = dict()
                Repostlist[word][quote]=count
    return Repostlist

Repostlist=reverse_postinglist(quote)
print Repostlist['we'] #test the result of a given word 'we' in Reposting-list

3.e


In [ ]:
def TF(w,q):
    """get the TF value for a given word and a given quote"""

    pl=postlist[q]
    TFvalue=pl[w]/float(max(pl.values()))  #float the max(pl.value) in order to get a float value for TFvalue
    return TFvalue

import math
def IDF(w):
    """get the IDF value for a given word"""
    
    IDFvalue=math.log((len(quote)/float(len(Repostlist[w])))) #float the len(Repostlist[w]) in order to get a float value for IDFvalue
    return IDFvalue
    
def TF_IDF(w,a):
    """get the TF_IDF value for a given word and a given quote"""
    
    return TF(w,a)*IDF(w)
     
print TF_IDF('we',quote[0]) # test the TF_IDF value for 'we' in quote[0]
print quote[0]

3.f


In [ ]:
def Quote_search_single(w):
    """get a dictinary whose keys are full quotes containing a given word, and whose values the TF_IDF score\
    of that word for that full quote"""
    
    if w not in Repostlist.keys():  # if the word is not in any quote, break
        print w +" doesn't apear in quotes at all"
        print "funtion ended."
        return {}
    else: 
        dict1=Repostlist[w]
        dict2=dict()
        for k in dict1.keys():
            #print k
            a = TF_IDF(w,k)
            dict2[k]= a
        return dict2

print Quote_search_single('we') # test the result for word 'we'

3.g


In [ ]:
def Quote_search_multiple(list):
    """get a dictionary whose keys are full quotes containing words in a given word list, and whose values\
    are the sum of TF_IDF scores of all the words for that full quote"""
    dict2=dict()    
    for w in list:
        if w not in Repostlist.keys(): # if the word is not in any quote, break
            print w +" doesn't apear in quotes at all"
            print "funtion ended."
            return
        else:    
            dict1=Quote_search_single(w)
            for k,v in dict1.items():
                if k in dict2: dict2[k]=dict2[k]+v
                else:          dict2[k]=v
    return dict2

print Quote_search_multiple(['we','him']) # test code for ['we','him']

In [ ]: