Created
June 24, 2025 21:04
-
-
Save lloydjatkinson/0a75a3cfc8d29b005ca1c2236284399a to your computer and use it in GitHub Desktop.
Excel like cell naming scheme
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
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