#OK to post homework #Natalya Ter-Saakov, Jan 21, 2024, Assignment 1 read "C1.txt": #Problem 3 #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]: #CCg(P,A,key): Inputs a message, a list of characters, P, an alphabet A an integer key from 0 to 26 outputs a shifted incrypted message. #Note: This procedure will not work as intended if A contains 'P', 'A' or 'T' CCg:=proc(P,A,key) local T,ind: for ind from 1 to nops(P) do if not (P[ind] in A) then return FAIL: fi: od: for ind from 0 to nops(A)-1 do T[A[ind+1]]:=A[(ind+key) mod nops(A) +1]: od: [seq(T[P[ind]],ind=1..nops(P))]: end: #Problem 4 read "ENGLISH.txt": LocFreq:=proc(startlen, endlen) local len, loc, letter, A, count, word, T, sum: 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]: for letter in A do for len from startlen to endlen do for loc from 1 to len do T[len, loc][letter]:=0: od: od: od: for len from startlen to endlen do for word in ENG()[len] do for loc from 1 to len do T[len, loc][word[loc]]+=1: od: od: od: for len from startlen to endlen do for loc from 1 to len do T[len, loc]:=[seq(evalf(T[len, loc][A[letter]]/nops(ENG()[len])), letter=1..26)]: print(len, loc, T[len, loc]): od: od: end: #This is not the most efficient calculation, but it works. TotalFreq:=proc(startlen, endlen) local len, letter, loc, A, count, word, F, sum: 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]: F:=[]: for letter from 1 to 26 do count:=0: for len from startlen to endlen do for word in ENG()[len] do for loc from 1 to len do if word[loc]=A[letter] then count+=1: fi: od: od: od: F:=[op(F),count]: od: sum:=add(F): [seq(evalf(F[letter]/sum), letter=1..26)]: end: print(TotalFreq(3,10)):