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