#Ok to post homework #Tifany Tong, October 25th, 2020, HW #14 # Question 1 CountRuns := proc(L, k) local i, count, ans; ans := 0; count := 0; for i from 2 to nops(L) do if L[i] = L[i - 1] then count := count + 1: fi: if L[i] <> L[i - 1] and k <= count then ans := ans + count - k + 1: count := 1: fi: if L[i] <> L[i - 1] and count < k then count := 1: fi: od: if k <= count then ans := ans + count - k + 1: fi: ans: end: # Question 2 with(Statistics); AvePathRuns := proc(m, n, k, N) local i, total, currL, runs, path: currL := []: for i to N do path := RPath(m, n): runs := CountRuns(path, k): currL := [op(currL), runs]: od: print(StandardDeviation(currL)): print(Mean(currL)): end: #Standard Deviation: 7.48732098600057 #Mean: 24.0933333333333 #Standard Deviation: 7.61013518986958 #Mean: 24.3273333333333 #... etc #I get similar results for each of the 10 results. They are close. # Question 3 with(Statistics): AveGPathRuns := proc(m, n, k, N) local i, ans, total, currL, runs, path: currL := []; for i to N do path := RGPath(m, n): runs := CountRuns(path, k): currL := [op(currL), runs]: od: print(StandardDeviation(currL)): print(Mean(currL)): end proc: #Standard Deviation: 7.64740168116247 #Mean: 24.0086666666667 #Standard Deviation: 7.51583696393309 #Mean: 23.9680000000000 #... etc #I get similar results for each of the 10 results. They are close. # Question 4 CountMaximalRuns := proc(L, k) local i, count, ans: ans := 0: count := 0: for i from 2 to nops(L) do if L[i] = L[i - 1] then count := count + 1: elif L[i] <> L[i - 1] and k < count then count := 1: elif L[i] <> L[i - 1] and count = k then ans := ans + 1: count := 1: elif L[i] <> L[i - 1] and count < k then count := 1: fi: od: if count = k then ans := ans + 1: fi: ans: end: with(Statistics): AvePathMaximalRuns := proc(m, n, k, N) local i, ans, total, currL, runs, path: currL := []: for i to N do path := RPath(m, n): runs := CountMaximalRuns(path, k): currL := [op(currL), runs]: od: print(StandardDeviation(currL)): print(Mean(currL)): end: # Standard Deviation: 2.38129273167193 # Mean: 6.16533333333333 # Standard Deviation: 2.39643356960019 # Mean: 6.19066666666667 # ... etc # We generally get the same value across the multiple trials. with(Statistics); AveGPathMaximalRuns := proc(m, n, k, N) local i, ans, total, currL, runs, path: currL := []: for i to N do path := RGPath(m, n): runs := CountMaximalRuns(path, k): currL := [op(currL), runs]: od: print(StandardDeviation(currL)): print(Mean(currL)): end: # Standard Deviation: 2.36087472462588 # Mean: 6.15533333333333 # Standard Deviation: 2.33465409832131 # Mean: 6.23066666666667 # ... etc # We generally get the same value across the multiple trials. # Question 5 check := proc(L) local j, curr, ans: curr := 0: for j to nops(L) - 1 do curr := curr + L[j]: if curr < 0 then return false: fi: od: if curr = 0 then return true: fi: return false: end: GoodBrother := proc(L) local i, set, curr, ans: for i to nops(L) do set := [op(i .. nops(L), L), op(1 .. i - 1, L)]: if check(set) = true then return set: fi: od: return L: end: # Question 6 # For there to be a repeat cyclic shift, there must have been a repeated word w within L k times, where k is greater than 1. # This would make the sum of L to be k times the sum of w. # If there's not a repeated word w within L k times, where k > 1, then we'd have all distinct cycles as # the letter that breaks the pattern would cycle around, making each cyclic shift unique. # Thus, the only time for a word to have all distinct cyclic shifts is when k = 1. # For sum(w) * k to equal 1, then k must equal 1 and sum(w) must equal 1 as there is no other combination of sum(w) and k where the product is 1. If k>1, # then sum(w) must be a fraction value. However, this is not possible. w only contains 1 and -1, which are both whole numbers. Thus, k and sum(w) must be 1 # for L to have a sum of 1. Then, since k=1 is the only option for L summing up to 1, then we know that all the cyclic shifts # must be distinct (no repeat words within L) when the word adds up to 1.