#OK to post #Yuxuan Yang, Jan 28th, Assignment 2 Help:=proc(): print(`SetToVec(S,n),VecToSet(v)`): end: with(combinat): #SetToVec(S,n): 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. SetToVec:=proc(S,n) local nn,j,i,v: if not type(S,set) then print(S, `should be a set `): RETURN(FAIL): fi: if not (type(n,integer) and n>=0) then print(n, `should be an integer`): RETURN(FAIL): fi: nn:={seq(j,j=1..n)}: if not S subset nn then print(S, `should be a subset of {1,2,...,n}`): RETURN(FAIL): fi: v:=[]: for i from 1 to n do if evalb(i in S) then v:=[op(v),1]: else v:=[op(v),0]: fi: od: v: end: VecToSet:=proc(v) local S,i,n: S:={}: n:=nops(v): for i from 1 to n do if v[i]=1 then S:=S union {i}: fi: od: S: end: #Box from C2 Box:=proc(L) local k,i,L1,B1,b1: option remember: if not type(L,list) then print(L, `should be a list `): RETURN(FAIL): fi: k:=nops(L): if k=0 then RETURN([[]]): fi: if not ({seq(type(L[i],integer),i=1..nops(L))}={true} and min(op(L))>=0) then print(`bad input`): RETURN(FAIL): fi: L1:=[op(1..k-1,L)]: B1:=Box(L1): [ seq(seq([op(B1[j]),i],i=0..L[k]),j=1.. nops(B1))]: end: #MyPS from C2 MyPS:=proc(S) local s,S1,PS1,s1: if not type(S,set) then print(S, `should be a set `): RETURN(FAIL): fi: if S={} then RETURN({{}}): fi: s:=S[nops(S)]: S1:=S minus {s}: PS1:=MyPS(S1): PS1 union { seq( s1 union {s}, s1 in PS1)}: end: #CheckBij(n): 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 S,i,j,nones,B,M: nones:=[seq(1,i=1..n)]: B:=Box(nones): for j from 1 to nops(B) do if not SetToVec(VecToSet(B[j]),n)=B[j] then RETURN(FAIL): fi: od: M:=MyPS({seq(i,i=1..n)}): for j from 1 to nops(M) do if not VecToSet(SetToVec(M[j],n))=M[j] then RETURN(FAIL): fi: od: RETURN(TURE): end: #NextInLine(L,v):inputs a list L and a member v of the list of lists (dictionary) and returns the vector right after it (in dictionary (lex.) order) without actually constructiong the (usually) very large set Box(L) NextInLine:=proc(L,v) local v1,n,i,j: n:=nops(L): v1:=v: for j from 1 to n do i:=n+1-j: if v1[i]=L[i] then v1[i]:=0: else v1[i]:=v1[i]+1: RETURN(v1): fi: od: print(v,`is a last one`): end: