Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given a string, find the 10 least common alpha-numeric characters within that string.

Output should be formatted with numbers left-padded with spaces so they look like this:

e:      1
t:     16
c:     42
b:    400
z:  50000
a: 100000

Use this to test your program: here

Output for test case:

j:  2
x:  3
q:  9
k: 32
v: 67
c: 73
w: 79
f: 93
b: 94
p: 96

Shortest code wins. You can submit a program or a function.

share|improve this question
Do you want a function or a whole program? – marinus Oct 25 '12 at 4:14
@marinus: A program or a function, whichever is shorter for you. – beary605 Oct 25 '12 at 4:23
I presume from your output to the test case that uppercase and lowercase letters are treated as the same? – Gareth Oct 25 '12 at 10:31
@Gareth: Yes, they should be treated the same. – beary605 Oct 25 '12 at 14:17
up vote 0 down vote accepted

K, 60

{(-:|/#:'f)$f:10#$(!x)[i]!r i:<r:. x:#:'=_x@&x in,/.Q`a`A`n}
share|improve this answer

Mathematica, 102 93:


              Last@# &],


         SortBy[Tally@StringCases[ToLowerCase@#, x_?LetterQ :> x <> ":"],
                #[[2]] &], 
         TableAlignments -> Right] &
share|improve this answer
You beat me to it (and had more streamlined code). – DavidC Oct 25 '12 at 18:58
@DavidCarraher But MMa is SO verbose! :( – Dr. belisarius Oct 25 '12 at 23:35
I detect a hint of irony in your comment. – DavidC Oct 26 '12 at 16:59

Bash, 72

Assuming no single-letter-named files in the current directory; input from stdin:

grep -io [a-z]|sort -f|uniq -ci|sort|head|sed -E 's/(.*)([a-z])/\2:\1/'

Works on Linux, but for some reason, my mac's grep seems to ignore the -i when used with -o (bug or undocumented gotcha?), but the following (78) works:

tr A-Z a-z|grep -o [a-z]|sort|uniq -c|sort|head|sed -E 's/(.*)([a-z])/\2:\1/'

If the output were allowed to be reversed, the sed part could be dropped to save 30 characters in each of the previous solutions.

share|improve this answer

Python, 206 199 195 characters

import sys
for c in t:
 if c.isalnum():f[c]=f.get(c,0)+1
s=lambda x:x[1]
for n in h:print n[0]+': '+`n[1]`.rjust(len(`max(h,s)[1]`))

Undoubtedly there is a lot of room for improvement, but here's my first attempt. Produces identical output for the test case provided.

206 -> 199: Removed unnecessary newline and keyword argument

199 -> 195: iteritems()

share|improve this answer

Javascript, 271 characters

I'm not sure whether this breaks the rules of not (it doesn't justify the numbers in the output, but here's my solution anyway:

c=[];t=128;while(t--)c[t]=[0,t];i=prompt().toLowerCase();for(n=0;n<i.length;x=i.charCodeAt(n++),c[x][0]+=x>96&&x<123);for(n=i=0,c=c.sort(function(a,b){return(a[0]-b[0])});n<10;i++)if(c[i][0]>0){console.log(String.fromCharCode(c[i][1])+": "+("  "+c[i][0]).slice(-2));n++}

I'm pretty sure it can be simplified (I won't win with this character count :P), but now I can't find anything.

Okay, so I completely forgot that the padding was actually a requirement :O Corrected that and fixed a bug. (250 -> 271)

share|improve this answer

Haskell (229)

import Char
import Data.List
k x=[(l$z(==m)x,m)|m<-x]
s z=[b:':':take(1+(maximum$map(l.t.fst)z)-l(t a))(repeat ' ')++t a++"\n"|(a,b)<-z]
main=interact$concat.s.take toLower.z isAlphaNum
share|improve this answer

Perl, 86 chars

given string is in $_

share|improve this answer

Python, 144

import os,10000).lower()
for c in a:print c+': %%%ii'%len(`n(a[-1])`)%n(c)
share|improve this answer
I just proposed an edit that saves you 14 characters by switching to the 'raw_input()' function. – ESultanik Oct 25 '12 at 14:25
@ESultanik The problem is that raw_input() terminates on newlines, so it would stop after the first line of input in the supplied test case. – Strigoides Oct 25 '12 at 15:38
@Strigoides: Good point; I missed that the input could have newlines. My fault for not looking at the test cases. – ESultanik Oct 25 '12 at 15:48
print c+': %%%ii'%len(n(a[-1]))%n(c) saves 3 characters – Daniel Oct 27 '12 at 9:47
@Daniel: Very nice; thanks. – grc Oct 27 '12 at 11:34

JavaScript 1.8 (ES6) 186

  s.slice(0,10).map(x=>x+': '+('      '+n[x]).slice(-(n[s[9]]+'').length))
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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