Find a rectangle frame drawn with a single color in the grid and extract the content inside the frame.
Initial readable solution with explicit loops and variable names.
def solve(grid):
rows = len(grid)
cols = len(grid[0])
best = None
for r in range(rows):
for c in range(cols):
color = grid[r][c]
for h in range(2, rows - r + 1):
for w in range(2, cols - c + 1):
if all(grid[r][c+i] == color for i in range(w)) and \
all(grid[r+h-1][c+i] == color for i in range(w)) and \
all(grid[r+j][c] == color for j in range(h)) and \
all(grid[r+j][c+w-1] == color for j in range(h)):
area = (h-2) * (w-2)
if best is None or area > best[0]:
best = (area, [row[c+1:c+w-1] for row in grid[r+1:r+h-1]])
return best[1]Shortened variable names, removed whitespace, used single-letter vars.
Used walrus operator := and replaced and with * for boolean multiplication.
Replaced multiple all() checks with set union: {v}=={...}|{...}|{...}|{...}
Changed loop variables to use end positions directly (H, W instead of h, w).
Used list slicing g[r][c:W] and concatenation instead of set comprehensions for rows.
Converted to single expression with max() and key=lambda x:x[1].
Put area first in tuple to use natural tuple ordering: ((H-r-2)*(W-c-2), result).
Removed outer brackets - used generator expression instead of list comprehension.
Simplified area calculation since only relative ordering matters: (H-r)*(W-c).
Key optimizations:
2>len({...})instead oflen(...)<2or{v}==...g[r][c:W]+g[H-1][c:W]with unpack for top/bottom rows[z[k]for z in g[r:H]for k in(c,W-1)]for left/right columns using tuple iteration
def solve(g):
R=len(g);C=len(g[0])
return max(((H-r)*(W-c),[x[c+1:W-1]for x in g[r+1:H-1]])for r in range(R)for c in range(C)for H in range(r+2,R+1)for W in range(c+2,C+1)if 2>len({*g[r][c:W]+g[H-1][c:W],*[z[k]for z in g[r:H]for k in(c,W-1)]}))[1]- Single-letter variables:
g,R,C,H,W,r,c,x,z,k - Semicolon chaining:
R=len(g);C=len(g[0]) - Generator expression in max(): Avoids storing list in memory
- Tuple comparison: Put area first for natural ordering
- Set unpacking:
{*list1,*list2}creates union efficiently - Tuple iteration:
for k in(c,W-1)is shorter thanfor k in[c,W-1] - Comparison flip:
2>len(...)saves space vslen(...)<2 - Slice arithmetic: Direct slice extraction avoids temporary variables
- Byte Count: 249
- Score: 2251 (= 2500 - 249)
- All tests passing: Yes (3 train + 1 test)