Created
October 5, 2020 14:27
-
-
Save Kwonkyu/7392ff1aa1628a0804b6b608fffc152b to your computer and use it in GitHub Desktop.
Look-and-say sequence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| class HelloWorld | |
| { | |
| static void Main() | |
| { | |
| String output = "1"; | |
| for (int i = 1; i < 20; i++) | |
| { | |
| int[] typeCount = new int[10]; // from 0 to 9 | |
| typeCount[int.Parse(output[0] + "")]++; | |
| int prev = 0; | |
| String newOutput = ""; | |
| for (int curr = 1; curr < output.Length; curr++) | |
| { | |
| if (output[prev] != output[curr]) | |
| { | |
| // logic to find which value is read and flush to output string. | |
| for (int j = 0; j < 10; j++) | |
| { | |
| if (typeCount[j] > 0) | |
| { | |
| newOutput += j.ToString(); | |
| newOutput += typeCount[j].ToString(); | |
| typeCount[j] = 0; | |
| break; | |
| } | |
| } | |
| } | |
| typeCount[int.Parse(output[curr] + "")]++; | |
| prev++; | |
| } | |
| for (int j = 0; j < 10; j++) | |
| { | |
| if (typeCount[j] > 0) | |
| { | |
| newOutput += j.ToString(); | |
| newOutput += typeCount[j].ToString(); | |
| typeCount[j] = 0; | |
| break; | |
| } | |
| } | |
| output = newOutput; | |
| } | |
| Console.WriteLine(output); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment