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!
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