46
\$\begingroup\$

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.
  • 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.
  • The only other output that is allowed is optional leading or trailing whitespace, such as a trailing newline. No other output inclusing whitespace is allowed.
  • Undefined behavior is allowed for input that does not contain any letters.

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

\$\endgroup\$
8
  • \$\begingroup\$ Is frequency measured for the upper case letters of the input only? \$\endgroup\$
    – Howard
    Jun 30, 2014 at 23:06
  • \$\begingroup\$ @Howard, no, frequency is measured based on all letters but the output is in uppercase only \$\endgroup\$
    – Abraham
    Jun 30, 2014 at 23:10
  • \$\begingroup\$ 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}). \$\endgroup\$
    – Dennis
    Jul 1, 2014 at 4:40
  • \$\begingroup\$ @Dennis I think it's standard to include the function definition in the character count, so I would say go with your last example. \$\endgroup\$
    – Abraham
    Jul 1, 2014 at 10:37
  • 1
    \$\begingroup\$ What 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. \$\endgroup\$
    – Dennis
    Jul 7, 2014 at 23:33

53 Answers 53

1
2
2
\$\begingroup\$

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
\$\endgroup\$
7
  • 1
    \$\begingroup\$ System.out.printf("%c",l+65) saves a few bytes. \$\endgroup\$ Oct 24, 2017 at 12:50
  • 1
    \$\begingroup\$ @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! \$\endgroup\$ Oct 24, 2017 at 12:56
  • \$\begingroup\$ 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;} \$\endgroup\$
    – Nevay
    Oct 24, 2017 at 13:05
  • \$\begingroup\$ 154 bytes (including @Nevay's changes). Using a char[], as usual ;-) \$\endgroup\$ Oct 24, 2017 at 13:11
  • 1
    \$\begingroup\$ 139 bytes assuming ASCII input (changed length of a to 96 and -=(65-c&c-91)>>-1 to ++). \$\endgroup\$ Oct 24, 2017 at 13:41
2
\$\begingroup\$

Japt, 8 bytes

;Bñ@kX l

Try it

;Bñ@kX l     :Implicit input of string U
;B           :Uppercase alphabet
  ñ          :Sort by
   @         :Passing each X through the following function
    kX       :  Case-insensitively remove all occurences of X from U
       l     :  Length
\$\endgroup\$
1
\$\begingroup\$

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)
  }
\$\endgroup\$
5
  • 1
    \$\begingroup\$ You could replace (for(c<-65 to 90)yield c.toChar) with ('A'to'Z'), .length with .size, and the remaining for ... yield with .map \$\endgroup\$
    – Dan Getz
    Jul 3, 2014 at 0:20
  • \$\begingroup\$ And one more character by def f(...)=print(...) instead of def f(...){print(...)} \$\endgroup\$
    – Dan Getz
    Jul 3, 2014 at 0:24
  • \$\begingroup\$ @DanGetz thanks for your suggestions, i implemented it ! I have not added "=" since I m not returning anything, i m printing in func itself. \$\endgroup\$
    – Sikorski
    Jul 3, 2014 at 13:14
  • \$\begingroup\$ 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. \$\endgroup\$
    – Dan Getz
    Jul 3, 2014 at 14:11
  • \$\begingroup\$ 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. \$\endgroup\$
    – Hakanai
    Jul 6, 2014 at 12:33
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 8 bytes

kAµ?⇧nO⌐

Try it Online!

kA       # Uppercase alphabet
  µ    ⌐ # Reverse sorted by...
      O  # Count of...
     n   # This item in...
   ?⇧    # Uppercase input
\$\endgroup\$
1
+500
\$\begingroup\$

BQN, 24 bytes

{'A'+⍒+˝(⥊𝕩-⌜"aA")=⌜↕26}

A tacit function that accepts a single argument.

-8 from ovs.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ (⥊𝕩-⌜"aA")=⌜↕26 from this answer might be helpful. \$\endgroup\$
    – ovs
    Mar 21, 2022 at 10:00
  • \$\begingroup\$ awesome, -8 bytes \$\endgroup\$
    – Razetime
    Mar 21, 2022 at 11:08
1
\$\begingroup\$

Retina, 81 75 bytes

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
\$\endgroup\$
0
\$\begingroup\$

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))
\$\endgroup\$
0
\$\begingroup\$

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.

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

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
\$\endgroup\$
0
\$\begingroup\$

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

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

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 ;
\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ 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} \$\endgroup\$
    – mkst
    Oct 24, 2017 at 14:04
0
\$\begingroup\$

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); 
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – TecBrat
    Mar 30, 2016 at 10:38
0
\$\begingroup\$

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.

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

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.

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

Burlesque, 12 bytes

ZZ:rdf:q[~\m

Try it online!

ZZ  # To caps
:rd # Filter for alpha
f:  # Frequency list (zipped count, char, sorted by frequency)
q[~ # Take tails (char)
\m  # Map-concat
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 9 bytes

lAΣ¢}ÙRáu

Try it online!


More interesting 10-byte version:

05AB1E, 10 bytes

l{γéJAìÚRá

Try it online!

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

Perl 5 -n0, 41 bytes

for$x(A..Z){$;[pos=-split/$x/i].=$x};say@

Try it online!

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

Thunno 2, 8 bytes

kAÞ$RncṄ

Try it online!

Explanation

kAÞ$RncṄ  # Implicit input
kA        # Uppercase alphabet
  Þ    Ṅ  # Reverse-sorted by:
   $R     #  Uppercase input
     nc   #  Count occurrences
          # Implicit output
\$\endgroup\$
0
\$\begingroup\$

Raku, 38 bytes

{[~] ('A'..'Z').sort:{-.uc.comb($^x)}}

Try it online!

  • ('A' .. 'Z').sort: { ... } sorts the capital letters by whatever the brace-delimited anonymous subroutine returns for each.
  • .uc converts the input string to uppercase.
  • .comb($^x) counts how many times the character $^x (the implicit argument to the function) appears in the uppercased input string.
  • - returns the negative of the character count, so the letters are sorted in order of decreasing frequency.
  • [~] joins the sorted letters into a single string.
\$\endgroup\$
0
\$\begingroup\$

Nibbles, 8 bytes (16 nibbles)

+=~|_\$u,+%.@`)$
+=~|_\$u,+%.@`)$        # full program
+=~|_\$u,+%.@`)$$       # with implicit arg added;
   |                    # filter
    _                   # printable characters
                        # (default value for _ when no STDIN)
                        # by
     \$u                #   is it an uppercase character?
 =~                     # now split these letters into groups, sorted by:
        ,               #   length  
         +              #   (after flattening)
          $     $       #   of using this letter to split
            @           #   input
           .            #   mapped
             `)$        #     to uppercase
+                       # and finally flatten the sorted groups

enter image description here

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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