[Question]
  [
Here are the letters of the English alphabet in order by frequency:
```
e t a o i n s h r d l c u m w f g y p b v k j x q z
```
That is, `e` is the most frequently used letter, and `z` is the least common. (Data from [Wikipedia](http://en.wikipedia.org/wiki/Letter_frequency).)
Your challenge is to take some ROT-n'd text, such as:
```
ocdndnvqzmtnzxmzohznnvbzocvodnqzmtnzxpmzviynvaz
```
This is the text "thisisaverysecretmessagethatisverysecureandsafe" that is "encrypted" through ROT-21 (half of 42). Your program, using the frequency table above, should be able to determine by how much each character was rotated and the original text.
(If you are not familiar with ROT-n, it is essentially shifting each character by `n`. For example, in ROT-2, `a -> c, b -> d, ..., x -> z, y -> a, z -> b`.)
How, you ask? The (very naive) algorithm you must use is:
* for each `n` from `0` to `25` inclusive, apply ROT-`-n` to the input string. (Negative `n` because we want to *reverse* the encryption. ROT-`-n` is equivalent to ROT-`26-n`, if that's any easier.)
* convert each input string to a number by adding up the relative frequencies of the characters. `e` is `0`, `t` is `1`, `a` is `2`, etc. For example, the corresponding number for the string `"hello"` is 7 + 0 + 10 + 10 + 3 = 30.
* find the string that has the lowest corresponding number.
* output that string and its corresponding `n`.
Rules:
* input can be anywhere reasonable (STDIN, function arguments, from a file, etc.), and so can output (STDOUT, function return value, to a file, etc.)
* you may use a different algorithm, as long as it always produces identical results. For example, having `z` be 0 and `e` be 25 and choosing the highest number is also okay.
* if two strings have identical scores, you can choose to output either one (or both) of them. This is an edge case and you do not have to account for it.
* this is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code in bytes will win!
Test cases:
Input: `ocdndnvqzmtnzxmzohznnvbzocvodnqzmtnzxpmzviynvaz`  
Output: `21 thisisaverysecretmessagethatisverysecureandsafe`
Input: `pmttwxmwxtmwnxzwoziuuqvoxchhtmakwlmowtnabiksmfkpivom`  
Output: `8 hellopeopleofprogrammingpuzzlescodegolfstackexchange`
Input: `ftueimeqzodkbfqpiuftdaffiqxhqeaufygefnqbqdrqofxkemrq`  
Output: `12 thiswasencryptedwithrottwelvesoitmustbeperfectlysafe`
Input: `jgtgkuvjghkpcnvguvecugvjcvaqwowuvfgetarv`  
Output: `2 hereisthefinaltestcasethatyoumustdecrypt`
In case you were wondering, [here is a JSFiddle of the JavaScript test code I wrote, which successfully decrypted all the test cases I threw at it.](http://jsfiddle.net/qLZ7W/2/)
  
[Answer]
## Haskell - ~~192~~ 175
```
f y=sum.map(\x->length.fst$break(==x)y)
main=interact(\s->snd$minimum$[(f"etaoinshrdlcumwfgypbvkjxqz"r,show(26-n)++" "++r)|n<-[0..25],let r=map(\x->([x..'z']++['a'..])!!n)s])
```
Running
```
% ./rot-n <<< "pmttwxmwxtmwnxzwoziuuqvoxchhtmakwlmowtnabiksmfkpivom"
8 hellopeopleofprogrammingpuzzlescodegolfstackexchange
```
[Answer]
# GolfScript, ~~112~~ ~~108~~ ~~102~~ 100 characters
```
{{}/]{97-}%}:b~:|;"etaoinshrdlcumwfgypbvkjxqz"b:f,:&,{:x[|{&x-+&%f?}%{+}*\]}%$0=1=:x|{&x-+&%97+}%''+
```
I'm not happy about the repetition with the re-decrypting at the end, but meh.
Ungolfed (if that makes any sense :P) and slightly older version:
```
# store input IDs (a = 0, b = 1, etc.) in s
[{}/]{97-}%:s;
# store frequency data IDs in f (blah, repetition)
"etaoinshrdlcumwfgypbvkjxqz"[{}/]{97-}%:f
# for each number from 0 to 26 (length of previous string left unpopped)...
,,{
  # the number is x
  :x;
  # return an array of...
  [
    # the score
    s{x 26\-+26%f?}%{+}*
    # and the n
    x
  ]
}%
# use $ort to find the n to output
$0=1=:x
# get the string that the n corresponded to (blah, more repetition)
s{x 26\-+26%97+}%''+
```
[Answer]
# GolfScript - 87
The cheat here is to build every rotation simultaneously. Since we need to loop over every ROT then every character, let's just loop over every character, slice the whole alphabet, then zip it up. From there proceed as expected: count the score for each ROT and choose the minimum.
Extra golfed:
```
{97- 26,{97+}%.+''+>}/]{27<}%zip:d{{"etaoinshrdlcumwfgypbvkjxqz"?}%{+}*}%.$0=?.26\-\d=
```
Only a little golfed:
```
# the alphabet, and by frequency
26,{97+}%.+''+:a;
"etaoinshrdlcumwfgypbvkjxqz":f;
# build evey ROT decryption
{97-a>}/]{27<}%zip:d
# calculate likelihood
{{f?}%{+}*}%.
# find min
$0=
# output rotation factor and decryption
?.26\-\d=
```
[Answer]
## JavaScript (205)
```
f='zqxjkvbpygfwmucldrhsnioate';a='abcdefghijklmnopqrstuvwxyz';for(s=prompt(o=m=n=0)
,i=27;i--;w>m&&(m=w,n=i,o=u))for(u='',w=c=0;c<s.length;w+=f.indexOf(x))u+=x=(a+a)[a
.indexOf(s[c++])+i];alert((26-n)+' '+o)
```
I think it can still be golfed a bit more, so suggestions welcome!
Some notes to help understand the solution
* `m`, `n`, and `o` track the highest score.
* `u` and `w` track the character and value result, respectively for the current `i`
* `(a+a)` helps prevent overflow when wrapping around past `z`, and is shorter than doing `%26`
* I have the frequency in reverse order so I can search max instead of min.
Proof: <http://jsfiddle.net/J9ZyV/5/>
[Answer]
## C# + Linq - ~~273~~ 264
As a function which takes the input string and returns the decoded string & offset (as per requirements):
```
static Tuple<string,int> d(string s){var r=Enumerable.Range(0,25).Select(i=>string.Concat(from c in s select (char)((c-97+i)%26+97))).OrderBy(x=>(from c in x select "etaoinshrdlcumwfgypbvkjxqz".IndexOf(c)).Sum()).First();return Tuple.Create(r,(s[0]-r[0]+26)%26);}
```
Ungolfed with comments:
```
static Tuple<string,int> d(string s)
{
    var r=Enumerable.Range(0,25)                                               // for every possible offset i
          .Select(i=>string.Concat(from c in s select (char)((c-97+i)%26+97))) // calculate rot_i(input string)
          .OrderBy(                                                            // order these by their score
              x=>(
              from c in x select "etaoinshrdlcumwfgypbvkjxqz".IndexOf(c)       // lookup frequency of each character
              ).Sum()                                                          // and sum each frequency to get the score
           ).First();                                                          // get the first one (lowest score)
    return Tuple.Create(r,(s[0]-r[0]+26)%26);                                  // compute offset and return results
}
```
---
Little test driver (remember to compile referencing `System.Core` for Linq):
```
using System;
using System.Linq;
namespace codegolf
{
    class Program
    {
        static Tuple<string,int> d(string s){var r=Enumerable.Range(0,25).Select(i=>string.Concat(from c in s select (char)((c-97+i)%26+97))).OrderBy(x=>(from c in x select "etaoinshrdlcumwfgypbvkjxqz".IndexOf(c)).Sum()).First();return Tuple.Create(r,(s[0]-r[0]+26)%26);}
        static void Main(string[] args)
        {
            while (true)
            {
                var input = Console.ReadLine();
                if (input == null) break;
                var retval = d(input);
                Console.WriteLine(String.Format("{0} {1}", retval.Item2, retval.Item1));
            }
        }
    }
}
```
Giving:
```
$ mcs /reference:System.Core.dll main.cs && mono ./main.exe
ocdndnvqzmtnzxmzohznnvbzocvodnqzmtnzxpmzviynvaz
21 thisisaverysecretmessagethatisverysecureandsafe
pmttwxmwxtmwnxzwoziuuqvoxchhtmakwlmowtnabiksmfkpivom
8 hellopeopleofprogrammingpuzzlescodegolfstackexchange
ftueimeqzodkbfqpiuftdaffiqxhqeaufygefnqbqdrqofxkemrq
12 thiswasencryptedwithrottwelvesoitmustbeperfectlysafe
jgtgkuvjghkpcnvguvecugvjcvaqwowuvfgetarv
2 hereisthefinaltestcasethatyoumustdecrypt
thisisaverysecretmessagethatisverysecureandsafe
0 thisisaverysecretmessagethatisverysecureandsafe
```
[Answer]
# dg - 137 130 129 128 bytes
```
f=t->min key:(t->sum$map 'etaoinshrdlcumwfgypbvkjxqz'.index$snd t)$map(n->n,''.join$map(c->chr$((ord c + 7-n)%26+ 97))t)(0..26)
```
Examples:
```
>>> f 'ocdndnvqzmtnzxmzohznnvbzocvodnqzmtnzxpmzviynvaz'
(21, 'thisisaverysecretmessagethatisverysecureandsafe')
>>> f 'pmttwxmwxtmwnxzwoziuuqvoxchhtmakwlmowtnabiksmfkpivom'
(8, 'hellopeopleofprogrammingpuzzlescodegolfstackexchange')
>>> f 'ftueimeqzodkbfqpiuftdaffiqxhqeaufygefnqbqdrqofxkemrq'
(12, 'thiswasencryptedwithrottwelvesoitmustbeperfectlysafe')
>>> f 'jgtgkuvjghkpcnvguvecugvjcvaqwowuvfgetarv'
(2, 'hereisthefinaltestcasethatyoumustdecrypt')
```
Ungolfed code:
```
func = t ->
  #: Compute the score of the text `t` with respect to the frequency table.
  #: score :: (int, str) -> int
  score = t ->
    sum $ map 'etaoinshrdlcumwfgypbvkjxqz'.index $ snd t
  #: Compute rot-n of the string `t`. Return the offset and the shifted text.
  #: rot :: int -> (int, str)
  rot = n ->
    n, ''.join $ map (c->chr $ ((ord c + 7 - n) % 26 + 97)) t
  # return the minimum (computed by `score`) amongst all shifted messages
  min key: score $ map rot (0..26)
```
[Answer]
# J - 92 char
A bit of an ugly duckling, but it works. Outputs the number and then the string, on two lines.
```
(26|](-1!:2&2&.":)(/:'ctljapqhewvknfdsyigbmuoxrz')+/@i."1(i.<./@:)26|(i.-27)+/])&.(_97+3&u:)
```
If you want them to be on the same line, space separated, this only goes up to **93 char**, but takes an uglier route.
```
((":@],[:u:32,97+26|-)(/:'ctljapqhewvknfdsyigbmuoxrz')+/@i."1(i.<./@:)26|(i.-27)+/])@(7+3&u:)
```
An explanation for `(/:'ctljapqhewvknfdsyigbmuoxrz')`: In this verb, we operate on the letter values as A=0, B=1, C=2, etc. To encode the letter values of the string `etaoinshrdlcumwfgypbvkjxqz`, the shortest way is actually to take the sort permutation for this weird string. This is because A is at index 4, B at index 19, C at 0, D at 14, and so on; hence the sort permutation is `4 19 0 14 8 13 ...` when you grade (`/:`) it, and you get exactly the number values for `etaoin...`.
Usage:
```
   (26|](-1!:2&2&.":)(/:'ctljapqhewvknfdsyigbmuoxrz')+/@i."1(i.<./@:)26|(i.-27)+/])&.(_97+3&u:) 'ocdndnvqzmtnzxmzohznnvbzocvodnqzmtnzxpmzviynvaz'
21
thisisaverysecretmessagethatisverysecureandsafe
   NB. naming for convenience
   f =: (26|](-1!:2&2&.":)(/:'ctljapqhewvknfdsyigbmuoxrz')+/@i."1(i.<./@:)26|(i.-27)+/])&.(_97+3&u:)
   f 'pmttwxmwxtmwnxzwoziuuqvoxchhtmakwlmowtnabiksmfkpivom'
8
hellopeopleofprogrammingpuzzlescodegolfstackexchange
   f 'wtaad'
0
wtaad
```
[Answer]
# q, 97
```
{(w;m w:g?min g:(+/')("etaoinshrdlcumwfgypbvkjxqz"!t)m:(t!u!/:rotate[;u:.Q.a]'[(-)t:(!)26])@\:x)}
```
.
```
q) tests:(
    "ocdndnvqzmtnzxmzohznnvbzocvodnqzmtnzxpmzviynvazocdndnvqzmtnzxmzohznnvbzocvodnqzmtnzxpmzviynvaz";
    "pmttwxmwxtmwnxzwoziuuqvoxchhtmakwlmowtnabiksmfkpivom";
    "ftueimeqzodkbfqpiuftdaffiqxhqeaufygefnqbqdrqofxkemrq";
    "jgtgkuvjghkpcnvguvecugvjcvaqwowuvfgetarv")
q) f:{(w;m w:g?min g:(+/')("etaoinshrdlcumwfgypbvkjxqz"!t)m:(t!u!/:rotate[;u:.Q.a]'[(-)t:(!)26])@\:x)}
q) f each tests
21 "thisisaverysecretmessagethatisverysecureandsafethisisaverysecretmessagethatisverysecureandsafe"
8  "hellopeopleofprogrammingpuzzlescodegolfstackexchange"
12 "thiswasencryptedwithrottwelvesoitmustbeperfectlysafe"
2  "hereisthefinaltestcasethatyoumustdecrypt"
```
[Answer]
# APL - 70 characters
```
F←{↑⍋+/'etaoinshrdlcumwfgypbvkjxqz'⍳⊃(⍳26){l[(⍺⌽l←⎕UCS 97+⍳26)⍳⍵]}¨⊂⍵}
```
Example:
```
      F 'ocdndnvqzmtnzxmzohznnvbzocvodnqzmtnzxpmzviynvaz'
21
      F 'pmttwxmwxtmwnxzwoziuuqvoxchhtmakwlmowtnabiksmfkpivom'
8
      F 'ftueimeqzodkbfqpiuftdaffiqxhqeaufygefnqbqdrqofxkemrq'
12
      F 'jgtgkuvjghkpcnvguvecugvjcvaqwowuvfgetarv'
2
```
I'm sure there are ways to compress this further, and I invite any other APL users to come up with solutions for that.
[Answer]
# Python 188
```
x="abcdefghijklmnopqrstuvwxyz"
y=input()
r=lambda n:"".join(x[x.find(i)-n]for i in y)
s={sum("etaoinshrdlcumwfgypbvkjxqz".find(b)for b in r(a)):(a,r(a))for a in range(26)}
print(s[min(s)])
```
[Answer]
Perl: 256 char (plus newlines for readability) including the frequency table:
```
@f=unpack("W*","etaoinshrdlcumwfgypbvkjxqz");
@c=unpack("W*",<>);$m=ord("a");$b=1E10;
for$n(0..25){$s=0;$t="";
for$x(0..scalar@c){$r=($c[$x]-$n-$m)%26+$m;$t.=chr($r);
for(0..scalar@f){if($r==$f[$_]){$s+=$_}}}
if($s<$b){$b=$s;$w=$t;$a=$n}}
printf"%d %s\n",$a,$w;
```
Text is provided like so:
```
echo "ocdndnvqzmtnzxmzohznnvbzocvodnqzmtnzxpmzviynvaz" | perl ./freq.pl 
21 thisisaverysecretmessagethatisverysecureandsafewm
```
Take off 12 char if you want to bake-in the values of ord(a) and the length of @f
[Answer]
# Elm - 465
Not going to win any golfing awards, but it makes a static webpage which displays a list of the form `[(rotation number, rotated string)]` as you type.
*Note: not yet working [here](http://share-elm.com) but you can copy-paste it into [the official editor](http://elm-lang.org/try) and run it.*
```
import String as S
import Char (..)
import Graphics.Input (..)
import Graphics.Input.Field (..)
f="ETAOINSHRDLCUMWFGYPBVKJXQZ"
r s n=let t c=mod(toCode c-65+n)26+65 in map(fromCode . t)(S.toList s)
w s=case s of 
 ""->0
 s->sum(S.indexes(S.left 1 s)f)+w(S.dropLeft 1 s)
b s=sort<|map(\x->((w . S.fromList . r s)x,(26-x,S.fromList<|r s x)))[0..25]
c=input noContent
main=above<~(field defaultStyle c.handle id""<~c.signal)~(asText . b . .string<~c.signal)
```
[Answer]
# Python 2, 171
```
f,R,i='zqxjkvbpygfwmucldrhsnioate',{},raw_input();a=sorted(f)*2
for n in range(26):_=''.join(a[ord(x)-71-n]for x in i);R[sum(2**f.index(x)for x in _)]=n,_
print R[max(R)]
```
[Answer]
# [Ruby](https://www.ruby-lang.org/), 142 bytes
```
->s{(0..25).map{|i|[*s.map{["etaoinshrdlcumwfgypbvkjxqz".chars.index(c=((_1.ord-97-i)%26+97).chr),c]}.transpose.map{_1.reduce:+},i]}.min[1..]}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=VY9NTsMwEIX3nGJUqVJCG4tWglKksuwlogg5TkJMG9ux8-s2V-ACbCIEZ0LchonbDbI8743n84z98aXruB8_97vvusqCx9_34NmcvDtC1vc-Kag6nfk5vDXOhrO0opILk-vkyOqizV57FTeHt660M8Jyqg3hIkk7j-0872VFpE6C7Sbg_nz9sNhufGS0v2TRQCpNhVHSpK4xojpNapY-LYYlx3LBRbgiJBqur_pRsA_nbSiBQQLC7QZKsFBAhd5Ch86ChByjcNXY5QyddPx_Wjm-AQ69oynYKLq5jlEOrKB1badYORXoLKrEyKHGVbr2HY7JcU0UhQMSR3QSdRpH8SkcTw2eZagKs-lWEUWX743jRf8A)
[Answer]
# [Python](https://python.org) + [`golfing-shortcuts`](https://github.com/nayakrujul/golfing-shortcuts), ~~165~~ 161 bytes
A port of [gcq's answer](https://codegolf.stackexchange.com/a/24812/114446).
```
from s import*
def F(Y):R=lambda N:sj("",(Sl[Sl.find(I)-N]for I in Y));S={s("etaoinshrdlcumwfgypbvkjxqz".find(B)for B in R(A)):(A,R(A))for A in r(26)};p(S[q(S)])
```
[Answer]
# [R](https://www.r-project.org), 140 bytes
```
\(e,u=utf8ToInt,x=u(e)-97)c(n<-which.min(sapply(25:0,\(z)sum(match((x+z)%%26,u("etaoinshrdlcumwfgypbvkjxqz")-97)))),intToUtf8((x-n)%%26+97))
```
[Attempt This Online!](https://ato.pxeger.com/run?1=TZBNTsMwEIX3HCNSJUdNEKoEFEQPwL7sunEcO3Zcj-3Ef_EZuAGbskDiSlyCMxBKhDqb0czom_dm3t6H02dLyTAZt_vwjtXbr9cDopXfzcV2r5_BVWnnES3rh_uSIHiqIxeEXysBaMTGHCe0uX28qQ4ol6NXSGFHOEJpncvVanNXeVRQh7WAkQ_tkXgVWTeZJsg-2Vyct85RCXB7_TJLzmgNZ3T9O1o8fS8eUaFJCy0Em5WDnFTWPAOEJmsSdAtL26gcxAQBzwpX_6hRzsWkYnIqQspRZ-G9DToRzp3CMh6Vjg5wI-SomDQiaHXJM-epUNRm3cqGWSM8cy1mTNjELcWeTR1lYBvbDlazJKka7CXfd66TPvQdl4ZA6HygxHehJwHbqKMPrJtfNYRiufp0-ss_)
]