#OK to post homework #Wanying Rao, 01/30/2021, Assignment 3 Help := proc(): print(`NewUCG(n), BoxG(L), Tomorrow(Date), Yesterday(Date)`): end: with(combinat): with(linalg): #1 #NewUCG(n): A new UCG(n) procedure by continuously applying NEXG NewUCG := proc(n) local i,w,B: B := []: w := [0$n]: for i from 1 to 2^n do B := [op(B), w]: w := NEXG(w): od: RETURN(B): end: #Checked that UCG(n)=NewUCG(n) for n = 1..14 by running: #for k to 14 do #if NewUCG(k) = UCG(k) then print(k): fi: #od: #2 #BoxG(L): Inputs a list L of positive integers and outputs Box(L) with another order that #when you go from one member to the next, it only changes one element BoxG := proc(L) local i,n,B,BB: option remember: n := nops(L): if n=0 then RETURN([[]]): fi: B:=BoxG(L[2..n]): BB := PreP(0,B): for i from 1 to L[1] do if irem(i,2) = 1 then BB := CAT(BB,PreP(i,REV(B))): else BB := CAT(BB,PreP(i,B)): fi: od: RETURN(BB): end: #BoxG([2, 2, 2])=[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 2], [0, 1, 1], [0, 1, 0], [0, 2, #0], [0, 2, 1], [0, 2, 2], [1, 2, 2], [1, 2, 1], [1, 2, 0], [1, 1, 0], [1, 1, 1], [1, 1, #2], [1, 0, 2], [1, 0, 1], [1, 0, 0], [2, 0, 0], [2, 0, 1], [2, 0, 2], [2, 1, 2], [2, 1, #1], [2, 1, 0], [2, 2, 0], [2, 2, 1], [2, 2, 2]] #3 #Tomorrow(Date): Inputs a Date=[DayOfTheMonth, Month, Year, DayOfTheWeek] and outputs the #date of the next day Tomorrow := proc(Date) local TDate, leap: TDate := Date + [1, 0, 0, 1]: if ((irem(Date[3],4) = 0 and irem(Date[3],100) != 0) or irem(Date[3],400) = 0) then leap := 1: else leap := 0: fi: if (Date[2] = 2 and (leap = 0 and TDate[1] = 29) or (leap = 1 and TDate[1] = 30)) then TDate[1] := 1: TDate[2] := 3: elif ((Date[2] in {1,3,5,7,8,10,12} and TDate[1] = 32) or (Date[2] in {4,6,9,11} and TDate[1] = 31))then TDate[1] := 1: TDate[2] := TDate[2] + 1: fi: if TDate[2] =13 then TDate[2] := 1: TDate[3] := TDate[3] + 1: fi: if TDate[4] = 8 then TDate[4] := 1: fi: RETURN(TDate): end: #Yesterday(Date):Inputs a Date=[DayOfTheMonth, Month, Year, DayOfTheWeek] and outputs the #date of the previous day Yesterday := proc(Date) local YDate, leap: YDate := Date - [1, 0, 0, 1]: if ((irem(Date[3],4) = 0 and irem(Date[3],100) != 0) or irem(Date[3],400) = 0) then leap := 1: else leap := 0: fi: if (Date[1] = 1 and Date[2] =3 and leap = 1) then YDate[1] := 29: YDate[2] := 2: elif (Date[1] = 1 and Date[2] =3 and leap = 0) then YDate[1] := 28: YDate[2] := 2: elif (Date[2] in {2,4,6,8,9,11,1} and Date[1] = 1) then YDate[1] := 31: YDate[2] := YDate[2] - 1: elif (Date[2] in {5,7,10,12} and Date[1] = 1) then YDate[1] := 30: YDate[2] := YDate[2] - 1: fi: if YDate[2] = 0 then YDate[2] := 12: YDate[3] := YDate[3] - 1: fi: if YDate[4] = -1 then YDate[4] := 7: fi: RETURN(YDate): end: #####C3##### Help3:=proc():print(` Box(L), CAT(L1,L2) , PreP(a,L), App(L,a), UC(n) , NEX(w) `): print(`REV(L), UCG(n), NEXG(w), PREG(w) , RBW(n)`): end: #Box(L): Inputs a list L (where k=nops(L)) of non-negative integers L outputs #the LIST of all LISTS [a1,a2,..., ak] in LEX ORDER such that ai is in {0,1,.., L[i]) #In particular the n-dim UNIT cube would be Box([1$n]); Box:=proc(L) local k,i,L1,B1,j: 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: #CAT(L1,L2): joining two lists in order CAT:=proc(L1,L2): [op(L1),op(L2)]:end: #PreP(a,L): Given a list L of lists, outputs the list where #sticks a to the beginnin of each of them #For example #PreP(0,[[1,2,3],[1,3,4]])= [[0,1,2,3],[0,1,3,4]] PreP:=proc(a,L) local i: [ seq([a, op(L[i])],i=1..nops(L))]:end: #App(L,a): Given a list L of lists, outputs the list where #sticks a to the end of each of them #For example #App([[1,2,3],[1,3,4]],0)= [[1,2,3,0],[1,3,4,0]] App:=proc(L,a) local i: [ seq([op(L[i]),a],i=1..nops(L))]:end: #UC(n): inputs a non-neg. integer n, and outputs the n-dim unit cube [0,1]^n #AS A LIST OF 0-1 vectors in Lex-order (same as Box([1$n]) UC:=proc(n) local B: option remember: if n=0 then RETURN([[]]): fi: B:=UC(n-1): CAT(PreP(0,B),PreP(1,B)): end: #NEX(w): Inputs a 0-1 vector w (of length n=nops(w)) outputs the vector right after it in lex order #or returns FAIL NEX:=proc(w) local n,w1: n:=nops(w): if n=0 then print(`You have reached the top`): RETURN(FAIL): fi: w1:=[op(1..n-1,w)]: if w[n]=0 then RETURN([op(w1),1]): fi: w1:=NEX(w1): if w1=FAIL then RETURN(FAIL): else RETURN([op(w1),0]): fi: end: #NEXr(w): Inputs a 0-1 vector w (of length n=nops(w)) outputs the vector right after it in lex order #or returns FAIL NEXr:=proc(w) local n,w1: option remember: n:=nops(w): if n=0 then print(`You have reached the top`): RETURN(FAIL): fi: w1:=[op(1..n-1,w)]: if w[n]=0 then RETURN([op(w1),1]): fi: w1:=NEXr(w1): if w1=FAIL then RETURN(FAIL): else RETURN([op(w1),0]): fi: end: #REV(L): REV:=proc(L) local i: [seq(L[-i],i=1..nops(L))]:end: #UCG(n): inputs a non-neg. integer n, and outputs the n-dim unit cube [0,1]^n #USING THE GRAY-CODE WHERE ONLY ONE BIT CHANGES AT A TIME #AS A LIST OF 0-1 vectors in Lex-order (same as Box([1$n]) UCG:=proc(n) local B: option remember: if n=0 then RETURN([[]]): fi: B:=UCG(n-1): CAT(PreP(0,B),PreP(1,REV(B))): end: #NEXG(w): inputs a word in {0,1}^n (n=nops(w)) and outputs the NEXT one in the the Gray Code given by UCG(n). #Try: NEXG([1,0,1]); NEXG:=proc(w) local n,w1, w1Next,w1Prev: n:=nops(w): if n=1 then if w[1]=0 then RETURN([1]): else RETURN(FAIL): fi: fi: w1:=[op(2..n,w)]: if w[1]=0 then w1Next:=NEXG(w1): if w1Next=FAIL then RETURN([1,op(w1)]): else RETURN([0,op(w1Next)]): fi: elif w[1]=1 then #Since w[1]=1, we are going BACKWARDS w1Prev:=PREG(w1): if w1Prev=FAIL then #IF THERE IS NO WAY TO GO WE RETURN FAIL RETURN(FAIL): else RETURN([1,op(w1Prev)]): fi: else print(`Something is wrong`): RETURN(FAIL): fi: end: #PREG(w): inputs a word in {0,1}^n (n=nops(w)) and outputs the PREVIOUS one in the the Gray Code given by UCG(n). #Try: NEXG([1,0,1]); PREG:=proc(w) local n,w1, w1Prev,w1Next: n:=nops(w): if n=1 then if w[1]=0 then RETURN(FAIL): else RETURN([0]): fi: fi: w1:=[op(2..n,w)]: if w[1]=0 then w1Prev:=PREG(w1): if w1Prev=FAIL then RETURN(FAIL): else RETURN([0,op(w1Prev)]): fi: elif w[1]=1 then #Since w[1]=1, we are going FORWARD w1Next:=NEXG(w1): if w1Next=FAIL then #WE CHANGE THE first bit RETURN([0,op(w1)]): else RETURN([1,op(w1Next)]): fi: else print(`Something is wrong`): RETURN(FAIL): fi: end: #RBW(n): A random binary word in {0,1} of length n RBW:=proc(n) local ra,i: ra:=rand(0..1): [seq(ra(),i=1..n)]: end: