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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值