Skip to content

Instantly share code, notes, and snippets.

@lloydjatkinson
Created June 24, 2025 21:04
Show Gist options
  • Save lloydjatkinson/0a75a3cfc8d29b005ca1c2236284399a to your computer and use it in GitHub Desktop.
Save lloydjatkinson/0a75a3cfc8d29b005ca1c2236284399a to your computer and use it in GitHub Desktop.
Excel like cell naming scheme
foreach (var seat in TheatreSeatGenerator.GenerateSeats(300, 50))
{
Console.WriteLine(seat);
}
public class TheatreSeatGenerator
{
private static string SequentialName(int columnNumber)
{
int dividend = columnNumber + 1;
string columnName = String.Empty;
int modulo;
while (dividend > 0)
{
modulo = (dividend - 1) % 26;
columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
dividend = ((dividend - modulo) / 26);
}
return columnName;
}
public static IEnumerable<string> GenerateSeats(int rows, int seatsPerRow)
{
int currentRow = 0;
int currentSeat = 1;
while (currentRow < rows)
{
yield return SequentialName(currentRow) + currentSeat;
currentSeat++;
if (currentSeat > seatsPerRow)
{
currentSeat = 1;
currentRow++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment