Coding the Matrix: Linear Algebra
through Computer Science Applications
這是一門用python實現矩陣運算的課,第一次作業就感覺對python的提高很大,用到了各種資料類型。
代碼如下:
## Task 1minutes_in_week = 60*24*7## Task 2remainder_without_mod = 2304811-2304811//47*47## Task 3divisible_by_3 = (673+909)/3==0## Task 4x = -9y = 1/2statement_val = 2**(y+1/2) if x+10<0 else 2**(y-1/2)## Task 5first_five_squares = { x*x for x in {1,2,3,4,5} }## Task 6first_five_pows_two = { 2**x for x in {0,1,2,3,4} }## Task 7: enter in the two new setsX1 = { 1, 2, 3 }Y1 = { 4, 11, 17 }## Task 8: enter in the two new setsX2 = { 1, 2, 0 }Y2 = { 4, 8, 16 }## Task 9base = 10digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}three_digits_set = { base**2*x+base*y+z for x in digits for y in digits for z in digits }## Task 10S = {1, 2, 3, 4}T = {3, 4, 5, 6}S_intersect_T = { x for x in S if x in T }## Task 11L_average = sum([20, 10, 15, 75])/len([20, 10, 15, 75]) # average of: [20, 10, 15, 75]## Task 12LofL = [[.25, .75, .1], [-1, 0], [4, 4, 4, 4]]LofL_sum = sum({sum(LofL[x]) for x in range(0,len(LofL))}) # use form: sum([sum(...) ... ])## Task 13cartesian_product = [[x,y] for x in {'A','B','C'} for y in {1,2,3} ] # use form: [ ... {'A','B','C'} ... {1,2,3} ... ]## Task 14S = {-4, -2, 1, 2, 5, 0}zero_sum_list = [ (x,y,z) for x in S for y in S for z in S if x+y+z==0 ] ## Task 15exclude_zero_list = [ (x,y,z) for x in S for y in S for z in S if (x+y+z==0) and (x or y or z) ]## Task 16first_of_tuples_list = [ (x,y,z) for x in S for y in S for z in S if (x+y+z==0) and (x or y or z) ][0]## Task 17L1 = [1,1,2] # <-- want len(L1) != len(list(set(L1)))L2 = [2,1,3] # <-- same len(L2) == len(list(set(L2))) but L2 != list(set(L2))## Task 18odd_num_list_range = { x for x in range(1,100,2)}## Task 19L = ['A','B','C','D','E']range_and_zip = list(zip(list(range(5)),L))## Task 20list_sum_zip = [x+y for (x,y) in zip([10,25,40],[1,15,20])]## Task 21dlist = [{'James':'Sean', 'director':'Terence'}, {'James':'Roger', 'director':'Lewis'}, {'James':'Pierce', 'director':'Roger'}]k = 'James'value_list = [d[k] for d in dlist]## Task 22dlist = [{'Bilbo':'Ian','Frodo':'Elijah'},{'Bilbo':'Martin','Thorin':'Richard'}]k = 'Bilbo'value_list_modified_1 = [d[k] if k in d else 'NOT PRESENT' for d in dlist] # <-- Use the same expression herek = 'Frodo'value_list_modified_2 = [d[k] if k in d else 'NOT PRESENT' for d in dlist] # <-- as you do here## Task 23square_dict = {x:x**2 for x in range(100)}## Task 24D = {'red','white','blue'}identity_dict = {x:x for x in D}## Task 25base = 10digits = set(range(10))representation_dict = { base**2*x+base*y+z:[x,y,z] for x in digits for y in digits for z in digits}## Task 26d = {0:1000.0, 1:1200.50, 2:990}names = ['Larry', 'Curly', 'Moe']listdict2dict = { names[x]:y for (x,y) in d.items() }## Task 27def nextInts(L): return [ x+1 for x in L ]## Task 28def cubes(L): return [ x**3 for x in L ] ## Task 29def dict2list(dct, keylist): return [ dct[x] for x in keylist ]## Task 30 def list2dict(L, keylist): return { x:y for (x,y) in zip(keylist,L) }
評分系統有一些bug,如task2不能用括弧等,如果出錯可以比對一下。
提交方法:
在linux下提交按照說明即可,但在windows下面,就要費好大一番功夫了。
由於submit_python_lab.py裡面預設待提交的py檔案在目前的目錄下,必須把py檔案和submit_python_lab.py檔案拷貝到python3的安裝資料夾下面。我用的是python3.3,因此要拷貝到的目錄為:C:\Python33。論壇說雙擊submit_python_lab.py輸入帳號密碼即可,為了方便我使用bat指令檔運行:
C:\Python33\python C:\Python33\submit_python_lab.py