ETAOIN SHRDLU golf

43

9

Short and sweet description of the challenge:
Based off ETAOIN SHRDLU, your challenge is to write the shortest program or function in any language that outputs the 26 letters of the English alphabet based on their frequency in the input.

Really long, dry and thorough specification:

  • Your program/function will receive as input a string of text, which will contain one or more uppercase and/or lowercase letters and may also contain punctuation, numerals, symbols, and other non-alphabetic characters.
  • The program/function must output only the 26 UPPERCASE letters of the English alphabet, including those that do not appear in the input, ordered from most to least frequent based on how many times they appear in the input.
  • Edit: The frequency is calculated case-insensitively, but the output must be in uppercase.
  • If two or more letters have the same frequency, they may be in any order.
  • No other output, such as whitespace, is allowed.
  • Edit 7/1/2014: Based on feedback, I am amending this rule. The only other output that is allowed is optional leading and/or trailing whitespace, such as a trailing newline. No other output is allowed.
  • Undefined behavior is allowed for input that does not contain any letters.

The winner will be picked 7 days from now, so get those fingers typing!


Example input:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent vitae erat velit. Mauris gravida euismod libero ut tincidunt. Phasellus elit dui, consectetur et egestas in, aliquam vitae diam. Donec eget varius ante. Vestibulum cursus diam aliquet, egestas orci quis, placerat dolor. Proin vel nisi lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Aliquam erat volutpat. Etiam libero tortor, ornare id dui eget, posuere dignissim libero. Pellentesque commodo consequat volutpat. Integer hendrerit sapien libero, vel viverra augue facilisis sit amet. Quisque consectetur eget nisl quis dignissim. Ut lacinia pretium quam a placerat.
Morbi sed interdum risus, nec pretium lectus. Morbi imperdiet est id accumsan molestie. Duis sed fermentum nisl. Nunc vitae augue mattis, dictum lectus vel, accumsan nisl. Sed ultricies adipiscing rhoncus. Vivamus eu lacus a enim venenatis eleifend. Praesent consectetur tortor non eleifend ultricies. Mauris et odio posuere, auctor erat at, fringilla est. Proin in vestibulum erat. Maecenas congue commodo ante vel varius. Sed tempus mi ut metus gravida, nec dictum libero dapibus. Morbi quis viverra elit. Ut pharetra neque eget lacus tincidunt dictum. Fusce scelerisque viverra tellus et pretium.
Fusce varius adipiscing odio. Nulla imperdiet faucibus sem, at rhoncus ipsum adipiscing vitae. Phasellus imperdiet congue lacus et mollis. Nullam egestas mauris magna, et mollis lectus varius ut. Sed sollicitudin adipiscing dolor, vel elementum elit laoreet molestie. Aliquam nec nulla vel sem ultrices ullamcorper. Nullam nec felis magna. Duis sodales orci non justo aliquam tempus. Integer mi diam, tempor sed vulputate et, varius et nunc. Vestibulum sodales ipsum id mi pharetra, ut convallis mi accumsan. Sed dictum volutpat vestibulum.
Quisque ac dolor sagittis, aliquam libero at, euismod enim. Nulla ullamcorper posuere nulla vitae varius. Nam at dolor non libero elementum pellentesque in in lorem. Fusce porttitor turpis in quam placerat varius. Donec lorem orci, condimentum eu sapien sit amet, aliquet commodo magna. Quisque sed lectus sit amet arcu euismod accumsan et non nunc. Phasellus placerat congue metus, feugiat posuere leo dictum quis. Sed ultricies feugiat eros dignissim bibendum.
Mauris scelerisque consectetur libero eget varius. Aenean neque nunc, ullamcorper vitae orci in, auctor ornare sapien. Nam lacinia molestie imperdiet. Nam vitae mattis nibh. Vestibulum consequat tellus ac nisi sagittis pulvinar. Nullam mollis ornare quam, et venenatis leo porttitor sit amet. Nulla urna neque, dignissim non orci ut, volutpat ultrices erat. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Pellentesque vestibulum tellus nec eros faucibus porta.

Example output:

EITUSALNROMCDPVGQBFHJKWXYZ

Note: there is a 5 way tie between KWXYZ for that input.

Edit:

The competition is over! Thanks to everyone who participated. And now for the winner(s!): Both Dennis' CJam and isaacg's Pyth answers came in at a whopping 19 characters. (Sorry, but I'm not going to accept either of the answers because I think it would be unfair to the other.) Edit: Taking Dennis' advice, I'm going to mark his answer as accepted because his was the first to reach 19 characters. Honorable mentions go to Ilmari Karonen's third-place Golfscript answer at 22 chars as well as undergroundmonorail's 75-char Python answer that got the most upvotes. Once again, thanks to everyone who participated!

Abraham

Posted 2014-06-30T23:02:35.750

Reputation: 1 023

I knew this from NoSleep lol. – Magic Octopus Urn – 2017-10-24T17:30:06.980

Is frequency measured for the upper case letters of the input only? – Howard – 2014-06-30T23:06:20.127

@Howard, no, frequency is measured based on all letters but the output is in uppercase only – Abraham – 2014-06-30T23:10:28.387

Does the "no whitespace in output" rule mean that the output must not end with a newline? – Ilmari Karonen – 2014-07-01T01:44:28.557

2@IlmariKaronen yes newlines count as whitespace so that would not be allowed. – Abraham – 2014-07-01T01:47:18.290

Can languages, like Pyth, which have essentially no means of output that does not include a trailing newline, be exempt from that rule? – isaacg – 2014-07-01T02:37:43.180

@isaacg sure, we can make an exception – Abraham – 2014-07-01T02:41:07.543

If we choose to use a function, how should the byte count be taken? In JavaScript, for example, f=function(){return} could be counted as 7 bytes (inner function: return), 18 bytes (anonymous function: function(){return}) or 20 bytes (named function: f=function(){return}). – Dennis – 2014-07-01T04:40:43.603

@Dennis I think it's standard to include the function definition in the character count, so I would say go with your last example. – Abraham – 2014-07-01T10:37:57.977

8@Abraham: IMO; if a trailing newline (specifically) is allowed for some languages, it should be allowed generally for all languages; not the least because a text stream should be terminated by a newline followed by EOF. – Williham Totland – 2014-07-01T12:15:11.857

If I misinterpreted the spec (e.g. my example is wrong), please edit! – durron597 – 2014-07-01T19:28:56.793

@durron597 The example is right on the mark, you da man! – Abraham – 2014-07-01T20:45:31.827

3@WillihamTotland based on the number of upvotes your comment has, I will modify the rules to allow a trailing newline. – Abraham – 2014-07-01T20:49:25.390

1What to do if there're two answers with the shortest code? suggests awarding the green checkmark to the earlier solution. isaacg posted his answer first, I golfed mine to 19 bytes first. Whichever tie breaker you pick will be fine by me, but not having an accepted answer at all is a little anti-climatic in my opinion. – Dennis – 2014-07-07T23:33:21.407

Answers

26

CJam, 21 19 bytes

qeu:A;'[,65>{A\-,}$

Try it online.

Example

$ cjam etaoin.cjam <<< "~XyxY YyxZ"
YXZABCDEFGHIJKLMNOPQRSTUVW

(no newline)

How it works

qeu:A; " Read from STDIN, convert to uppercase, save in the variable “A” and discard, ";
'[,    " Push an array of all ASCII characters before “[” (NUL to “Z”).               ";
65>    " Remove the first 64 characters (NUL to “@”).                                 ";
{      " Sort the array of characters by the following mapping:                       ";
  A\   " Swap the character with the string saved in variable “A”.                    ";
  -    " Remove all occurrences of the character from the string.                     ";
  ,    " Push the length of the string.                                               ";
}$     "                                                                              ";

More occurrences means more characters get removed, so the most frequent characters appear at the beginning of the array.

Dennis

Posted 2014-06-30T23:02:35.750

Reputation: 196 637

Very clever indeed. – Abraham – 2014-07-02T01:18:22.477

Congratulations @Dennis on winning the contest! – Abraham – 2014-07-07T22:40:38.833

43

Python 2 or 3 - 77 75 bytes

f=lambda s:''.join(sorted(map(chr,range(65,91)),key=s.upper().count))[::-1]

I had an answer before that grabbed input from STDIN, but I realized it was technically invalid. I used input() which gets only a single line, but the question's example input implies that it should handle multiple lines at once. To meet spec, I turned my answer into a function that takes a string argument. To my surprise, it was two bytes smaller! It didn't occur to me that print(...) and input() were longer than f=lambda s: and s.

This also makes the answer compatible with both Python 2 and Python 3. Originally it was only Python 3, because it used input() (which was called raw_input() in 2). Now that it's a function, it works in both.

Explained

                                  range(65,91)                              # The numbers 65 to 90
                          map(chr,range(65,91))                             # Convert to ASCII

                                                    s                       # The input string
                                                    s.upper()               # Convert to uppercase
                                                    s.upper().count         # Function literal for 'how many times the argument appears in the string'

                   sorted(map(chr,range(65,91)),key=s.upper().count)        # Sort by that function
           ''.join(sorted(map(chr,range(65,91)),key=s.upper().count))       # Concatenate to string
           ''.join(sorted(map(chr,range(65,91)),key=s.upper().count))[::-1] # Step through by -1 (i.e. reverse string)

  lambda s:''.join(sorted(map(chr,range(65,91)),key=s.upper().count))[::-1] # Make it a function (`return` is implicit for lambdas)
f=lambda s:''.join(sorted(map(chr,range(65,91)),key=s.upper().count))[::-1] # Give it a name

undergroundmonorail

Posted 2014-06-30T23:02:35.750

Reputation: 5 897

2On the other hand, the comments in the explanation make me facepalm. Welcome to CS 101! – Izkata – 2014-07-02T19:37:05.203

6@Izkata The important thing is that it shows you in which order to read the code. Because the best place to start reading golfed code is rarely obvious, especially when it gets a bit more complicated or terse than this. – Martin Ender – 2014-07-02T21:41:05.170

1Beautiful presentation! – xnor – 2014-07-03T00:07:39.377

Can't you shave off ~3 characters with def f(s):\n instead of f=lambda s:? – Patrick Collins – 2014-07-03T05:02:21.470

1Can you save 5 chars by changing key=s to key=-s and deleting [::-1]? – user253751 – 2014-07-03T06:36:43.897

3@Izk My goal was to make it understandable to people who don't know python. I would never make comments like these in a real project. – undergroundmonorail – 2014-07-03T13:35:37.613

@pat No, because I'd also have to use return. Also, FYI: Because this is only one line, the \n would be unnecessary. – undergroundmonorail – 2014-07-03T13:36:35.703

2@imm No. count isn't a variable or anything, it's a literal function. As cool as it would be to be able to multiply a function's return value by -1 by sticking a - in front, that's not a feature python has. – undergroundmonorail – 2014-07-03T13:40:30.230

You can save 3 whole characters by using positional arguments and removing the slicing array reversal -- you have to specify None as the second (comparator), but that lets you specify 1 (as in True) to the reverse argument. – Aesin – 2014-07-05T09:24:27.477

15

Bash, 65 bytes

(tr a-z A-Z;echo {A..Z})|fold -1|sort|uniq -c|sort -nr|tr -dc A-Z

Example

$ bash etaoin.sh <<< "~AbaB BbaC"
BACZYXWVUTSRQPONMLKJIHGFED

How it works

(              #
  tr a-z A-Z   # Turn lowercase into uppercase letters.
  echo {A..Z}  # Print all uppercase letters.
) |            #
fold -1 |      # Split into lines of length 1.
sort |         # Sort those lines (required for piping to uniq).
uniq -c |      # Print the frequencies of all lines.
sort -nr |     # Sort by frequency (reversed).
tr -dc A-Z     # Remove everything that's not an uppercase letter.

Dennis

Posted 2014-06-30T23:02:35.750

Reputation: 196 637

1This is not locale portable, you need to force LC_COLLATE=C (or shorter, LC_ALL). – Chris Down – 2014-07-01T09:17:14.477

6@ChrisDown Portability isn't generally a concern in code golf answers. – Kevin – 2014-07-01T13:07:59.253

1Without portability, what this answer does is not well defined. – Chris Down – 2014-07-01T13:09:09.277

@ChrisDown: I've tested a few strings and a few locales, but I couldn't find an example where uniq misbehaves for alphabetic characters. Could you show me one? – Dennis – 2014-07-01T14:34:35.023

@ChrisDown This script is portable enough to run on OpenBSD using the BSD versions of fold, sort, tr, uniq, if the shell is bash or ksh93. Other shells, like zsh, fail to expand {A..Z}. All LC_COLLATE locales work, because OpenBSD only has LC_COLLATE=C. – kernigh – 2014-07-02T17:59:53.723

12

Pyth 1.0.2, 19 20

=ZUwsVm;dSm[cZkk)UG

Try it here: http://ideone.com/fork/YlWpEJ

Learn more about Pyth here: http://esolangs.org/wiki/Pyth

Example:

Based off ETAOIN SHRDLU, your challenge is to write the shortest program or function in any language that outputs the 26 letters of the English alphabet based on their frequency in the input.

Gives:

TENOHARSIULGFPYDCBWQMZXVKJ

Explanation:

=ZUw: Convert input to uppercase and store in Z.

sV: Print the sum of the reverse of

m;d: The last entries of

S: Sorted by their first entry, in increasing order

m[cZkk): The lists [count of k in Z, k]

UG: For k in the uppercase letters.

Rough Python equivalent:

G='abcdefghijklmnopqrstuvwxyz'
Z=copy(upper(input()))
print(_sum(rev(_map(lambda d:d.pop(),sorted(_map(lambda k:_list(count(Z,k),k),upper(G)))))))

This is not entry, I just thought people might like to see it. In Pyth 1.0.4, the following program is a solution in 10 characters:

JUwo_cJNUG

Explanation:

JUw: Convert input to uppercase and store in J.

o: (Print) Sort by

_cJN: -1*(count of N in J)

UG: Over N in the uppercase letters.

It is not a legal solution because several of the changes from Pyth 1.0.2 to 1.0.4, including the addition of the o, sort by, function, were in response to this problem.

isaacg

Posted 2014-06-30T23:02:35.750

Reputation: 39 268

I suggest you to update the Pyth link to the official page of Pyth, if it exists. – A.L – 2014-07-01T02:15:11.183

@A.L. That is the official page of Pyth, until I put up an esolang entry. – isaacg – 2014-07-01T02:35:47.587

Why do you need to store the input in a variable Z to refer to it once later? Would just putting the expression for Z in its place cause input to be read multiple times? – xnor – 2014-07-01T20:53:09.663

@xnor The position Z is used in is inside a lambda function, because it is in the first argument of a map, so it needs to be saved to a variable. Using Uw there would indeed result in input being read 26 times. – isaacg – 2014-07-01T20:56:54.857

@isaacg see the rule change above allowing trailing newlines for everyone. – Abraham – 2014-07-01T21:23:00.237

@Abraham Great, I think an even footing is a good way to go. I'll edit my answer accordingly. – isaacg – 2014-07-01T21:30:58.373

@isaacg Yeah, i decided to change it based on the amount and direction of feedback I was getting – Abraham – 2014-07-01T21:32:22.013

@isaacg: I asked this because GolfScript has a website: http://www.golfscript.com/golfscript/ In fact, I think that the source code of Pyth would be more interesting with code colorisation, if I remember correctly Codepad can do this.

– A.L – 2014-07-02T01:17:42.123

1@A.L Alright, Pyth is up on both esolang and on github. The github link is at the bottom of the esolang page, which is linked in the answer. Github does code colorization. – isaacg – 2014-07-02T01:44:36.443

Thanks, I hope it will help Pyth to gain visibility and be used more and more for golfing. – A.L – 2014-07-04T15:17:18.033

Congratulations @isaacg on winning the contest! – Abraham – 2014-07-07T22:40:59.140

11

Javascript (ES6) 119 117

Edit: (-2) Removed the need for toUpperCase using a case-insensitive RegEx in the split call.

a=prompt(f=x=>a.split(RegExp(x,'i')).length)
alert([...'ABCDEFGHIJKLMNOPQRSTUVWXYZ'].sort((b,c)=>f(c)-f(b)).join(''))

Alternative (same length): Condensed the sort and character counting into a single function.

a=prompt()
alert([...'ABCDEFGHIJKLMNOPQRSTUVWXYZ'].sort(f=(b,c)=>c?f(c)-f(b):a.split(RegExp(b,'i')).length).join(''))

As a function: 105 104

Edit: (-1) Condensed the sort and character counting into a single function.

F=a=>[...'ABCDEFGHIJKLMNOPQRSTUVWXYZ'].sort(f=(b,c)=>c?f(c)-f(b):a.split(RegExp(b,'i')).length).join('')

nderscore

Posted 2014-06-30T23:02:35.750

Reputation: 4 912

1Interesting technique. – Matt – 2014-07-01T04:44:38.347

1Splattering a string… delightful! – Bergi – 2014-07-01T20:23:51.613

10

GolfScript, 22 chars

:?91,+-26>{.32+]?\-,}$

Try it online.

Explanation:

  • :? assigns the input string to the symbol ?. (I use a punctuation symbol so that the following number 91 is not parsed as part of the symbol name.)
  • 91, constructs a list of the numbers from 0 to 90 (ASCII code of Z).
  • + appends this list to the input string, thereby converting it from an array of ASCII codes to a string (and conveniently also removing the input string from the stack).
  • -26> takes the last 26 characters of this string, producing a string containing the uppercase ASCII letters from A to Z.
  • The { }$ applies the code block to all the characters in the input string, and sorts those characters according to the result.
  • Inside the code block, . duplicates the character and 32+ converts the copy from uppercase to lowercase. The ] collects these two characters into an array, ?\- takes the input string stored in ? and removes all occurrences of the characters in the array from it, and , counts the length of the remaining string, which will be the sort key. The characters will be sorted in ascending order by this key, and thus in descending order by number of occurrences.

Ilmari Karonen

Posted 2014-06-30T23:02:35.750

Reputation: 19 513

1Looks like we had pretty much the same idea. Minor bug: The letter Z is missing. It should be 91,+-26>. – Dennis – 2014-07-01T04:03:39.923

@Dennis: Ah, oops. Fixed, although who needs that letter anyway? :) – Ilmari Karonen – 2014-07-01T13:34:35.277

2@IlmariKaronen Based on community feedback, I amended the rules to allow for a trailing newline (see the question for a complete description). Your score is now 22, rather than 25 :) – Abraham – 2014-07-01T20:54:58.637

8

Haskell, 110 bytes

import Data.List
import Data.Char
a%f=compare(f a).f
f t=sortBy(% \c->length$filter((/=c).toUpper)t)['A'..'Z']

Example usage:

λ> f "Based off ETAOIN SHRDLU, your challenge is to write the shortest program or function in any language that outputs the 26 letters of the English alphabet based on their frequency in the input."
"ETNAHORISULFGPBCDYMQWJKVXZ"

Flonk

Posted 2014-06-30T23:02:35.750

Reputation: 7 621

1What about (/=c) and getting rid of the 0-? – Lynn – 2015-08-14T02:21:10.027

@Mauris nice catch! I edited it in and shaved off another character. – Flonk – 2015-08-19T09:52:02.553

6

Ruby 2.0, 53 characters

EDIT: Fixed to work correctly with multiline strings, thanks @durron597!

f=->s{$><<(?A..?Z).sort_by{|c|-s.upcase.count(c)}*''}

Creates a function called f which can be used as follows:

f['jackdaws love my big sphinx of quartzzz']

Prints to STDOUT:

AZOSICGHEJKLMBFPQRDTUVWXYN

Paul Prestidge

Posted 2014-06-30T23:02:35.750

Reputation: 2 390

2This answer is not correct. It gives this answer: EITASUROLNCMPDVQGBHFKJWXYZ for the example in the question – durron597 – 2014-07-01T19:23:54.880

1@durron597 Thanks, you are correct! It wasn't handling multiline strings correctly - gets returns a line at a time. It can be fixed by changing gets to gets$n but changing it into a function is 1 char shorter. – Paul Prestidge – 2014-07-01T23:46:32.603

6

Perl, 54 46 bytes

UPDATE: after further optimizations it could be squeezed to 46 bytes: (thx dennis for -n/{} hack; chinese perl goth for <=>->- hack)

s/./$h{uc$&}++/eg}{say sort{$h{$b}-$h{$a}}A..Z

It is to be run with run with perl -nE

Original solution (does not need special Perl options):

s/./$h{uc$&}++/egfor<>;print sort{$h{$b}<=>$h{$a}}A..Z

Verified in Perl 5.8.3, 5.14.2

If you get a warning, separate eg and for with a space (+1 char), if you mind

example usage:

$ python -c 'import this' | perl -le 's/./$h{uc$&}++/egfor<>;print sort{$h{$b}<=>$h{$a}}A..Z' 2>/dev/null
ETAISONLRHPBUCDYMFGXVWKZJQ

EXPLANATION: On each character (.) of each input line (for<>), apply a substitution "pattern", which in fact is evaluated as an expression (e flag of the s///), that increments an upcased (uc) character (./$& is shorter than more obvious (.)/$1) count in the (uninitialized) hash (%h). Then, the letter frequency hash is used in a sort comparison function to print out upper-case alphabet in the right order.

mykhal

Posted 2014-06-30T23:02:35.750

Reputation: 641

You can cut one more byte by using the -F option instead of -n: Try it online!

– Xcali – 2017-10-24T16:11:05.050

1Shorter and no error messages: perl -ne 's/./$h{uc$&}++/eg}{print sort{$h{$b}<=>$h{$a}}A..Z' – Dennis – 2014-07-03T13:45:10.460

Dennis: very interesting, it looks like a typo.. wth is this? i've had some variants using -n and END{}, but they were always longer.. feel free to update the answer, if you want – mykhal – 2014-07-03T14:02:58.003

Dennis: OK, now i get it. breaking the imaginary (? probably not so) while loop. amazing – mykhal – 2014-07-03T14:14:39.127

1Yes, -n wraps while(<>){...} around the code. I avoid editing other users' code. Too easy to make a mistake, some things only work on some computers, etc. – Dennis – 2014-07-03T14:25:18.187

Dennis: anyway, it's ingenious hack, i can't see it anywhere in the docs. please let me know if you have some links to discussions on this - it's difficult to search (i wish google could do regexp search) – mykhal – 2014-07-03T14:49:13.353

perldoc perlrun has all the magic command-line switches. – Joe McMahon – 2014-07-04T03:11:38.143

1Joe: i was talking about the }{ "injection", not just about the well known -n option. One might not expect that the code string actually technically is wrapped by the while loop code string before the execution, not only just that it somehow works like as if it was wrapped.. – mykhal – 2014-07-04T06:10:05.897

1save two more chars: replace $h{$b}<=>$h{$a} with $h{$b}-$h{$a} – chinese perl goth – 2014-07-07T10:03:22.530

5

R, 123 bytes

The code is improved thanks to the suggestions by @RichieCotton.

text <- "Based off ETAOIN SHRDLU, your challenge is to write the shortest program or function in any language that outputs the 26 letters of the English alphabet based on their frequency in the input."

f=function(x){b=plyr::count(toupper(strsplit(x,"")[[1]]));c=merge(LETTERS,b,all.x=T);paste(c[order(-c$freq),1],collapse="")}

f(text)

Output:

> f(text)
[1] "ETNAHORISULFGPBCDYMQWJKVXZ"

djhurio

Posted 2014-06-30T23:02:35.750

Reputation: 1 113

1

@RichieCotton: I have rejected your suggested edit twice now. I don't know if you get notified with the reason for the reject vote, so if you read this: please provide golfing improvements in comments, so the OP can review them. Here is why: http://meta.codegolf.stackexchange.com/a/1619/8478

– Martin Ender – 2014-07-05T20:46:26.683

4

VBScript 181 109

Updated to use a completely different algorithm. Beats JavaScript!

Pretty:

dim b(99):i=ucase(inputbox(k))
for y=65to 90
    c=chr(y)
    a=len(replace(i,c,k))
    b(a)=c+b(a)
next
msgbox join(b,k)

Golfed:

dim b(99):i=ucase(inputbox(k)):for y=65to 90:c=chr(y):a=len(replace(i,c,k)):b(a)=c+b(a):next:msgbox join(b,k)

comfortablydrei

Posted 2014-06-30T23:02:35.750

Reputation: 701

4

J 41 35 Bytes

(u:65+i.26)([\:[#/.~@,e.~#])toupper

Demo:

i=: 'This is a test to see whether this is still working'
(u:65+i.26)([\:[#/.~@,e.~#])toupper i
STIEHLORWAGKNBCDFJMPQUVXYZ

Explanation:

(u:65+i.26) & ( [ \: [ #/.~@,e.~#]) toupper) )
ABCDE...          |    |    |   |      uppercase the right argument
                  |    |    |   \copy from right only member from left
                  |    |     \append the left argument
                  |    \ Afterwards Count apperances of each letter
                  \ Sort the left according to the appearances

The key is to append the left array, such that all letters are available, and already in order. A funny consequence of using a noun as third tine to the fork is that it works as verb as well as as phrase.

jpjacobs

Posted 2014-06-30T23:02:35.750

Reputation: 3 440

4

SAS - 217 (I think)

Input should be placed on the line after the cards4 statement, or on the appropriate punch cards for your system. I think this approach saves a few characters vs. attempting to quote the input.

data a;
input;
S = upcase(compress(_INFILE_,,'ak'));
do i=1 to length(S);
l=substr(S,i,1);
output;
end;
cards4;
;;;;
run;
proc sql;
select l into :o separated by '' from
(select l, 1/count(l) as f from a group by l) order by f;
quit;

I'm aware that this doesn't meet the full spec, as it only returns characters that appear in the input string. I might need to rethink this a bit.

user3490

Posted 2014-06-30T23:02:35.750

Reputation: 809

Doesn't meet the spec, but is still cool, so +1 :D – cat – 2016-03-30T01:57:20.407

4

C++, 185 183 179 177 bytes

Not expected to win, of course (can C++ ever win?) but a fun exercise nonetheless.

#include <algorithm>
#include <stdio.h>
int f[256],p;main(){for(p=65;p<91;p++)f[p]=p;while(~(p=getchar()))f[p&95]+=256;p=256;std::sort(f,f+p);while(p--)f[p]&95&&putchar(f[p]);}

Explanation:

#include <algorithm>         // for std::sort
#include <stdio.h>           // for getchar, putchar
int f[256],p;                // declare an array of count-prefixed chars, and a counter
main(){
    for(p=65;p<91;p++)       // 65 == 'A', 91 == the character after 'Z'
        f[p]=p;              // set the character for the slot
    while(~(p=getchar()))    // read characters until EOF
        f[p&95]+=256;        // increment the packed count for the character stripped of the 'lowercase bit'
    p=256;                   // start a countdown
    std::sort(f,f+p);        // sort the array
    while(p--)               // do the countdown
        f[p]&95 &&           // if the masked-off character is set...
          putchar(f[p]);     // print it
}

fluffy

Posted 2014-06-30T23:02:35.750

Reputation: 725

4

Groovy - 130 123 115 112 98 92

As per @cfrick's advice (twice!):

f={('A'..'Z').collectEntries{c->[c,it.grep(~/(?i)$c/).size()]}.sort{-it.value}*.key.join()}

A small test (shamelessly stolen from @jpjacobs):

assert f('This is a test to see whether this is still working') == 
    'STIEHLORWAGKNBCDFJMPQUVXYZ'

And the proposed test is also passing

Will Lp

Posted 2014-06-30T23:02:35.750

Reputation: 797

1The function needs to output all 26 letters, not just those present in the input string. – algorithmshark – 2014-07-01T20:44:39.260

@algorithmshark, indeed, my mistake, fixed – Will Lp – 2014-07-01T21:02:24.627

f={('A'..'Z').collectEntries{c->[c,it.toUpperCase().findAll(c).size()]}.sort{-it.value}.keySet().join()} for 104 – cfrick – 2014-07-03T17:00:18.763

1another 6 bytes: it.grep(~/(?i)$c/) instead of it.toUpperCase().grep(c) – cfrick – 2014-07-04T10:57:22.430

@cfrick Wow! Thanks again! That toUpperCase was bothering the hell out of me. – Will Lp – 2014-07-04T11:43:36.730

4

AppleScript, 278

I noticed that "a" = "A" is true in AppleScript. I can use this in code golf, but the rest of the script is too wordy. I used AppleScript 1.8.3.

This defines a function f. If you add f("a string") at the bottom of the script and run it in the Script Editor, it will show the result.

on c(n)
ASCII character(64+n)
end
on f(s)
set{a,r}to{{},""}
repeat with i from 1 to 26
set j to 0
repeat with b in s
if b&""=c(i)then set j to j+1
end
set a to a&j
end
repeat with j from 0 to(count s)
repeat with i from 1 to 26
if a's item i=j then set r to c(i)&r
end
end
r
end

Formatted and commented:

-- Returns nth letter of alphabet.
on c(n)
    ASCII character (64 + n)
end c

-- Returns letters in s sorted by frequency.
on f(s)
    -- a: list of letter counts
    -- r: resulting string
    set {a, r} to {{}, ""}

    -- For each letter from A to Z,
    -- count letters in string s.
    repeat with i from 1 to 26
        set j to 0
        repeat with b in s
            -- Can't use b = c(i), because
            -- b is a reference to a string
            -- and = never dereferences its
            -- operands. Get contents of b,
            -- here by coercing b to string.
            if b & "" = c(i) then set j to j + 1
        end repeat
        -- Set item i of a to count j.
        set a to a & j
    end repeat

    -- Sort letters by frequency.  Do a counting sort
    -- because AppleScript lacks a sort command.
    repeat with j from 0 to (count s)
        repeat with i from 1 to 26
            if a's item i = j then set r to c(i) & r
        end repeat
    end repeat
    r
end f

-- Example call:
f("Now is the time for all good men to come to the aid of their country.")
-- Result: "OTEIRNMHLFDCAYWUSGZXVQPKJB"

kernigh

Posted 2014-06-30T23:02:35.750

Reputation: 2 615

3

Jelly, 9 bytes (non-competing)

ØAŒuċ¥@ÞU

Explanation

ØAŒuċ¥@ÞU  Main Link
       Þ   Sort
ØA         The uppercase alphabet by
  Œuċ¥@    number of occurrences in the input:
  Œu       Uppercase
    ċ      Count occurrences
     ¥     Grammar: Last two links as a dyad
      @    Swap arguments
        U  Reverse (because sort sorts up)

This reads as "sort the uppercase alphabet by the number of occurrences in the input uppercased, reversed", which is a rather literal translation of the challenge :P

Try it online!

This challenge was linked to Jelly HyperTraining where we solved the challenge. I posted this because I was the first to reach 10 bytes.

-1 byte thanks to Erik the Outgolfer (JHT teacher)

HyperNeutrino

Posted 2014-06-30T23:02:35.750

Reputation: 26 575

9 bytes: ØAŒuċ¥@ÞU – Erik the Outgolfer – 2017-10-24T19:04:46.137

@EriktheOutgolfer Oh cool, thanks! – HyperNeutrino – 2017-10-24T20:10:24.250

3

J - 38 35 char

A function taking input on the right as a string. Not a winner, but it was fun to write.

(u:65+i.26)([\:[#/.~@,e.~#])toupper

Explained:

  • toupper is a verb in the standard library that upcases a string. That then becomes the right argument of the verb, while the left argument is the alphabet: ASCII codepoints 65 to 90.

  • [ and ,e.~#]) selects (#) those letters in the right arg (]) that are elements of (e.~) the left, and then prepends (,) the left arg ([). To wit, we keep only the uppercase chars, and add a single copy of the alphabet to the end, to make sure we catch them all.

  • #/.~@ then gives the frequencies of each character. It so happens that this is given in alphabetical order, so right afterwards we can downsort (\:) the alphabet (the left argument [).

A quick lazy example below. Feel free to try it for yourself at tryj.tk.

   (u:65+i.26)([\:[#/.~@,e.~#])toupper 'Based off ETAOIN SHRDLU, your challenge is to write the shortest program or function in any language that outputs the 26 letters of the English alphabet based on their frequency in the input.'
ETNAHORISULFGPBCDYMQWJKVXZ

algorithmshark

Posted 2014-06-30T23:02:35.750

Reputation: 8 144

3

VBScript 157 156 bytes

Edit: changed msgbox(p) to msgbox p

More readable:

s=ucase(InputBox(z))    'z is empty.
L=len(s)
Dim a(255)
for i=1to L
    x=asc(mid(s,i))
    a(x)=a(x)+1
next
for t=0to L
    For i=65To 90
        If a(i)=t then p=chr(i)&p
    next
next
msgbox p

Golfed: (155 characters + 1 carriage return)

s=ucase(InputBox(z)):L=len(s):Dim a(255):for i=1to L:x=asc(mid(s,i)):a(x)=a(x)+1:next:for t=0to L:For i=65To 90:If a(i)=t then p=chr(i)&p
next:next:msgbox p

I had it at 171 earlier with code that I found more interesting, but comfortablydrei's sorting method is shorter and requires len(s), which makes a "for" shorter than the "while" for the first loop. (yawn)

's=UCase(InputBox(Z))&8 'just need any extra character.  0-7 don't work because &7 is octal

s=UCase(InputBox(Z)) 'nevermind
Dim a(999)
While Len(s)
    x=Asc(s) 'returns ascii of first char
    a(x)=a(x)-1 'going negative saves a character later...
    s=Mid(s,2) 'doesn't care if you run out of string
Wend
for j=1 to 26 'this used to be   While Len(p)<26
    For i=65To 90
        If a(i)<a(y) Then y=i 'it is barely not worth it to do a(i)+a(i+32)>a(y) here to skip the ucase() above
    Next
    p=p&Chr(y)
    a(y)=1 'if I didn't go negative this would have to be -1.  arrays default to 0.
Next
MsgBox(p)

JesterBLUE

Posted 2014-06-30T23:02:35.750

Reputation: 696

I learned some cool tricks from this post! Thanks for the mention, too. One thing: I think for t=0 should be for t=1, otherwise you always print the whole alphabet. – comfortablydrei – 2014-07-01T17:10:59.543

1@comfortablydrei Printing the whole alphabet is required.

"The program/function must output only the 26 UPPERCASE letters of the English alphabet, including those that do not appear in the input" – JesterBLUE – 2014-07-01T18:18:00.900

whoa. missed that one. then it's my mistake. thanks! – comfortablydrei – 2014-07-01T18:20:02.433

3

Perl, 78 bytes

undef$/;$i=<>;$r{$i=~s/$_//gi}.=$_ for A..Z;print$r{$_}for sort{$b<=>$a}keys%r
  • Only the 26 uppercase ASCII letters without any white space are output in frequency order.
  • Tied characters are given in alphabetic order.

Result for the example in the question:

EITUSALNROMCDPVGQBFHJKWXYZ

Ungolfed:

# read input
# ----------
undef $/; # disable input separator
$i = <>;  # $i holds the complete input as one string

# analyze
# -------
# For each uppercase letter (A upto Z) its occurences are counted
# via the number of substitutions made by s/$_//gi. The lowercase
# letter is included via modifier "i".
# 
# The occurrence count is then used as key for hash %r.
# The uppercase letter is appended to the value of that hash entry.
$r{$i =~ s/$_//gi} .= $_ for A..Z;

# output
# ------
# The hash keys are sorted numerically in reverse order by
# the specified sort function.
print $r{$_} for sort {$b<=>$a} keys %r

Heiko Oberdiek

Posted 2014-06-30T23:02:35.750

Reputation: 3 841

This might work for the example, bot not e.g. for echo -e 'x\ny\n\nz\n' output, which should return XYZABCDEFGHIJKLMNOPQRSTUVW, but yields XYABCDEFGHIJKLMNOPQRSTUVWZ instead. Guess why.. :) – mykhal – 2014-07-02T19:57:14.307

@mykhal: Fixed. – Heiko Oberdiek – 2014-07-02T20:11:31.717

3

T-SQL 178

Basically, this is my VBScript solution but implemented in SQL.

This is XML output abuse to concatenate a column. In practical use, it can be joined to an outer table to emulate GROUP_CONCAT functions in MySQL and others.

Declaring the @ variable:

DECLARE @ CHAR(1024)= 'enter your text here';

Code:

with y AS(
    SELECT UPPER(@)i,0l,91y
    UNION ALL
    SELECT i,len(replace(i,char(y-1),'')),y-1
    FROM y
    WHERE y>65
)
SELECT LTRIM(
(
    SELECT char(y)
    FROM y
    WHERE y<91
    ORDER BY l
    FOR XML PATH(''))
)

comfortablydrei

Posted 2014-06-30T23:02:35.750

Reputation: 701

3

C# (and LINQ) 255 226 210 Bytes

Using Patrick Huizinga's advice, the query syntax is now shorter:

namespace System.Linq{class P{static void Main(string[]a){Console.Write((from c in(a[0]+"ABCDEFGHIJKLMNOPQRSTUVWXYZ").ToUpper()where c>'@'&&c<'['group c by c into g orderby-g.Count()select g.Key).ToArray());}}}

Explanation:

Console.Write(
    (from c //declare our range variable
       in (a[0] + "ABCDEFGHIJKLMNOPQRSTUVWXYZ").ToUpper() //declare the datasource
     where c > '@' && c < '[' //include only letters
     group c by c into g //run of the mill group by
     orderby -g.Count() //order by descending
     select g.Key //we only want the actual letters
     ).ToArray() //mash it all into an array
  );

Equivalent method syntax (217):

namespace System.Linq{class P{static void Main(string[]a){Console.Write((a[0]+"ABCDEFGHIJKLMNOPQRSTUVWXYZ").ToUpper().GroupBy(c=>c).OrderBy(c=>-c.Count()).Where(c=>c.Key>'@'&&c.Key<'[').Select(c=>c.Key).ToArray());}}}

Original post:

namespace System.Linq{class P{static void Main(string[]a){(a[0]+"ABCDEFGHIJKLMNOPQRSTUVWXYZ").ToUpper().GroupBy(c=>c).OrderByDescending(c=>c.Count()).Where(c=>c.Key>'@'&&c.Key<'[').ToList().ForEach(c=>Console.Write(c.Key));}}}

This is my first ever submission and I should be doing things at work but this just looked like so much fun because I felt like I could actually participate for once.

Explanation:

(a[0] + "ABCDEFGHIJKLMNOPQRSTUVWXYZ") //ensure each character appears once
  .ToUpper()
  .GroupBy(c => c) //get access to .Count()
  .OrderByDescending(c => c.Count())
  .Where(c => c.Key > '@' && c.Key < '[') //exclude anything other than letters
  .ToList() //Only lists have a .ForEach() :(
  .ForEach(c => Console.Write(c.Key)); //print output

I never use the method syntax for LINQ so this was a learning experience for me :) also thinking about it now I could save 2 bytes by replacing the character literals with their integer counterparts, but, meh.

Shortened thanks to tips from ProgramFOX and Num Lock :)

The equivalent query syntax (slightly longer):

(from c in (a[0]+"ABCDEFGHIJKLMNOPQRSTUVWXYZ").ToUpper() where c>'@'&&c<'[' group c by c into g orderby g.Count() descending select g.Key).ToList().ForEach(c=>Console.Write(c));

ldam

Posted 2014-06-30T23:02:35.750

Reputation: 190

1On a first look, you could save a lot of characters by naming your class just P instead of Program and string[]a instead of string[] args and c=>... instead of (c)=>.... – Num Lock – 2014-07-02T09:00:49.533

Instead of two using statements, you can also put your class inside the System.Linq namespace, and remove both using statements. Then you can save some characters and it will still work fine. – ProgramFOX – 2014-07-02T09:33:54.240

@NumLock Right, didn't even think of that :) @ProgramFOX that won't save me anything because namespace is longer than using and the two extra {}s will cost me more. – ldam – 2014-07-02T09:52:36.840

1namespace System.Linq{} is clearly shorter than using System;using System.Linq; just by looking at it. The idea is to omit both usings completely. – Num Lock – 2014-07-02T10:36:28.313

Ahh yes it removes both, you're right, I was thinking it only removed the one. Thanks. – ldam – 2014-07-02T10:51:33.353

If you use ToLookup instead of GroupBy, you can use the Count property instead of the method. Also Char.IsUpper(c.Key) should be a bit shorter, although in case of 'unicode letters' it will include those too, like À. – Patrick Huizinga – 2014-07-03T15:21:58.380

And there is a Console.Write that takes a char[]. So you can use ToArray instead of ToList and cut the ForEach. – Patrick Huizinga – 2014-07-03T15:25:44.693

And of course .OrderBy(c => -c.Count) instead of OrderByDescending – Patrick Huizinga – 2014-07-03T15:29:58.933

Only think I'm able to get working is the OrderBy :( will work on it more at home. – ldam – 2014-07-03T16:02:58.203

ToLookup is not giving me what I want, I'm not able to get that Count you mentioned, only the key. Ordering by the key just puts it in alphabetical order and that's all I'm able to do it seems. ToArray is giving me an array of IGrouping. :( – ldam – 2014-07-03T16:58:13.763

3

PHP - 105 bytes

<?preg_filter(~‹§æ“Ö¢‹ö,'$f[$0&fl]++',join('',range(a,z)).$argv[1]);arsort($f);foreach($f as$l=>$F)echo$l;

Here's a hexdump, cause of the special characters:

0000000 3c 3f 70 72 65 67 5f 66 69 6c 74 65 72 28 7e dc
0000010 a4 be d2 85 a2 dc 9a 2c 27 24 66 5b 24 30 26 df
0000020 5d 2b 2b 27 2c 6a 6f 69 6e 28 27 27 2c 72 61 6e
0000030 67 65 28 61 2c 7a 29 29 2e 24 61 72 67 76 5b 31
0000040 5d 29 3b 61 72 73 6f 72 74 28 24 66 29 3b 66 6f
0000050 72 65 61 63 68 28 24 66 20 61 73 24 6c 3d 3e 24
0000060 46 29 65 63 68 6f 24 6c 3b                     
0000069

And a slightly less golfed version:

<?
preg_filter(           // regular expression
  "#[A-z]#e",          // matches every letter + 'eval' flag
  '$f[$0&fl]++',        // so this code runs for every letter
                       // $f is an array whose indices are uppercase letters
                       //   and whose values represent the number of occurences
                       // lowercase is converted to uc with the bitwise and
                       //   fl is 11011111 in binary, every bit except for 32's is set
  join('', range(a,z)) // adding abcdefghijklmnopqrstuvwxyz to the input
    .$argv[1]);        //   because not all letters have to appear in the input
arsort($f);            // sort $f in reverse, maintaining indices
foreach($f as$l=>$F)   //
  echo$l;              // print each index in order

Example:

 $ php etaoin_shrdlu.php "This function sorts an array such that array indices maintain their correlation with the array elements they are associated with."
 ATIRESHNOCYUWMDLFXZBVGPQKJ

Aurel Bílý

Posted 2014-06-30T23:02:35.750

Reputation: 1 083

echo join(array_keys($f)); can save one byte – Titus – 2017-10-24T15:38:16.400

How do the special characters in preg_filter() work? – Abraham – 2014-07-03T13:33:30.683

3In PHP ~ is the bitwise NOT operator, and you can apply it to strings as well, in which case it works on every character. Furthermore, PHP is happy to parse strings of text as string literals given that there are no special characters in them (e.g. operators, $ for variables, semicolon, parantheses ...). So, writing ~‹§æ“Ö¢‹ö (the bitwise inverted version) instead of "#[A-z]#e" saves one byte, as it does not have to be quoted. – Aurel Bílý – 2014-07-03T13:42:50.950

Ah, thanks. Makes sense now. – Abraham – 2014-07-03T13:44:03.287

1Insofar as anything in PHP makes sense. Holy moly. – fluffy – 2014-07-07T20:47:46.480

3

C# in LINQPad - 203 Bytes

I took a different approach to the answer of Logan Dam. I made sure first that every character in the input string is sorted by its appearance and only exists once in the output string. After that I added every missing character from the alphabet to the output string.

void e(string i){var a="";foreach(var d in i.ToUpper().GroupBy(x=>x).OrderByDescending(u=>u.Count()))if(d.Key<91&&d.Key>64){a+=d.Key;}for(int x=65;x<91;x++)if(!a.Contains((char)x)){a+=(char)x;}a.Dump();}

Sadly it wouldn't beat Logan Dam's answer if I would have made in Visual Studio.

More readable version:

void e(string i)
    {
        var a = "";
        foreach (var d in i.ToUpper().GroupBy(x => x).OrderByDescending(u => u.Count()))
        {
            if (d.Key < 91 && d.Key > 64)
            {
                a += d.Key;
            }
        }
        for (int x = 65; x < 91; x++)
        {
            if (!a.Contains((char)x))
            {
                a += (char)x;
            }
        }
        a.Dump();
    }

tsavinho

Posted 2014-06-30T23:02:35.750

Reputation: 163

Yay, more LINQ love! :D – ldam – 2014-07-03T15:49:11.390

3

C++ 701 322 232 bytes

First version 701 bytes (idiomatic STL usage)

#define _HAS_TRADITIONAL_STL 1
#include <numeric>
#include <iostream>
#include <iterator>
#include <string>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#define ALL(x) x.begin(), x.end()
using namespace std;
typedef istream_iterator<char> iic;typedef pair<int, char> pic;map<char, int> c;set<pic> d;
void f1(char x) {c[x]--;}
void f2(const pic &p) {d.insert(make_pair(p.second, p.first));}
int main(){string s(26, 0);stdext::iota(ALL(s), 65);copy(ALL(s), ostream_iterator<char>(cout));transform(iic(cin), iic(), back_inserter(s), toupper);for_each(ALL(s), f1);for_each(ALL(c), f2);transform(ALL(c2), ostream_iterator<char>(cout), select2nd<pic>());}

Expanded clean version :

#define _HAS_TRADITIONAL_STL 1
#include <numeric>
#include <iostream>
#include <iterator>
#include <string>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
using namespace std;

typedef istream_iterator<char> iic;
map<char, int> counts;
set<pair<int, char> > counts2;

void docount(char ch) { counts[ch]--; }
void toCounts2(const pair<char, int> &p) { counts2.insert(make_pair(p.second, p.first)); }

int main()
{
    string s(26, 0);
    stdext::iota(s.begin(), s.end(), 65);
    transform(iic(cin), iic(), back_inserter(s), toupper);
    for_each(s.begin(), s.end(), docount);
    for_each(counts.begin(), counts.end(), toCounts2);
    transform(counts2.begin(), counts2.end(), ostream_iterator<char>(cout), select2nd< pair<int, char> >());
}

The idea is to demonstrate a "proper" C++ program without any hacks. Ignore the boilerplate and the fact that this only compiles on VC++

Explanation :

We fill A to Z into a string with iota(), this ensures that when we count the occurrences, each character appears even if it's not in the input.

transform() copies character by character from standard input and puts it into the end of s after calling toupper() on each one

The count of each character is decremented in the map (by maintaining negative counts we can have descending sort without extra code)

The counts map entries are copied to a set of pairs, swapping (char, count) to (count, char). Since sets are ordered, we get them sorted by decreasing frequency count

Finally we copy the set's content to standard out, using transform, and using select2nd() to pick out only the second member of the pair.

The code is fairly readable. A C++11 solution would look much prettier, since we can use lambdas

C++11 version - no need of lambdas, but auto and range based for makes things very clean (come to think of it you can do very similar with regular C++98)

#include<iostream>
#include<iterator>
#include<map>
#include<set>
using namespace std;int main(){istream_iterator<char> b(cin),e;map<char,int> c;set<pair<int,char>> d;for(char i='A';i<='Z';++i){--c[i];}for(auto i=b;i!=e;++i){c[toupper(*i)]--;}for(auto p:c){d.insert(make_pair(p.second,p.first));}for(auto p:d){cout<<p.second;}}

Expanded version :

#include <iostream>
#include <iterator>
#include <map>
#include <set>
using namespace std;
int main()
{
    istream_iterator<char> b(cin), e;
    map<char, int> c;
    set<pair<int, char>> d;
    for(char i = 'A'; i <= 'Z'; ++i) {--c[i];}
    for(auto i = b; i != e; ++i) {c[toupper(*i)]--;}
    for(auto p : c) { d.insert(make_pair(p.second, p.first)); }
    for(auto p : d) { cout << p.second; }
}

Next iteration (why read from stdin when we have argv):

#include <set>
#include <iostream>
int c[256];int main(int n, char **s){std::set<std::pair<int,char>> d;while(*s[1]){c[toupper(*s[1]++)]--;}for(n=65;n<92;++n){d.insert(std::make_pair(--c[n],n));}for(auto p:d){std::cout<<p.second;}}

Expanded version:

#include <set>
#include <iostream>
int c[256];
int main(int n, char **s)
{
    std::set<std::pair<int, char>> d;
    while (*s[1])
    {
        c[toupper(*s[1]++)]--;
    }
    for (n = 65; n < 92; n++)
    {
        d.insert(std::make_pair(--c[n], n));
    }
    for (auto p : d)
    {
        std::cout << p.second;
    }
}

rep_movsd

Posted 2014-06-30T23:02:35.750

Reputation: 141

2

Java 8, 171 169 154 139 138 bytes

s->{int x=26,a[]=new int[96],i,l;for(int c:s)a[c&~32]++;for(;x-->0;System.out.printf("%c",l),a[l]=-1)for(i=l=65;i<91;i++)l=a[i]>a[l]?i:l;}

Surprised there wasn't a Java answer yet.

-27 bytes thanks to @OlivierGrégoire.
-6 bytes thanks to @Nevay.

Explanation:

Try it here.

s->{               // Method with character-array parameter and no return-type
  int x=26,        //  Counter-integer, starting at 26
  a[]=new int[96], //  Integer array with 96x 0
  i,l;             //  Index integers
  for(int c:s)     //  Loop (1) over the input-array
    a[c&~32]       //   Convert the character to uppercase, and use it's index in the array
             ++;   //   and increase the integer at that current index by 1
                   //  End of loop (1) (implicit / single-line body)
  for(;x-->0       //  Loop (2) from 26 down to 0 (exclusive)
      ;            //    After every iteration:
       System.out.printf("%c",l+65),
                   //     Print the highest frequency uppercase letter
       a[l]=-1)    //     And then change it's frequency to -1, since we're done with it
    for(i=l=65;    //   Reset both `i` and `l` to 65
        i<91;i++)  //   Inner loop (3) from 65 to 91 (exclusive)
      l=a[i]>a[l]? //    If the current frequency is larger than the `l`'th frequency:
         i         //     Change `l` to the current index `i`
        :          //    Else:
         l;        //     Leave `l` the same
                   //   End of inner loop (3) (implicit / single-line body)
                   //  End of loop (2) (implicit / single-line body)
}                  // End of method

Kevin Cruijssen

Posted 2014-06-30T23:02:35.750

Reputation: 67 575

1System.out.printf("%c",l+65) saves a few bytes. – Olivier Grégoire – 2017-10-24T12:50:34.237

1@OlivierGrégoire Sigh.. I checked if System.out.printf("%c",c) saves any bytes compared to System.out.print((char)c) in another challenge a few hours ago, which it didn't. So I didn't even consider it here anymore.. Obviously it works here due to (l+65) vs l+65.. Thanks! – Kevin Cruijssen – 2017-10-24T12:56:39.260

164 bytes: s->{int x=26,l,a[]=new int[91],i;s.chars().forEach(c->a[c&=~32]-=(64-c&c-91)>>-1);for(;x-->0;System.out.printf("%c",l),a[l]=-1)for(i=l=65;i<91;i++)l=a[i]>a[l]?i:l;} – Nevay – 2017-10-24T13:05:26.140

154 bytes (including @Nevay's changes). Using a char[], as usual ;-) – Olivier Grégoire – 2017-10-24T13:11:26.377

1139 bytes assuming ASCII input (changed length of a to 96 and -=(65-c&c-91)>>-1 to ++). – Olivier Grégoire – 2017-10-24T13:41:13.007

-1 byte: c&~32 instead of c&=~32. – Nevay – 2017-10-24T15:59:38.547

@Nevay My mistake, I didn't review enough ;-) – Olivier Grégoire – 2017-10-25T09:21:06.257

2

APL, 26 20 characters

⎕a[⍒+/⎕a∘.=('\w'⎕r'\u0')⍞]

⎕a[⍒+/⎕a∘.=1(819⌶)⍞]

-6 thanks to Adam.

ngn

Posted 2014-06-30T23:02:35.750

Reputation: 11 449

1('\w'⎕r'\u0')1(819⌶) – Adám – 2017-10-24T08:36:42.117

2

C++ 377

Implements qsort using letter counts in array n to sort alphabet in array A. Run via command line: golf.exe < in.txt

int n[26],c,k,N;
char A[26];
int C(const void*a,const void*b)
{
int i=(int)(*(char*)a -'A');
int j=(int)(*(char*)b -'A');
return n[j]-n[i];
}
int main()
{
for(;k<26;k++)
{
A[k]=k+'A';
}
N=sizeof(A);
c=getchar();
while(c>0)
{
c=toupper(c);
c=c-'A';
if(c>=0&&c<26)n[c]++;
c=getchar();
}
qsort(A,N,1,C);
for(k=0;k<N;k++)
{
putchar(A[k]);
}
return 0;
}

bacchusbeale

Posted 2014-06-30T23:02:35.750

Reputation: 1 235

2

C, 117(119) bytes

x[256];m=1;char c;main(){while(c=getchar()+1)++x[c-1&95];for(;m=x[++c]<x[m]?m:c;x[m<65|m>90||c?m*!c:putchar(m)]=-1);}
  • Some inputs containing ASCII codes >= 128 will incorrectly increase letter frequencies. To fix this, replace the constant 95 with 223, at a cost of 1 extra byte.
  • This will terminate early on inputs containing the character with ASCII code 255. To fix this at the cost of 1 extra byte, change char c; to just c; and ++c to c=c+1%255.

j_random_hacker

Posted 2014-06-30T23:02:35.750

Reputation: 31

2

PowerShell - 139 chars

First, I'm not an PowerShell expert. Pretty sure there are shorter than this. But was happy with it and decided to share.

$a = Read-host
$b = ($a.ToUpper() -replace '[^A-Z]','').ToCharArray() + (65..90|%{[char[]]$_})|Group|sort Count -desc|%{$_.Name}
-join $b

How it works:

$a = Read-host            # read from stdin and save into a string var $a
$a.ToUpper()              # Convert the string to UPPERCASE
-replace'[^A-Z]',''       # Remove all non A-Z characters from the str
(...).ToCharArray()       # Convert the inner object (string) to a Char Array
+  (65..90|%{[char[]]$_}) # Create another char array with A-Z chars expanded, 
                          #  and append it to the previous one.
|Group                    # Group the char array by value for each element, 
                          #  consolidates them and count each char occurrence. Example:
                          #  Count | Name
                          #  ----- | -----
                          #      4 | B
                          #      1 | F
                          #      2 | C 
                          #     .. | ..
                          # 
|sort Count -desc         # Sorts the previous hash-table by the 'Count' column 
                          #   in desc ordering
|%{$_.Name}               # Grab only the 'Name' column from the previous sorted hash-table. 
                          # The retuslt obj will be a simple char array again, 
                          #   with the letters in the desired order
$b = (...)                # Saves the resulting char array into a new variable $b
-join $b                  # join the resulting char array elements into a single 
                          #   string, and print it to stdout. 

Samuel Pizarro

Posted 2014-06-30T23:02:35.750

Reputation: 21

2

Ceylon, 98 bytes

String f(String s)=>String(('A':26).sort(byDecreasing((Object c)=>s.uppercased.count(c.equals))));

gdejohn

Posted 2014-06-30T23:02:35.750

Reputation: 121

1

K4, 16 bytes

Solution:

.Q.A@>+/.Q.a=/:_

Try it online! (Note this is an oK port/demonstration as there is no TIO for K4).

Example:

Note that the input string is stored as variable 'x' for ease:

q)\
  .Q.A@>+/.Q.a=/:_x
"EITUSALNROMCDPVGQBFHJKWXYZ"

Explanation:

.Q.A@>+/.Q.a=/:_  / the solution
               _  / convert to lowercase
            =/:   / equals each right (/:)
        .Q.a      / lowercase alphabet "a..z"
      +/          / sum
     >            / descending sorted indices
    @             / index into
.Q.A              / uppercase alphabet "A..Z"

Notes:

In q/kdb+ it would be 27 bytes:

.Q.A idesc sum .Q.a=/:lower

streetster

Posted 2014-06-30T23:02:35.750

Reputation: 3 635

1

Scala 184 170 136 132

 def c(s:String){print((('A'to'Z').mkString+s).filter(_.isLetter).groupBy(x=>x.toUpper).toSeq.sortBy(-_._2.size).map(_._1).mkString)}

Full Version

  def c(s: String) {
    print((('A' to 'Z').mkString + s).filter(_.isLetter).groupBy(x => x.toUpper).toSeq.sortBy(-_._2.size).map(_._1).mkString)
  }

Sikorski

Posted 2014-06-30T23:02:35.750

Reputation: 171

1You could replace (for(c<-65 to 90)yield c.toChar) with ('A'to'Z'), .length with .size, and the remaining for ... yield with .map – Dan Getz – 2014-07-03T00:20:11.330

And one more character by def f(...)=print(...) instead of def f(...){print(...)} – Dan Getz – 2014-07-03T00:24:31.877

@DanGetz thanks for your suggestions, i implemented it ! I have not added "=" since I m not returning anything, i m printing in func itself. – Sikorski – 2014-07-03T13:14:45.710

In Scala def f(...): Unit = { ... } is considered the normal form of a function that returns nothing, with def f(...) { ... } simply another way of writing the same thing. – Dan Getz – 2014-07-03T14:11:53.050

If isLetter here does the same thing as Java's Character.isLetter, then it's allowing a lot more than 26 characters through the filter. – Trejkaz – 2014-07-06T12:33:43.830

1

R, 111 characters

Another solution in R (using only base R):

f=function(x)cat(names(sort(table(factor(strsplit(gsub("[^A-Z]","",toupper(x)),"")[[1]],LETTERS)),d=T)),sep="")

Explanation:

toupper(x) makes the input upper case.
gsub("[^A-Z]","",...) gets rid of everything non alphabetical.
strsplit(...,"")[[1]] splits the character string into a vector of single characters.
factor(...,LETTERS) makes a factor out of it with uppercase letters as possible levels.
table(...) build a frequency table of each factor levels.
sort(...,d=T) sorts it according to its values, in decreasing order.
names(...) takes the names of the elements (aka the upper case letters).
cat(...,sep="") prints the result to stdout with no separator.
And everything wrapped into a function to make it directly comparable with djurio solution.

Usage:

> f=function(x)cat(names(sort(table(factor(strsplit(gsub("[^A-Z]","",toupper(x)),"")[[1]],LETTERS)),d=T)),sep="")
> f("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent vitae erat velit. Mauris gravida euismod libero ut tincidunt. Phasellus elit dui, consectetur et egestas in, aliquam vitae diam. Donec eget varius ante. Vestibulum cursus diam aliquet, egestas orci quis, placerat dolor. Proin vel nisi lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Aliquam erat volutpat. Etiam libero tortor, ornare id dui eget, posuere dignissim libero. Pellentesque commodo consequat volutpat. Integer hendrerit sapien libero, vel viverra augue facilisis sit amet. Quisque consectetur eget nisl quis dignissim. Ut lacinia pretium quam a placerat.
+ Morbi sed interdum risus, nec pretium lectus. Morbi imperdiet est id accumsan molestie. Duis sed fermentum nisl. Nunc vitae augue mattis, dictum lectus vel, accumsan nisl. Sed ultricies adipiscing rhoncus. Vivamus eu lacus a enim venenatis eleifend. Praesent consectetur tortor non eleifend ultricies. Mauris et odio posuere, auctor erat at, fringilla est. Proin in vestibulum erat. Maecenas congue commodo ante vel varius. Sed tempus mi ut metus gravida, nec dictum libero dapibus. Morbi quis viverra elit. Ut pharetra neque eget lacus tincidunt dictum. Fusce scelerisque viverra tellus et pretium.
+ Fusce varius adipiscing odio. Nulla imperdiet faucibus sem, at rhoncus ipsum adipiscing vitae. Phasellus imperdiet congue lacus et mollis. Nullam egestas mauris magna, et mollis lectus varius ut. Sed sollicitudin adipiscing dolor, vel elementum elit laoreet molestie. Aliquam nec nulla vel sem ultrices ullamcorper. Nullam nec felis magna. Duis sodales orci non justo aliquam tempus. Integer mi diam, tempor sed vulputate et, varius et nunc. Vestibulum sodales ipsum id mi pharetra, ut convallis mi accumsan. Sed dictum volutpat vestibulum.
+ Quisque ac dolor sagittis, aliquam libero at, euismod enim. Nulla ullamcorper posuere nulla vitae varius. Nam at dolor non libero elementum pellentesque in in lorem. Fusce porttitor turpis in quam placerat varius. Donec lorem orci, condimentum eu sapien sit amet, aliquet commodo magna. Quisque sed lectus sit amet arcu euismod accumsan et non nunc. Phasellus placerat congue metus, feugiat posuere leo dictum quis. Sed ultricies feugiat eros dignissim bibendum.
+ Mauris scelerisque consectetur libero eget varius. Aenean neque nunc, ullamcorper vitae orci in, auctor ornare sapien. Nam lacinia molestie imperdiet. Nam vitae mattis nibh. Vestibulum consequat tellus ac nisi sagittis pulvinar. Nullam mollis ornare quam, et venenatis leo porttitor sit amet. Nulla urna neque, dignissim non orci ut, volutpat ultrices erat. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Pellentesque vestibulum tellus nec eros faucibus porta.")
EITUSALNROMCDPVGQBFHJKWXYZ
> f("Based off ETAOIN SHRDLU, your challenge is to write the shortest program or function in any language that outputs the 26 letters of the English alphabet based on their frequency in the input.")
ETNAHORISULFGPBCDYMQWJKVXZ

plannapus

Posted 2014-06-30T23:02:35.750

Reputation: 8 610

1

Retina, 81 75 bytes (not competing)

The language (especially the Sort mode) is newer than the challenge.

;
{T`;L`L`.$
}`[^Z]$
$0$0
T`l`L
Os`.
[^A-Z]

(.)\1*
$0¶
O#$^`.*
$.0
¶

D`.

Try it online

Explanation:

;               # Add the entire alphabet plus a bunch of semicolons into the input
{T`;L`L`.$          # (so that all letters are included in the input at least once)
}`[^Z]$             #
$0$0                #
T`l`L           # Translate all lowercase into uppercase
Os`.            # Sort all characters by code point
[^A-Z]          # Remove non-letters

(.)\1*          # Separate same characters by newlines
$0¶
O#$^`.*         # Sort backwards numerically by the length
$.0
¶               # Remove newlines

D`.             # Remove excess letters

mbomb007

Posted 2014-06-30T23:02:35.750

Reputation: 21 944

0

PHP, 92+1 bytes (late entry)

$r=count_chars(strtoupper($argn));arsort($r);foreach($r as$c=>$n)$c>64&$c<91&&print chr($c);

Run as pipe with -nR or try it online.

Titus

Posted 2014-06-30T23:02:35.750

Reputation: 13 814

0

05AB1E, 9 bytes (Non-competing)

lAΣ¢}ÙRáu

Try it online!


More interesting 10-byte version:

05AB1E, 10 bytes

l{γéJAìÚRá

Try it online!

Magic Octopus Urn

Posted 2014-06-30T23:02:35.750

Reputation: 19 422

0

Perl, 43 bytes

adding 1 for -n, as usual.

I know variable variables are generally a bad idea and shouldn't be used, but hey it's code golf :)

#!perl -n
${uc$_}++for/\w/g}{print+sort{$$b-$$a}A..Z

chinese perl goth

Posted 2014-06-30T23:02:35.750

Reputation: 1 089

0

k4, 21 bytes

  {.Q.A@<<>#:'.Q.a#=_x}
  {.Q.A@<<>#:'.Q.a#=_x}@,/0:`:e
"EITUSALNROMCDPVGQBFHJKWXYZ"

This approach uses the builtin alphabetic constants .Q.a (the lowercase letters) and .Q.A (the uppercase) to do the filtering and translation back to upper. The process:

  • _: convert to lowercase (leaves non-letters untouched)
  • =: group into associative array of characters and their indices
  • .Q.a#: take only lowercase letters (includes empty lists for characters that did not occur)
  • #:': count each value in the array
  • >: get the keys of the array ordered by its values, descending
  • <<: find the position where each item would appear if the list were sorted
  • .Q.A@: apply that list of positions back to the list of uppercase letters

The last two steps are conceptually a bit tricky; they basically allow you to permute one list according to another. See the q documentation for rank for more details.

(Note that if we stop before the translation back to uppercase, we have a correct result, but in lowercase:

  {>#:'.Q.a#=_x}@,/0:`:e
"eitusalnromcdpvgqbfhjkwxyz"

and a code length of 14!)

Aaron Davies

Posted 2014-06-30T23:02:35.750

Reputation: 881

You can change the << to . to get the values out of the dictionary before sorting for a 1 byte saving: {.Q.A@>.#:'.Q.a#=_x} – streetster – 2017-10-24T14:04:42.937

0

PHP 135 Bytes

I know I'm late but I wanted to post anyway:

function a($b){
  $b=strtoupper($b);
  foreach(range('A','Z') as $c){
   $d[$c]=substr_count($b, $c);
  }  
  arsort($d);
  return implode(array_keys($d));
}
echo a($z); //let $z be the supplied string.

golfed:

function a($b){$b=strtoupper($b);foreach(range('A','Z') as $c){$d[$c]=substr_count($b, $c);}arsort($d);return implode(array_keys($d));}echo a($z); 

TecBrat

Posted 2014-06-30T23:02:35.750

Reputation: 222

The echo is how the function is called, so I'm omitting that from the byte count. Just as a CLI script wouldn't include the command. – TecBrat – 2016-03-30T10:38:38.350

0

Factor, 327 261 248 bytes

:o

CONSTANT: a "ABCDEFGHIJKLMNOPQRSTUWXYZ"
: s ( s -- s ) >array [ 1string ] map ;
[ >upper s dup a s [ [ = ] curry ] map [ [ count ] curry ] map [ call( x -- x ) ] with map [ a s union natural-sort ] dip zip [ last ] sort-with [ first ] map "" join ]

Writing this took a while since I'm still learning the language, but it was fun.

Run the above code, then do "string" swap call to call the lambda. Less golfed:

USING: arrays ascii assocs combinators kernel sequences
  sequences.generalizations sets sorting strings ;
IN: input-freq

CONSTANT: alph "ABCDEFGHIJKLMNOPQRSTUWXYZ"

: alphabet-counter ( -- alph-quot )
  alph string>array [
    [ = ] curry
  ] map [
    [ count ] curry
  ] map ;

: string>array ( str -- array )
  >array [ 1string ] map ;

: freq-find ( str -- freqmap )
  >upper string>array dup alphabet-counter
  [ call( x -- x ) ] with map
  [ alph string>array union natural-sort ] dip zip
  [ last ] sort-with [ first ] map "" join ;

cat

Posted 2014-06-30T23:02:35.750

Reputation: 4 989

0

Lua, 226 Bytes

I'm coming in long after this contest has ended, but I though it would be fun to do :). Also, it may be one of the longest submission down here, but actually took me some time to find a clever way to do it. It may still be golfable I'll explain it below, but right now, here's the code:

f,t={},{}(...):upper():gsub("%u",function(c)c=c:byte()-64 f[c]=f[c]and f[c]+1or 1 end)for i=1,26
do c=(i+64 ..''):char()f[i]=f[i]and f[i]or 0 x=f[i]t[x]=t[x]and c..t[x]or c end
for i=#...,0,-1 do io.write(t[i]and t[i] or'')end

It's a full program, taking its input via command-line argument as a string.

Ungolfed and explanations

f={}                       -- f will be our array for counting iteration
t={}                       -- t will be the sorted array
(...):upper()              -- convert the input in all uppercase
     :gsub("%u",function(c)-- then iterate over each uppercase letter in it
  c=c:byte()-64            -- set c at its position in the alphabet
  f[c]=f[c]                -- if f[c] is set (letter already encountered)
       and f[c]+1          -- increment it
       or 1                -- else, we set it to 1
end)
   -- At that point, wev'e counted each character, let's sort based on occurence!
for i=1,26                 -- iterate on the size of the alphabet
do
  c=(i+64 ..''):char()     -- c is the character corresponding to the current i
  f[i]=f[i]and f[i]or 0    -- if c wasn't in the input, initialise a counter at 0
  x=f[i]                   -- set x to the number of occurence of c
  t[x]=t[x]                -- if t[x] is set, two or more letter have the same count
       and c..t[x]         -- so concatenate the string in t[x] with the new letter
       or c                -- else, just set it
end

for i=#...,0,-1            -- iterate from the size of input to 0
do
    io.write(t[i]          -- if we have i occurence of letter(s)
         and t[i]          -- output them
         or'')             -- else, output an empty string
end

This algorythm is really inefficient. It works because arrays don't exist in lua, we only use tables, which are associative arrays. Which means I can map t[123456]='B' even if t[1] doesn't exist. It makes sorting the number of occurence really fast, but if you want to find them, the time will depend on the size of the input you gave to the program.

Katenkyo

Posted 2014-06-30T23:02:35.750

Reputation: 2 857

0

Mathcad, 102 "bytes"

Mathcad doesn't index strings, so the function f converts the string to a vector of its equivalent character codes (subtracting A's code to aid indexing). f then counts each occurence of the letters A..Z (taking 32 of the values of codes >= "a" to implicitly convert case).

Then there's a verbose sequence of nested functions that sort the character codes by count in ascending order, extract the sorted codes in reverse order and add the "A" offset back, and convert the resulting character code vector back into a string.

enter image description here

I interpret Mathcad "bytes" as the number of keyboard / menu operations necessary to create a Mathcad symbol.

  • Identifier names, eg submatrix, are typed in letter-by-letter, hence the byte count = number of characters in the name.
  • Type ' to add a balanced pair of parentheses
  • Array indices are entered by typing the array name and then the character '[' followed by the indices. For c25, this would be the 4 bytes c [ 2 5.
  • Type ctl-shft-# A to create a "for loop"; this results in a for programming construct that automatically displays the for keyword and the element-of symbol, plus three placeholders. The placeholders are for the iteration variable, it's range and the main body of the loop, respectively.
  • Type ] to add a new programming line. See the image below ...

enter image description here

Stuart Bruff

Posted 2014-06-30T23:02:35.750

Reputation: 501

0

Floroid - 37 bytes

Ba:''.y(t(R(P,w(65,91)),fi=a.fk.fm))D

A little late to the party, but here it is. An Floroid port of @undergroundmonorails answer.

This language was made some time after this challenge, therefore this isn't competing.

Yytsi

Posted 2014-06-30T23:02:35.750

Reputation: 3 582

-1

VB.net

Dim r = String.Join("",( From a In "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                         Group Join c In t On a Equals c Into Group
                         Order By Group.Count Descending
                       ).Select(Function(g) g.a))

Adam Speight

Posted 2014-06-30T23:02:35.750

Reputation: 1 234