#OK to post homework #Zidong Zhang, 31/1/2021, Assignment 2 with(combinat): #SetToVec(S,n): that inputs a subset S of {1,...,n} and outputs the 0-1 vector of length n #such that v[i]=1 iff i belongs to S. Be sure to check that the input is legal (i.e. that S is #indeed a set, and that n is a non-neg. integer and that S is indeed a subset of {1,...,n}. SetToVec := proc(S,n) local i,j,v: if not (type(S,set) and n>=0) then print(`illegal input!`): RETURN(FAIL): fi: if not S subset {seq(i, i =1..n)}=true then print(`wrong input`): RETURN(FAIL): fi: v:=[]: for j from 1 to n do if j in S then v := [op(v),1]: else v := [op(v),0]: fi: od: v: end: #VecToSet(v): that inputs a 0-1 vector v and outputs the subset of {1,...,n} (where n=nops#(v)) such that v[i]=1 iff i belongs to S. VecToSet := proc(v) local i,s,j,n: n:=nops(v): for i from 1 to n do if v[i]>1 then print(`entries of v should either 0 or 1`): RETURN(FAIL): fi: od: s :={}: for j from 1 to nops(v) do if v[j] = 1 then s := s union {j}: fi: od: s: end: #CheckBij(n): that checks that SetToVec(VecToSet(v),n)=v is always true for each member #of Box([1$n]) and VecToSet(SetToVec(S,n))=S for each member of MyPS({seq(i,i=1..n)}) . CheckBij := proc(n) local v,i,j,S,k: v := [seq(rand(0..1)(), i =1..n)]: if SetToVec(VecToSet(v),n) = v then print(`SetToVec(VecToSet(v),n) = v is true.`): else print(`SetToVec(VecToSet(v),n) = v is not true.`): fi: S := MyPS({seq(j,j=1..n)}): for k from 1 to nops(S) do if VecToSet(SetToVec(S[k],n))=S[k] then print(`VecToSet(SetToVec(S,n))=S is true for each member of MyPS({seq(i,i=1..n)})`): RETURN(TRUE): else print(`VecToSet(SetToVec(S[k],n))=S[k] is false for each member of MyPS({seq(i,i=1..n)})`): RETURN(FALSE): fi: od: end: