Run-Length Encoding
sh - 33 29 28
read;echo`xargs -n1|uniq -c`
Usage:
$ cat input|sh 1015.sh
2 1 1 3 2 2
read
skips the first linexargs -n1
reads the reast and outputs each number on one line:1 1 3 2 2
uniq -c
filters adjacent matching lines (with thec
switch it also prints the number of adjancent lines) :2 1 1 3 2 2
echo
sees those numbers as separate arguments and just prints them separated by a space:2 1 1 3 2 2
Perl, 46 56 68
$_=<>;s/(?{$a=1})(\d+)( \1(?{++$a}))*/$a $1/g
Run with the p
command-line option (counted in code size):
$ perl -pe '$_=<>;s/(?{$a=1})(\d+)( \1(?{++$a}))*/$a $1/g'
5
1 1 3 2 2
=> 2 1 1 3 2 2
3
1 1 1
=> 3 1
Haskell (84 82)
import List
main=interact$unwords.(>>= \x->[show$length x,x!!0]).group.tail.words
Number of elements in the list is ignored.