#OK to post homework #George Spahn, 1/31/2021, Assignment 3 #NewUCG(n) same input and output as UCG(n), but instead starts with the #vector [0$n] and keeps applying NEXG(w), always appending the #new guy to the list. NewUCG:=proc(n) local v,l: l:=[]: v:=[0$n]: while not v=FAIL do l:=[op(l),v]: v:=NEXG(v): od: l: end: #BoxG(L) Inputs a list of positive integers and outputs a list whose members #are the same as Box(L), but in such an order that when you go from one member #to the next, it only changes in one place BoxG:=proc(L) local n,B,i,toRet: n:=nops(L): if n=0 then RETURN([[]]): fi: B:=BoxG([op(2..n,L)]): toRet:=[]: for i from 0 to L[1] do: if irem(i,2)=0 then toRet:= CAT(toRet, PreP(i,B)): else toRet:= CAT(toRet, PreP(i,REV(B))) fi: od: toRet: end: #Tomorrow(Date) inputs four-tuple #Date=[DayOfTheMonth , Month , Year, DayOfTheWeek ] #and outputs the date of the next day Tomorrow:=proc(date) local NewDate,thirties: thirties:= {4,6,9,11}: NewDate:=date: NewDate[1]:= date[1]+1: NewDate[4]:= date[4]+1: if NewDate[4]= 8 then NewDate[4]:= 1: fi: if date[2]=2 and (date[1] = 29 or (date[1]=28 and (irem(date[3],4)<>0 or (irem(date[3],100)=0 and irem(date[3],400)<>0)))) then NewDate[1]:=1: NewDate[2]:=3: fi: if (date[1]=30 and date[2] in thirties) or date[1]=31 then NewDate[1]:=1: NewDate[2]:=date[2]+1: if NewDate[2] = 13 then NewDate[3]:=date[3]+1: NewDate[2]:=1: fi: fi: NewDate: end: #Yesterday(Date) inputs four-tuple #Date=[DayOfTheMonth , Month , Year, DayOfTheWeek ] #and outputs the date of the previous day Yesterdady:=proc(date) local NewDate,thirties: thirties:= {4,6,9,11}: NewDate:=date: NewDate[1]:= date[1]-1: NewDate[4]:= date[4]-1: if NewDate[4]= 0 then NewDate[4]:= 7: fi: if NewDate[1]=0 then NewDate[2]:= date[2]-1: NewDate[1]:= 31: if NewDate[2] in thirties then NewDate[1]:=30: fi: if NewDate[2] = 0 then NewDate[3]:=date[3]-1: NewDate[2]:=12: fi: fi: if date[2]=3 and date[1]=1 then NewDate[1]:=29: if irem(date[3],4)<>0 or (irem(date[3],100)=0 and irem(date[3],400)<>0) then NewDate[1]:=28: fi: fi: NewDate: end: #MyNEXG(w) NEXG without recursion MyNEXG:=proc(w) local v,n,i,b: n:=nops(w): v:=w: b:=0: for i from 1 to n do: b:=b+w[i]: od: if irem(b,2)=0 then v[n]:=irem(w[n]+1,2): RETURN(v): fi: for i from 1 to n do: if w[-i] = 1 then if i=n then RETURN([0$n]): fi: v[-(i+1)]:=irem(w[-(i+1)]+1,2): RETURN(v): fi: od: end: PreP:=proc(a,L) local i: [ seq([a, op(L[i])],i=1..nops(L))]:end: CAT:=proc(L1,L2): [op(L1),op(L2)]:end: REV:=proc(L) local i: [seq(L[-i],i=1..nops(L))]: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: