Cells in a Range on an Excel Sheet - Problem
Excel Sheet Range Generator
Imagine you're working with an Excel spreadsheet and need to programmatically generate all cell references within a given range. In Excel, cells are represented using a combination of:
For example, cell
The Challenge: Given a range string like
Important: The output should be sorted column-first (like reading a book: left to right, then top to bottom). So for range
Imagine you're working with an Excel spreadsheet and need to programmatically generate all cell references within a given range. In Excel, cells are represented using a combination of:
- Column letters: A, B, C, ... Z, AA, AB, ... (like a base-26 numbering system)
- Row numbers: 1, 2, 3, ... (simple integers)
For example, cell
B3 means column B (2nd column) and row 3.The Challenge: Given a range string like
"A1:F1" (from A1 to F1) or "K1:L2" (from K1 to L2), you need to return all cell references in that rectangular range.Important: The output should be sorted column-first (like reading a book: left to right, then top to bottom). So for range
"A1:B2", the order would be: ["A1", "A2", "B1", "B2"]. Input & Output
example_1.py โ Basic Range
$
Input:
s = "K1:L2"
โบ
Output:
["K1","K2","L1","L2"]
๐ก Note:
The range spans 2 columns (K, L) and 2 rows (1, 2). We generate column-first: all K cells first (K1, K2), then all L cells (L1, L2).
example_2.py โ Single Row Range
$
Input:
s = "A1:F1"
โบ
Output:
["A1","B1","C1","D1","E1","F1"]
๐ก Note:
This is a horizontal range spanning 6 columns but only 1 row. Each column contributes exactly one cell.
example_3.py โ Single Cell
$
Input:
s = "A2:A2"
โบ
Output:
["A2"]
๐ก Note:
Edge case where start and end are the same cell. The range contains only one cell: A2.
Constraints
-
The length of
sis in range[1, 7] -
sconsists of uppercase English letters, digits and':' -
sis a valid range string in the format"<col1><row1>:<col2><row2>" -
1 <= col1 <= col2 <= 26(only single letters A-Z) -
1 <= row1 <= row2 <= 9 - The range is guaranteed to be valid (col1 โค col2 and row1 โค row2)
Visualization
Tap to expand
Understanding the Visualization
1
Parse the Range
Split 'A1:C2' into start coordinates (A,1) and end coordinates (C,2)
2
Column A Processing
Generate all cells in column A: A1, A2 (rows 1 to 2)
3
Column B Processing
Generate all cells in column B: B1, B2 (rows 1 to 2)
4
Column C Processing
Generate all cells in column C: C1, C2 (rows 1 to 2)
5
Combine Results
Final ordered list: [A1, A2, B1, B2, C1, C2]
Key Takeaway
๐ฏ Key Insight: Excel ranges follow column-first ordering, not row-first. This is like reading a book where you finish each page (column) completely before moving to the next page, rather than reading line by line across multiple pages.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code