Question 1: Use a procedure CountRuns(L,k) that inputs a list L of numbers (or for that matter, any objects) and outputs the number of "runs" of length k in L [A run of length k is a string L is an occurence of an i such that L[i]=L[i+1]=...=L[i+k-1] For example CountRuns([1,2,2,2,1,1,1,1,2,2,2],3) is 4 for the runs starting at i=2, i=5, i=6, i=9. Note that it can be part of a longer run. Answer 1: CountRuns:=proc(L, k) local i, j, ans: option remember: ans :=0: j:=1: for i in L do: if nops({op(j..(j+k-1),L)})=1 then ans:=ans+1: fi: j:=j+1: if j =nops(L)-k+2 then: break: fi: od: ans: end proc: Question 2 - Use the above CountRuns(L,k) that you just wrote, combined with procedure RPath in M14.txt to write a procedure AvePathRuns(m,n,k,N) That inputs positive inetgers m,n,k, and a LARGE positive integer N, generated N random paths, for each of them records the number of runs, and finds the sample average and standard-deviation (the squre-root of the variance) Run AvePathRuns(200,200,5,3000) ten times and see what you get. Are they close to each other? Answer 2: AvePathRuns:=proc(m, n,k,N) local i, l, t, t1, v, sd: with(Statistics): t:=[]: t1:=0: for i in [seq(i, i=1..N)] do: l:= RPath(m ,n): t:=[op(t), CountRuns(l, k)]: t1:=t1+t[nops(t)]: od: t1/N: StandardDeviation(t) end proc: seq(AvePathRuns(200,200,5,300), i=1..10) 7.77837596574227, 7.37252424291535, 7.91017013264021, 7.65842414754069, 7.33864027089767, 7.29208731068898, 7.27240627141998, 7.45157162835072, 7.51714799211376, 7.38969423081714 We see that the values are very close to each other Question 3 - Do the analogous thing for Good paths, writing AveGPathRuns(m,n,k,N) [Hint: you only have to change one word in the code] Run AvePathGRuns(200,200,5,3000) ten times. How does it compare with the previous output for all paths? Answer 3 AveGPathRuns:=proc(m, n,k,N) local i, l, t, t1, v, sd: with(Statistics): t:=[]: t1:=0: for i in [seq(i, i=1..N)] do: l:= RGPath(m ,n): t:=[op(t), CountRuns(l, k)]: t1:=t1+t[nops(t)]: od: t1/N: StandardDeviation(t) end proc: AveGPathRuns(200, 200, 5, 10) 5.79367471184452 RGPath(3, 3) [1, 2, 1, 2, 1, 2] seq(AveGPathRuns(200,200,5,300), i=1..10) 7.52190706149010, 7.54945345648536, 7.55682375633314, 7.26547401052040, 7.57099434730418, 7.75782842651255, 6.85491954453850, 7.24277520306550, 7.54327241662354, 7.75374257818667 Question 4: Question 5: Write a procedure GoodBrother(L) that inputs an arbitrary list of ODD length that adds up to 1 (so if the length is 2*n+1 it must have n+1 1's and n -1's), and outputs the UNIQUE cyclic shift that is a so-called Dyck path of length 2*n with 1 appended at the end. Answer 5: GoodBrother:=proc(L) local i, j, total, S: option remember: total:=0: S:=CycShi(L): for i in S do #print(i): total:=0: for j in i do #print(j): total:=total+j: print(total): if total<0 then total:=0: break: fi: #od: if total != 0 then: print(i): fi: od: od: end proc: