Coding the Matrix Week 0 作业

Coding the Matrix: Linear Algebra through Computer Science Applications


  本次作业分成三部分,第一部分点击打开链接 已经记录过,后两部分也早已完成,趁有时间记下来。

  hw0 比较简单,如果有问题在论坛都可以找到答案。不过要注意使用python3运行。不过没有想到python里面也支持j作为虚数单位。

# Please fill out this stencil and submit using the provided submission script.





## Problem 1
def myFilter(L, num): return [x for x in L if x%num]



## Problem 2
def myLists(L): return [list(range(1,x+1)) for x in L]



## Problem 3
def myFunctionComposition(f, g): return {x:g[f[x]] for x in f.keys()}


## Problem 4
# Please only enter your numerical solution.

complex_addition_a = 5+3j
complex_addition_b = 0+1j
complex_addition_c = -1+.001j
complex_addition_d = .001+9j



## Problem 5
GF2_sum_1 = 1
GF2_sum_2 = 0
GF2_sum_3 = 0


## Problem 6
def mySum(L):
    sumL=0
    for x in L:
        sumL+=x
    return sumL



## Problem 7
def myProduct(L):
    sumL=1
    for x in L:
        sumL*=x
    return sumL



## Problem 8
def myMin(L):
    sumL=L[0]
    for x in L:
        if sumL>x:
            sumL=x
    return sumL



## Problem 9
def myConcat(L):
    output=''
    for x in L:
        output+=x
    return output



## Problem 10
def myUnion(L):
    output=set()
    for x in L:
        output=output | x
    return output


  inverse_index_lab也比较简单:

from random import randint
from dictutil import *

## Task 1
def movie_review(name):
    """
    Input: the name of a movie
    Output: a string (one of the review options), selected at random using randint
    """
    review_options = ["See it!", "A gem!", "Ideological claptrap!"]
    return review_options[randint(0,len(review_options)-1)]

## Tasks 2 and 3 are in dictutil.py

## Task 4    
def makeInverseIndex(strlist):
    """
    Input: a list of documents as strings
    Output: a dictionary that maps each word in any document to the set consisting of the
            document ids (ie, the index in the strlist) for all documents containing the word.

    Note that to test your function, you are welcome to use the files stories_small.txt
      or stories_big.txt included in the download.
    """
    output={}
    for (i,x) in list(enumerate(strlist)):
        tmp=x.split()
        for word in tmp:
            if word in output:
                output[word]=output[word] | {i}
            else:
                output[word]={i}
    return output

## Task 5
def orSearch(inverseIndex, query):
    """
    Input: an inverse index, as created by makeInverseIndex, and a list of words to query
    Output: the set of document ids that contain _any_ of the specified words
    """
    output=set()
    for word in query:
        output=output | inverseIndex[word]
    return output

## Task 6
def andSearch(inverseIndex, query):
    """
    Input: an inverse index, as created by makeInverseIndex, and a list of words to query
    Output: the set of all document ids that contain _all_ of the specified words
    """
    output=set()
    if len(query)==0:return output
    output=inverseIndex[query[0]]
    for word in query:
        output=output & inverseIndex[word]
    return output


你可能感兴趣的:(Coding the Matrix Week 0 作业)