︠8c18a7b9-ae80-471d-978c-eedeec1be08fs︠ ### Data types a = 3; # an integer b = "3.141"; # a string c = True; # boolean value d = (3,1,4,1); # a tuple (not mutable) e = [3,1,4,1]; # a list (mutable) f = {3,1,4,1}; # a set f={"three":3, "one":1, "four":4, "three":3}; # a dictionary ︡d9f8a207-5f6e-4a7a-b4a0-a479843cde73︡{"done":true}︡ ︠51f456ed-7095-4eae-9ab7-47cb5085b50as︠ ### use type(a) to check the type of a type(a) ︡91b8897e-4e46-431d-af45-52ee70f5d54e︡{"stdout":"\n"}︡{"done":true}︡ ︠ddeac15a-5c22-4fd9-b1af-450d3f25238f︠ ### in and not in students=["John","Jephian","Jane"]; "John" in students; ︡8649c6ef-bd5d-437e-b79f-32df1f2b3113︡ ︠d97e7c6c-c1f2-41ef-82d1-6d32a9f095a1︠ ### relations ### also try: >, >=, <, <=, != 3==3 ︡5b77b65b-3fd9-4709-a5ed-62397f3d7caa︡ ︠73a50303-6ddb-4804-8a99-20e7c1324bf3︠ ### arithmetic operators print "23+4 =", 23+4; print "23-4 =", 23-4; print "23*4 =", 23*4; print "23/4 =", 23/4; print "23**4 =", 23**4; print "23^4 =", 23^4; print "23%4 =", 23%4; print "23//4 =", 23//4; ︡1aed5918-2432-46d4-858f-a12c766e8118︡ ︠2732b113-3301-41a8-81d5-7cb6194f90e3s︠ ### Exercise: ### Guess the output of the following. type(2/3) ︡f2ce29e8-6f4f-4743-8650-f2e1eaffc44a︡{"stdout":"\n"}︡{"done":true}︡ ︠7e942dbe-6f0e-47d4-8256-4934aa5fad5e︠ a=2; a in [2,3,5]; ︡2e70e6b4-eca4-462e-95a1-ce4627956d78︡ ︠d434cfe5-7e68-4fa4-ac85-b88efbe5761c︠ a=2; a in ["a","b","c"]; ︡e9b447bb-8a6f-4f8e-83a3-4034f8cdd3a9︡ ︠6ca325e1-c599-4d2e-9743-d110b842cde6︠ a=2; type(a)==int; ︡2a3fa810-f2e1-44c0-ac61-c1d5d05e70c6︡ ︠7cc2b167-74cd-466c-a2bc-ca683d545d24︠ a=int(2); type(a)==int; ︡179283d2-1c7e-42cd-8a3b-4d56a59d0e61︡ ︠24590899-6881-48ff-8286-9e611de0d08d︠ (2,3,5)==[2,3,5] ︡26dc680a-1f92-44b6-95a8-0f8b664fcb79︡ ︠a8673181-bf11-47c4-9aa6-b43c3afa074c︠ 2==2.0 ︡0b1bc3ad-341e-43e5-8534-d4b6c4b60784︡ ︠6b4dd4a1-5d60-49a3-82bf-d2c4eba4507b︠ d={"two":2, "three":3, "five":5}; print 2 in d.keys(); print 2 in d.values(); ︡c58b6db7-ac1c-481e-8e73-0e028c7316e2︡ ︠d6e03d8c-9fba-41b3-bd27-3d7b9e0b0902︠ 50%3==0 ︡1f0de80a-04ff-47b1-97b5-2752fa37710e︡ ︠e5357348-508f-4980-9f9c-fe696d5104ed︠ 50%3==0 or 51%3==0 ︡41d1b967-b368-4c8d-9908-2b3b6beaef05︡ ︠ab82e739-4032-4a7d-acf1-51ef3866964a︠ 50%3==0 or 51%3==0 ︡39cffc8c-0e39-488d-8241-1775b37f17bb︡ ︠e4eb389a-c86b-46d5-9719-609f9f3554b1︠ [1,2,3,4]>[2,3,4] ︡be57bc93-dd10-4c48-a964-44ee1c289f8b︡ ︠9277a7d9-3b33-4a59-9f26-05e86800a7ce︠ "Z">"B" ︡0004c828-c1a0-4ab5-a416-ff58a8f43d89︡ ︠5be9d377-7646-4d18-97aa-b798d79958b6︠ ### type conversion a=2/2 print type(a) print (a==1) ### This line will print True in Sage but False in C a=Integer(a) ### Integer means an object in the integer ring in Sage; ### int is a data structure in most of programming language print type(a) ︡e4e32d9b-fa06-46f3-8204-70459fb0bd74︡{"stdout":"\n"}︡{"stdout":"True\n"}︡{"stdout":"\n"}︡{"done":true}︡ ︠e9fe6f5c-e999-4cbb-b1c4-88edd3cab504s︠ ### len returns the size ### append add an element to a list a = [3,1,4,1] print len(a) a.append(5) a.append(9) print a print len(a) ︡243b5e86-7fdb-4375-8a49-9127180c26b7︡{"stdout":"4\n"}︡{"stdout":"[3, 1, 4, 1, 5, 9]\n"}︡{"stdout":"6\n"}︡{"done":true}︡ ︠93e12b35-d850-426c-bc1f-a047d8ff3abb︠ ### check repeated elments ### set converse a list to a set a = [3,1,4,1] print set(a) print len(set(a)) < len(a) ︡fc35d79d-c50b-495b-b7e3-e586c411dbcb︡{"stdout":"set([1, 3, 4])\n"}︡{"stdout":"True\n"}︡{"done":true}︡ ︠2fb0f9c4-0ae2-429d-bb7c-f4724cf3fbc8s︠ ### Combinations(list,k) returns all k-combinations in list counter = 0 for com in Combinations(range(5),2): counter += 1; print com; print "---" print "total count is", counter print "5 choose 2 is", binomial(5,2) ︡6332726a-ac02-4bb2-9d15-9267222a380f︡{"stdout":"[0, 1]\n[0, 2]\n[0, 3]\n[0, 4]\n[1, 2]\n[1, 3]\n[1, 4]\n[2, 3]\n[2, 4]\n[3, 4]\n"}︡{"stdout":"---\n"}︡{"stdout":"total count is 10\n"}︡{"stdout":"5 choose 2 is 10\n"}︡{"done":true}︡ ︠ec8d067c-4ade-4e50-b956-23eb953e503e︠ ︡5413c436-e52a-492e-b8fd-96dedb5b51d9︡ ︠9f20d4a5-6626-47fd-9308-d8da5f58503di︠ %md ## Project 1 Suppose `positions` is a list of the form `[(x1,y1),(x2,y2),...]`. Write a function to check `positions` is non-attacking or not. ︡1491331a-ff0e-4231-ae95-9c18acf6be1f︡{"done":true,"md":"\n## Project 1\nSuppose `positions` is a list of the form `[(x1,y1),(x2,y2),...]`.\n\nWrite a function to check `positions` is non-attacking or not."} ︠e8a40d27-ed26-4b3c-81aa-4bc1b950f580︠ def is_nonattacking(positions): ### positions is a list of the form [(x1,y1),(x2,y2),...] ### create two lists xcors and ycors to records the x-coordinates and y-coordinates. ### len(set(l)) < len(l) allows you to check if l contains any repeated elements ### Your answers: xcors = [] ycors = [] for pos in positions: xcors.append(pos[0]); ycors.append(pos[1]); if len(set(xcors)) < len(xcors): return False; if len(set(ycors)) < len(ycors): return False; ### if nothing happens return True; ### test; positions1=[(0,0),(0,1)] positions2=[(0,0),(1,1)] print "your answer is {}, {}=correct answer".format(is_nonattacking(positions1),False) print "your answer is {}, {}=correct answer".format(is_nonattacking(positions2),True) ︡3c9220c1-7ec6-4b90-bd23-f2cb34d4bbe8︡{"stdout":"your answer is False, False=correct answer\n"}︡{"stdout":"your answer is True, True=correct answer\n"}︡{"done":true}︡ ︠9521f063-083a-4bb6-b448-aef313add05ci︠ %md ## Project 2 Suppose `B` is a list all positions on a board. For example `B = [(1,2),(1,3),(2,1),(2,3),(3,1),(3,2)]` for the $3\times 3$ derangement. Write a function to compute number of ways to put $k$ rooks on $B$. ︡bc1a6203-7757-4240-9efd-494b052c3ed8︡{"done":true,"md":"## Project 2\nSuppose `B` is a list all positions on a board.\n\nFor example `B = [(1,2),(1,3),(2,1),(2,3),(3,1),(3,2)]` for the $3\\times 3$ derangement.\n\nWrite a function to compute number of ways to put $k$ rooks on $B$."} ︠19071467-d0cd-4343-9c78-e4b15712975cs︠ def rook_placements(k,B): ### Hint: use Combinations(B,k) ### You answers: counter = 0; for positions in Combinations(B,k): if is_nonattacking(positions): counter += 1; return counter; ### test; n = 10 B = [(i,i) for i in range(n)] + [(i,(i+1)%n) for i in range(n)] for k in range(n+1): print "your answer for r_10,{} is {}, {}=correct answer".format(k,rook_placements(k,B),2*n/(2*n-k)*binomial(2*n-k,k)) ︡b02a5f35-b21e-4bde-9453-bd5a7584f213︡{"stdout":"your answer for r_10,0 is 1, 1=correct answer\nyour answer for r_10,1 is 20, 20=correct answer\nyour answer for r_10,2 is 170, 170=correct answer\nyour answer for r_10,3 is 800, 800=correct answer\nyour answer for r_10,4 is 2275, 2275=correct answer\nyour answer for r_10,5 is 4004, 4004=correct answer"}︡{"stdout":"\nyour answer for r_10,6 is 4290, 4290=correct answer"}︡{"stdout":"\nyour answer for r_10,7 is 2640, 2640=correct answer"}︡{"stdout":"\nyour answer for r_10,8 is 825, 825=correct answer"}︡{"stdout":"\nyour answer for r_10,9 is 100, 100=correct answer"}︡{"stdout":"\nyour answer for r_10,10 is 2, 2=correct answer"}︡{"stdout":"\n"}︡{"done":true}︡ ︠6a6551c2-dba9-4378-809b-1fc31e17c0aa︠