# OK to post # RDB, 2024-01-21, HW1 # 1. Exempt. # 2. I've read through RSA details before, but admittedly I don't remember them # that well! I promise to remember them well soon. # 3. CCg := proc(msg, alphabet, k) chars := {op(msg)}: dict := table(): for c in chars do idx := ListTools[Search](c, alphabet): if idx = 0 then error("message contains ", c); return FAIL: fi: dict[c] := idx: od: get_shift := x -> (dict[x] + k - 1) mod nops(alphabet) + 1: [seq(alphabet[get_shift(x)], x in msg)]: end: # To check. alph := [seq(0..9)]; outs := [seq(CCg([0], alph, k), k=0..10)]; ins := [seq(CCg(outs[k + 1], alph, -k), k=0..10)]; # 4. # I'd like to raise the objection that we should really be working with strings # here, e.g. "a", "i", and so on. If I happened to use the variable `k` above, # then we would get nonsense when reading ENGLISH.txt! This would make the # below code much easier as well, because we could use more of StringTools. # Nontheless, I've done this using the current variables-as-letters format. makeFreqLists := proc(list_of_words) res := table(): # Make a 0'd out table. for i1 from 3 to 10 do for j1 from 1 to i1 do res[i1, j1] := [0 $ 26]: od: od: denoms := [seq(nops(length_words), length_words in list_of_words)]: F := [0 $ 26]: for len from 3 to 10 do for word in list_of_words[len] do for pos from 1 to len do # Going to cheat here and use strings. # lowercase letters are 97-122 in ASCII, and StringTools[Ord] # converts letters to ASCII values. sym := word[pos]: idx := StringTools[Ord](convert(sym, string)) - 96: res[len, pos][idx] += 1 / denoms[len]: F[idx] += 1: od: od: od: n_letters := add(k1 * denoms[k1], k1=3..10): op(res), F / n_letters, F: end: read "ENGLISH.txt": A, B, C1 := makeFreqLists(ENG()): # Lots of data here. # I'm most interested in knowing when "e" is the most common letter (as it is # across all letters in all positions). e_winners := []: e_losers := []: for len from 3 to 10 do for pos from 1 to len do if max[index](A[len, pos]) = 5 then e_winners := [op(e_winners), [len, pos]]: else e_losers := [op(e_losers), [len, pos]]: fi: od: od: