### Look and Say Sequence

26Jul13

In the twelve months from March 2013 to March 2014, I programmed solutions to the problems posted on the Contest Coding blog run by Lewis Cornwall, solving 33 problems (out of 47) and achieving a position of 4^{th} on the leaderboard (out of 23).

As that blog has now been discontinued, I’m posting here the solutions I programmed to those problems.

Look and Say SequenceThe “look and say sequence” is defined by reading off the previous number, counting the digits in a consecutive group – then using that number as the next term. For example, this is the look and say sequence beginning with 1:

- 1 is read as “one 1″, or 11, so the next term is 11
- 11 is read as “two 1s”, or 21, so the next term is 21
- 21 is read as “one 2, then one 1″, or 1211, so the next term is 1211
Find the 8th term (including the first, 1-digit term) of the look and say sequence beginning with 3.

**Solution and answer (LookAndSaySequence.java):**

/* Solution and answer for problem "Look and Say Sequence" (26th July 2013) of http://ContestCoding.WordPress.com/ Fri Jul 26 09:00:00 GMT+01:00 2013 The 8th term of the "look and say sequence" beginning with 3 is: 13211321322113 Fri Jul 26 09:00:00 GMT+01:00 2013 Solution programmed in Java using Metrowerks CodeWarrior IDE 2.1 (Discover Programming Edition); solution took ~1s to run on a 80MHz PowerPC 601. */ import java.util.Date; public class LookAndSaySequence { public static void main( String p[] ) { System.out.println( new Date()); String s = "3"; for( int i = 2; i <= 8; i ++ ) s = nextTerm( s ); System.out.println( "\nThe 8th term of the \"look and say sequence\" beginning with 3 is: " + s + "\n" ); System.out.println( new Date()); } private static String nextTerm( String p ) { char c = p.charAt( p.length() - 1 ); int c_n = 1; String s = ""; for( int i = p.length() - 2; i >= 0; i -- ) if( c == p.charAt( i )) c_n ++; else { s = "" + c_n + c + s; c = p.charAt( i ); c_n = 1; } return "" + c_n + c + s; } }

Advertisements

Filed under: Uncategorized | Leave a Comment

Tags: computer science, computing, contest, Contest Coding, Java, Lewis Cornwall

## No Responses Yet to “Look and Say Sequence”