#OK to post homework #Ryan Badi, January 21, 2024, Assignment 1 # Encodes a list by shifting each element of an alphabet. Returns FAIL on failure. # P_ is the list to be encoded. # A_ is the alphabet that the elements of P_ should belong to. # k_ is the number of times each element of P_ should be shifted. CCg := proc(P_, A_, k_) local T_, i_, s_: for i_ from 1 to nops(P_) do: if not has(A_, P_[i_]) then return FAIL fi: od: for i_ from 1 to nops(A_) do: T_[A_[i_]] := A_[((i_ + k_ - 1) mod nops(A_)) + 1]: od: return [seq(T_[P_[i_]], i_ = 1 .. nops(P_))]: end: # Generates T[i, j], a frequency list for a list of i-letter English words at the j-th letter. # eng_ is the list of English words to be chcked against. # A_ is the alphabet that the elements of each word should belong to. # i_ is the length of the English word to be concerned with. # j_ is the corresponding letter in the word to be concerned with. GenerateTij := proc(eng_, A_, i_, j_) local S_, Tij_, k_: for k_ from 1 to 26 do: S_[A_[k_]] := 0: od: for k_ from 1 to nops(ENG()[i_]) do: S_[eng_[i_][k_][j_]] := S_[eng_[i_][k_][j_]] + 1: od: for k_ from 1 to 26 do: Tij_[k_] := S_[A_[k_]]: od: return Tij_: end: # Generates all T[i, j] where 3 <= i <= 10, 1 <= j <= i. # eng_ is the list of English words to be chcked against. # A_ is the alphabet that the elements of each word should belong to. GenerateAllT := proc(eng_, A_) local T_, i_, j_: for i_ from 3 to 10 do: for j_ from 1 to i_ do: T_[i_, j_] := GenerateTij(eng_, A_, i_, j_): od: od: return T_: end: # Generates F, a frequency list for each letter and a list of English words of varying length. # eng_ is the list of English words to be chcked against. # A_ is the alphabet that the elements of each word should belong to. GenerateF := proc(eng_, A_) local F_, S_, i_, j_, k_: for k_ from 1 to 26 do: S_[A_[k_]] := 0: od: for i_ from 3 to 10 do: for k_ from 1 to nops(ENG()[i_]) do: for j_ from 1 to i_ do: S_[eng_[i_][k_][j_]] := S_[eng_[i_][k_][j_]] + 1: od: od: od: for k_ from 1 to 26 do: F_[k_] := S_[A_[k_]]: od: return F_: end: # Create a default alphabet. A_ := [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]: # Generate all T[i, j] and F. T := GenerateAllT(ENG(), A_): F := GenerateF(ENG(), A_): # Print the results of Task 3 using [r, y, a, n] as an example to be shifted twice forward and back. printf("Result of CCg([r, y, a, n], A_, 2) where A_ is the lowercase alphabet: %a\n", CCg([r, y, a, n], A_, 2)): printf("Reverse the above result with CCg(CCg([r, y, a, n], A_, 2), A_, -2): %a\n\n", CCg(CCg([r,y,a,n], A_, 2), A_, -2)): # Print the results of Task 4. printf("The difference between each letter's frequency as a result of methods 1 (the sum of all T[i, j][i1]) and 2 (F[i1]) is: \n"): for c_ from 1 to 26 do: sum_ := 0: for i_ from 3 to 10 do: for j_ to i_ do: sum_ := sum_ + T[i_, j_][c_]: od: od: printf("%a: %a\t", A_[c_], sum_ - F[c_]): od: printf("\n"): printf("As expected, the two methods produce the same result.\n"):