Skip to content

Instantly share code, notes, and snippets.

@Kwonkyu
Created October 5, 2020 14:27
Show Gist options
  • Save Kwonkyu/7392ff1aa1628a0804b6b608fffc152b to your computer and use it in GitHub Desktop.
Save Kwonkyu/7392ff1aa1628a0804b6b608fffc152b to your computer and use it in GitHub Desktop.
Look-and-say sequence
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