9
\$\begingroup\$

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 , so the shortest code wins!
  • Have fun!
\$\endgroup\$
  • \$\begingroup\$ How many points for a decryptor? \$\endgroup\$ – Digital Trauma Feb 20 '14 at 3:45
  • \$\begingroup\$ This is a fun challenge, but the encryption method here would make a cryptographer weep. Not only is it a single-substitution cipher, but it has the audacity to introduce a recognizable and exploitable bias: the more common a ciphertext letter is in usage of the enciphered language, the more likely the plaintext lies near the beginning of the alphabet. It out-Caesar's Caesar. ;-) \$\endgroup\$ – Jonathan Van Matre Feb 20 '14 at 4:21
  • \$\begingroup\$ @TheDoctor great idea, but you have made one little error... the ciphered text should start with the respective ciphered letters according to your algorithm! :) First sentence starts with M now, second sentence with P and so on... \$\endgroup\$ – WallyWest Feb 20 '14 at 10:15
  • \$\begingroup\$ @DigitalTrauma - let's say 30 points for a decryptor \$\endgroup\$ – TheDoctor Feb 20 '14 at 13:48
  • \$\begingroup\$ 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\$ – e-sushi Mar 2 '14 at 12:38

13 Answers 13

3
\$\begingroup\$

GolfScript, 39 characters

:I{97,26,{97+}%{[.32-]I\-,}$+'{|}~'+=}%

Online version for testing. Note that the sorting is not defined if multiple characters have the same count in the input string.

Example output

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 wlm qaeglhge prfm. Vlfhrijmjd svpnjl jmhgrslf denpe e tgrpnrmme. Qjrfbjl qvghhrhvg, grfjf fjfsrqrh qlmmlphlfbjl hgrfhrbjl, vgsr mvgld essjdfep prfr, fslmlgrfbjl wrwlgge hjgqrf dlhjf frh edlh feqrlp. Fjfsl tesrmrfrf ured hjgqrf, pls mvivghrf ujr imepurh pls. Vlfhrijmjd es jgpe jh mesjf svddvuv fvmmrsrhjurp pls pvp wlmrh. Njmme sjgfjf jgpe fld, eh hrpsrujph fld dvmlfhrl wlm. Njmmed tgrpnrmme ephl lj uvmvg irilpujd, qvfjlgl resjmrf pjps mesrpre. Slu es qglhrjd lfh, wlm fslmlgrfbjl prfm. Paeflmmjf mvivghrf uvmvg fvuemlf feqrlp dvmmrf alpuglgrh. Iphlnlg fslmlgrfbjl hldqvg hlmmjf, wrwlgge wegrjf plbjl dehhrf rp. Iphlnlg qvghe wlfhrijmjd prfm, lh tljnreh hvghvg hrpsrujph wlm. Alplep urnprffrd lmlrtlpu tejsrijf. Mvgir pls plbjl wlm ephl qjmwrpeg dvmmrf lj rp rqfjd.

\$\endgroup\$
  • \$\begingroup\$ Darn golfscript! it always wins! \$\endgroup\$ – TheDoctor Feb 28 '14 at 23:20
3
\$\begingroup\$

Bash/coreutils, 91 chars

tr a-z `echo {a..z} $1|fold -w1|grep '[a-z]'|sort|uniq -c|sort -rn|awk '{printf $2}'`<<<$1

Save as cipher.sh, chmod +x it and run:

$ ./cipher.sh "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."
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 jlm qaeglhge prfm. Vlfhrijmjd svpnjl jmhgrslf denpe e tgrpnrmme. Qjrfbjl qvghhrhvg, grfjf fjfsrqrh qlmmlphlfbjl hgrfhrbjl, vgsr mvgld essjdfep prfr, fslmlgrfbjl jrjlgge hjgqrf dlhjf frh edlh feqrlp. Fjfsl tesrmrfrf ured hjgqrf, pls mvivghrf ujr imepurh pls. Vlfhrijmjd es jgpe jh mesjf svddvuv fvmmrsrhjurp pls pvp jlmrh. Njmme sjgfjf jgpe fld, eh hrpsrujph fld dvmlfhrl jlm. Njmmed tgrpnrmme ephl lj uvmvg irilpujd, qvfjlgl resjmrf pjps mesrpre. Slu es qglhrjd lfh, jlm fslmlgrfbjl prfm. Paeflmmjf mvivghrf uvmvg fvuemlf feqrlp dvmmrf alpuglgrh. Iphlnlg fslmlgrfbjl hldqvg hlmmjf, jrjlgge jegrjf plbjl dehhrf rp. Iphlnlg qvghe jlfhrijmjd prfm, lh tljnreh hvghvg hrpsrujph jlm. Alplep urnprffrd lmlrtlpu tejsrijf. Mvgir pls plbjl jlm ephl qjmjrpeg dvmmrf lj rp rqfjd.
$ 
\$\endgroup\$
2
\$\begingroup\$

Ruby, 104 92 91 characters

Saved quite a few characters thanks to @Chron

f=->(s){m=[*?a..?z];l=m.map{|x|[-s.downcase.count(x),x]};s.tr(m*'',l.sort.transpose[1]*'')}

Online Version here. Sorting of characters with the same count is not defined, as mentioned in another answer. With the input "asdf", each answer has another output so far.

In other words: all answers have the same behaviour (thus represent a decodable encoding) when the input contains the whole alphabet with each letter having a unique count.

\$\endgroup\$
  • \$\begingroup\$ A couple of small improvements: ("a".."z").to_a can be [*?a..?z], m.join can be m*'' and def f(s)...end can be f=->s{...} \$\endgroup\$ – Paul Prestidge Feb 25 '14 at 22:38
1
\$\begingroup\$

Mathematica 171

f@m_:=StringReplace[m,Thread[(CharacterRange["a",
FromCharacterCode[96+Length@(l=Reverse@SortBy[Tally[Select[Characters@m,
(LetterQ@#\[And]LowerCaseQ@#)&]],Last][[All,1]])]])->l ]]

Assuming t is the Lorem ipsum text.

f[t]

"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 vlm qaeglhge prfm.Vlfhrijmjd svpnjl jmhgrslf denpe e tgrpnrmme.Qjrfbjl qvghhrhvg,grfjf fjfsrqrh qlmmlphlfbjl hgrfhrbjl,vgsr mvgld essjdfep prfr,fslmlgrfbjl vrvlgge hjgqrf dlhjf frh edlh feqrlp.Fjfsl tesrmrfrf ured hjgqrf,pls mvivghrf ujr imepurh pls.Vlfhrijmjd es jgpe jh mesjf svddvuv fvmmrsrhjurp pls pvp vlmrh.Njmme sjgfjf jgpe fld,eh hrpsrujph fld dvmlfhrl vlm.Njmmed tgrpnrmme ephl lj uvmvg irilpujd,qvfjlgl resjmrf pjps mesrpre.Slu es qglhrjd lfh,vlm fslmlgrfbjl prfm.Paeflmmjf mvivghrf uvmvg fvuemlf feqrlp dvmmrf alpuglgrh.Iphlnlg fslmlgrfbjl hldqvg hlmmjf,vrvlgge vegrjf plbjl dehhrf rp.Iphlnlg qvghe vlfhrijmjd prfm,lh tljnreh hvghvg hrpsrujph vlm.Alplep urnprffrd lmlrtlpu tejsrijf.Mvgir pls plbjl vlm ephl qjmvrpeg dvmmrf lj rp rqfjd."

The replacement rules generated by Thread…-> l were:

{"a" -> "e", "b" -> "i", "c" -> "s", "d" -> "u", "e" -> "l", "f" -> "t", "g" -> "n", "h" -> "a", "i" -> "r", "j" -> "o", "k" -> "c", "l" -> "m", "m" -> "d", "n" -> "p", "o" -> "v", "p" -> "q", "q" -> "b", "r" -> "g", "s" -> "f", "t" -> "h", "u" -> "j"}

\$\endgroup\$
1
\$\begingroup\$

K, 43

{x^(b!b^26$>#:'=a@&(a:_x)in b:"c"$97+!26)x}
\$\endgroup\$
1
\$\begingroup\$

C# 386

using System.Collections.Generic;using System.Linq;namespace N{class P{static void Main(string[]a){char[] f="abcdefghijklmnopqrstuvwxyz".ToCharArray();Dictionary<char,int>l=new Dictionary<char,int>();foreach (char c in f) l.Add(c, "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.".ToLower().Trim().Count(v => v == c));foreach (KeyValuePair<char, int> i in l.OrderByDescending(p => p.Value))System.Console.Write(i.Key +""+i.Value);}}}

Uncompressed.

using System.Collections.Generic;
using System.Linq;
namespace N {
    class P {
        static void Main(string[]a){
            char[] f="abcdefghijklmnopqrstuvwxyz".ToCharArray();
            Dictionary<char,int>l=new Dictionary<char,int>();
            foreach (char c in f) 
                l.Add(c, "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.".ToLower().Trim().Count(v => v == c));
            foreach (KeyValuePair<char, int> i in l.OrderByDescending(p => p.Value))
                System.Console.Write(i.Key +""+i.Value);
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ Don't include the input string in your char count \$\endgroup\$ – TheDoctor Feb 20 '14 at 21:45
  • \$\begingroup\$ Did you even run this? Output: e93i89s71u65l61t59n56a49r47o37c34m31d22p20v15q12b11g10f9h3j1k0w0x0y0z0. And it doesn't come even close to the challenge. \$\endgroup\$ – RobIII Feb 21 '14 at 1:18
0
\$\begingroup\$

PHP, 151

(with setting short_open_tag = On)

<?$c=array_slice(count_chars(strtolower($s=$argv[1])),97,26,1);arsort($c);echo strtr($s,array_combine(range('a','z'),array_map('chr',array_keys($c))));

This expects the text as the first argument to the script. Like so:

php cypher.php "Lorem ipsum [...]"
\$\endgroup\$
0
\$\begingroup\$

R, 137

l=letters;s=strsplit(readline(),"")[[1]];g=grep("[a-z]",s);s[g]=names(sort(table(factor(tolower(s),l)),d=T))[match(s[g],l)];cat(s,sep="")

Output (based on example in question):

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.

\$\endgroup\$
0
\$\begingroup\$

Smalltalk, 138

input in s:

m:=(s select:[:c|cisLetter])asLowercase asBag sortedCounts map:#value.
i:=($ato:$z).m:=m,(i copyWithoutAll:m).
s copyTransliterating:i to:m

the decoder is:

s copyTransliterating:m to:i

but as (if I understand correctly) I may not reuse "i" and "m", I'll golf without it. Code above has two additional CRs inserted for readability, which were uncounted in the char count.

\$\endgroup\$
0
\$\begingroup\$

Clojure, 135

(Assuming the input text is contained in the var s)

(let[a(map char(range 97 123))m(->> s .toLowerCase frequencies(sort-by val >)keys(filter(set a))(zipmap a))](apply str(map #(m % %)s)))
\$\endgroup\$
0
\$\begingroup\$

Python 2.7 (147)

Not the shortest code at all but as Python is not yet represented and as I see "I love python!" in the problem setting, here I go,

import sys;s=sys.argv[1];a=map(chr,range(97,123));print"".join([sorted(a,key=lambda x:-s.lower().count(x))[ord(c)-97]if c in a else c for c in s])

Expects the input string to be passed via the command line. (number of characters are reduced to 122 if the input string was magically inserted into the variable "s")

a=map(chr,range(97,123));print"".join([sorted(a,key=lambda x:-s.lower().count(x))[ord(c)-97]if c in a else c for c in s])

Ouput

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.

\$\endgroup\$
  • \$\begingroup\$ Yep, i did all that in 20 lines of python, but it's way longer than yours \$\endgroup\$ – TheDoctor Feb 20 '14 at 21:44
0
\$\begingroup\$

Perl, 84

$c{$_}++for lc($_=<>)=~/./g;@h{a..z}=sort{$c{$b}-$c{$a}}a..z;s/[a-z]/$h{$&}/ge;print

.

perl cipher.pl

.. input skipped ..

Lvgld rqfjd uvmvg frh edlh, svpflshlhjg eurqrfsrpn lmrh. Njps flu ujr eh prfr lj rfdvu qlmmlphlfbjl es flu lprd. Njmmed ejshvg ojfhv bjrf ured tejsrijf, lj tgrpn rmme lfh qvghhrhvg. Plmmlphlfbjl klm qaeglhge prfm. Vlfhrijmjd svpnjl jmhgrslf d enpe e tgrpnrmme. Qjrfbjl qvghhrhvg, grfjf fjfsrqrh qlmmlphlfbjl hgrfhrbjl, vgsr mvgld essjdfep prfr, fslmlgrfbjl krklgge hjgqrf dlhjf frh edlh feqrlp. Fjfsl te srmrfrf ured hjgqrf, pls mvivghrf ujr imepurh pls. Vlfhrijmjd es jgpe jh mesjf s vddvuv fvmmrsrhjurp pls pvp klmrh. Njmme sjgfjf jgpe fld, eh hrpsrujph fld dvmlf hrl klm. Njmmed tgrpnrmme ephl lj uvmvg irilpujd, qvfjlgl resjmrf pjps mesrpre. Slu es qglhrjd lfh, klm fslmlgrfbjl prfm. Paeflmmjf mvivghrf uvmvg fvuemlf feqrl p dvmmrf alpuglgrh. Iphlnlg fslmlgrfbjl hldqvg hlmmjf, krklgge kegrjf plbjl dehh rf rp. Iphlnlg qvghe klfhrijmjd prfm, lh tljnreh hvghvg hrpsrujph klm. Alplep ur nprffrd lmlrtlpu tejsrijf. Mvgir pls plbjl klm ephl qjmkrpeg dvmmrf lj rp rqfjd.

P.S. Was it a joke, about decipher? Or should I claim 30 bonus for proving it impossible? Is aab deciphered to aab or bba? Or babaca, was it cacaba or ababcb in original, or babaca itself, literally?

\$\endgroup\$
0
\$\begingroup\$

C# – 393 bytes

string e(string i){char[] f="abcdefghijklmnopqrstuvwxyz".ToCharArray();Dictionary<char,int>l=new Dictionary<char,int>();foreach (char c in f) l.Add(c, i.ToLower().Count(v => v == c));var w = (l.OrderByDescending(p => p.Value)).ToDictionary(q=>q.Key, y=>y.Value);var z = w.Keys.ToList();string r = "";foreach(char c in i) {if((int)c <=97 || (int)c>=122)r+=c;else r += z[((int)c-97)];}return r;}

The extended version of @PauloHDSousa’s answer

\$\endgroup\$
  • \$\begingroup\$ Why do you have a lot of extra spaces in the answer? \$\endgroup\$ – Qwertiy Jan 14 '16 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.