Problem:
You must make a program that does the following:
- takes a large string of lowercase text, and counts all the occurrences of each letter.
- then you put the letters in order from greatest to least occurences.
- then you take that list and turns it into an encoder/decoder for the text.
- then encodes the text with that cipher.
Hard to understand? See this example:
Example:
Input text:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc sed dui at nisi euismod pellentesque ac sed enim. Nullam auctor justo quis diam faucibus, eu fringilla est porttitor. Pellentesque vel pharetra nisl. Vestibulum congue ultrices magna a fringilla. Quisque porttitor, risus suscipit pellentesque tristique, orci lorem accumsan nisi, scelerisque viverra turpis metus sit amet sapien. Fusce facilisis diam turpis, nec lobortis dui blandit nec. Vestibulum ac urna ut lacus commodo sollicitudin nec non velit. Nulla cursus urna sem, at tincidunt sem molestie vel. Nullam fringilla ante eu dolor bibendum, posuere iaculis nunc lacinia. Sed ac pretium est, vel scelerisque nisl. Phasellus lobortis dolor sodales sapien mollis hendrerit. Integer scelerisque tempor tellus, viverra varius neque mattis in. Integer porta vestibulum nisl, et feugiat tortor tincidunt vel. Aenean dignissim eleifend faucibus. Morbi nec neque vel ante pulvinar mollis eu in ipsum.
Convert to lowercase.
Char count (per letter. spaces and punctuation ignored):
[('a', 49), ('b', 11), ('c', 34), ('d', 22), ('e', 93), ('f', 9), ('g', 10), ('h', 3), ('i', 89), ('j', 1), ('k', 0), ('l', 61), ('m', 31), ('n', 56), ('o', 37), ('p', 20), ('q', 12), ('r', 47), ('s', 71), ('t', 59), ('u', 65), ('v', 15), ('w', 0), ('x', 0), ('y', 0), ('z', 0)]
Ordered char count:
[('e', 93), ('i', 89), ('s', 71), ('u', 65), ('l', 61), ('t', 59), ('n', 56), ('a', 49), ('r', 47), ('o', 37), ('c', 34), ('m', 31), ('d', 22), ('p', 20), ('v', 15), ('q', 12), ('b', 11), ('g', 10), ('f', 9), ('h', 3), ('j', 1), ('k', 0), ('w', 0), ('x', 0), ('y', 0), ('z', 0)]
Then create a lookup table using the original and sorted lists:
abcdefghijklmnopqrstuvwxyz
||||||||||||||||||||||||||
eisultnarocmdpvqbgfhjkwxyz
Python dictionary:
{'o': 'v', 'n': 'p', 'm': 'd', 'l': 'm', 'k': 'c', 'j': 'o', 'i': 'r', 'h': 'a', 'g': 'n', 'f': 't', 'e': 'l', 'd': 'u', 'c': 's', 'b': 'i', 'a': 'e', 'z': 'z', 'y': 'y', 'x': 'x', 'w': 'w', 'v': 'k', 'u': 'j', 't': 'h', 's': 'f', 'r': 'g', 'q': 'b', 'p': 'q'}
And now encode the original text with this lookup table:
'Lvgld rqfjd uvmvg frh edlh, svpflshlhjg eurqrfsrpn lmrh. Njps flu ujr eh prfr ljrfdvu qlmmlphlfbjl es flu lprd. Njmmed ejshvg ojfhv bjrf ured tejsrijf, lj tgrpnrmme lfh qvghhrhvg. Plmmlphlfbjl klm qaeglhge prfm. Vlfhrijmjd svpnjl jmhgrslf denpe e tgrpnrmme. Qjrfbjl qvghhrhvg, grfjf fjfsrqrh qlmmlphlfbjl hgrfhrbjl, vgsr mvgld essjdfep prfr, fslmlgrfbjl krklgge hjgqrf dlhjf frh edlh feqrlp. Fjfsl tesrmrfrf ured hjgqrf, pls mvivghrf ujr imepurh pls. Vlfhrijmjd es jgpe jh mesjf svddvuv fvmmrsrhjurp pls pvp klmrh. Njmme sjgfjf jgpe fld, eh hrpsrujph fld dvmlfhrl klm. Njmmed tgrpnrmme ephl lj uvmvg irilpujd, qvfjlgl resjmrf pjps mesrpre. Slu es qglhrjd lfh, klm fslmlgrfbjl prfm. Paeflmmjf mvivghrf uvmvg fvuemlf feqrlp dvmmrf alpuglgrh. Iphlnlg fslmlgrfbjl hldqvg hlmmjf, krklgge kegrjf plbjl dehhrf rp. Iphlnlg qvghe klfhrijmjd prfm, lh tljnreh hvghvg hrpsrujph klm. Alplep urnprffrd lmlrtlpu tejsrijf. Mvgir pls plbjl klm ephl qjmkrpeg dvmmrf lj rp rqfjd.'
I love python!
Rules:
- Your program will accept a string and output one.
- Convert all input to lowercase before doing anything
- I don't care how you do the list sorting, but only count lowercase letters
- Bonus points (-30) for making a decryptor (no copying the decryption list, do it from scratch
- This is code-golf, so the shortest code wins!
- Have fun!
Bonus points (-30) for making a decryptor (no copying the decryption list, do it from scratch
? THAT’S IMPOSSIBLE! You would need to have the key (your “decryption list“ aka “lookup table”) to be able to do that. Just taking a ciphertext and expecting anyone to provide a decryptor that transforms it back into plaintext without the use of any key is as if you’re asking us to post a question to StackOverflow without typing any letters or numbers. Information theory already tells us that you can’t gain information out of nowhere… so your substitution cipher needs that lookup table (aka key). \$\endgroup\$