They are defined very simply. Start with an integer, say 1. Then we look at the integer and say it consists of one 1, and write this as 11, the next element in the sequence. Then we look at 11 and say that it consists of two 1's, or 21. Then 21 would be described as one 2 and one 1, or 1211. And so on.
The golf challenge is to write a program to print out the first 10 elements of a look and say sequence from a specified integer in the fewest number of keystrokes. The first 10 elements for the look and say sequence generated from the integer 1 are:
@test = qw|1 11 21 1211 111221 312211 13112221 1113213211 31131211131221 13211311123113112211|;
Here is a strawman solution that surely could be improved upon:
$s=1;print"$s\n";for(2..10){@n=split//,$s;my@r;$c=$n[0];$l=0;while(@n){$n=shift@n;if($n==$c){$l++}else{push@r,[$l,$c];$c=$n;$l=1;}}push@r,[$l,$c];$s =join"",map{"$_->[0]$_->[1]"}@r;print"$s\n";}
Bonus question: Can you find an integer that is its own look and say description?
-Mark
20050412 Edit by ysth: code tags
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Golf: Look and Say Sequences
by japhy (Canon) on Apr 12, 2005 at 04:33 UTC | |
Re: Golf: Look and Say Sequences
by Tanktalus (Canon) on Apr 12, 2005 at 05:13 UTC | |
Re: Golf: Look and Say Sequences
by thospel (Hermit) on Apr 13, 2005 at 17:40 UTC |