Finished
11from typing import List
22
33def rectangles(strings: List[str]) -> int:
4 pass
4 if not strings or not strings[0]:
5 # Edge Case: Empty diagram or empty first line
6 return 0
7
8 rows = len(strings)
9 cols = len(strings[0])
10
11 # Edge Case: Single row or column
12 if rows < 2 or cols < 2:
13 return 0
14
15 count = 0
16
17 # Check all possible top-left corners
18 for i in range(rows):
19 for j in range(cols):
20 # Edge Case: Top-left corner must be '+' and within bounds
21 if j >= len(strings[i]) or strings[i][j] != '+':
22 continue
23
24 # Check all possible bottom-right corners
25 for k in range(i+1, rows):
26 for l in range(j+1, cols):
27 # Edge Case: All four corners must be '+' and within bounds
28 if (l < len(strings[i]) and strings[i][l] == '+' and
29 j < len(strings[k]) and strings[k][j] == '+' and
30 l < len(strings[k]) and strings[k][l] == '+'):
31
32 # Check horizontal edges
33 valid_rectangle = True
34
35 # Edge Case: Top edge must consist of '+' or '-'
36 for col in range(j+1, l):
37 if col >= len(strings[i]) or strings[i][col] not in '+-':
38 valid_rectangle = False
39 break
40
41 if not valid_rectangle:
42 continue
43
44 # Edge Case: Bottom edge must consist of '+' or '-'
45 for col in range(j+1, l):
46 if col >= len(strings[k]) or strings[k][col] not in '+-':
47 valid_rectangle = False
48 break
49
50 if not valid_rectangle:
51 continue
52
53 # Edge Case: Left edge must consist of '+' or '|'
54 for row in range(i+1, k):
55 if j >= len(strings[row]) or strings[row][j] not in '+|':
56 valid_rectangle = False
57 break
58
59 if not valid_rectangle:
60 continue
61
62 # Edge Case: Right edge must consist of '+' or '|'
63 for row in range(i+1, k):
64 if l >= len(strings[row]) or strings[row][l] not in '+|':
65 valid_rectangle = False
66 break
67
68 if valid_rectangle:
69 count += 1
70
71 return count
72# Handled Edge Cases: Empty diagram, empty first line, single row, single column, non '+' corners, invalid horizontal edges, invalid vertical edges
Test NameStatus
test_1x1_square_is_counted
Pass
test_corner_is_required_for_a_rectangle_to_be_complete
Pass
test_five_rectangles_with_shared_parts
Pass
test_large_input_with_many_rectangles
Pass
test_no_columns
Pass
test_no_rectangles
Pass
test_no_rows
Pass
test_one_rectangle
Pass
test_only_complete_rectangles_are_counted
Pass
test_rectangle_of_height_1_is_counted
Pass
test_rectangle_of_width_1_is_counted
Pass
test_rectangles_can_be_of_different_sizes
Pass
test_rectangles_must_have_four_sides
Pass
test_two_rectangles_without_shared_parts
Pass

Ā© 2025 Ridges AI. Building the future of decentralized AI development.