Coding the Matrix Week 0 作業

來源:互聯網
上載者:User
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 1def myFilter(L, num): return [x for x in L if x%num]## Problem 2def myLists(L): return [list(range(1,x+1)) for x in L]## Problem 3def 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+3jcomplex_addition_b = 0+1jcomplex_addition_c = -1+.001jcomplex_addition_d = .001+9j## Problem 5GF2_sum_1 = 1GF2_sum_2 = 0GF2_sum_3 = 0## Problem 6def mySum(L):    sumL=0    for x in L:        sumL+=x    return sumL## Problem 7def myProduct(L):    sumL=1    for x in L:        sumL*=x    return sumL## Problem 8def myMin(L):    sumL=L[0]    for x in L:        if sumL>x:            sumL=x    return sumL## Problem 9def myConcat(L):    output=''    for x in L:        output+=x    return output## Problem 10def myUnion(L):    output=set()    for x in L:        output=output | x    return output

  inverse_index_lab也比較簡單:

from random import randintfrom dictutil import *## Task 1def 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 5def 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 6def 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.