Finished
11def rectangles(strings):
2 pass
2 if not strings or not strings[0]:
3 return 0
4
5 rows = len(strings)
6 cols = len(strings[0])
7
8 # Edge Case: Single row or column
9 if rows < 2 or cols < 2:
10 return 0
11
12 count = 0
13
14 # Check all possible top-left corners
15 for i in range(rows):
16 for j in range(cols):
17 # Edge Case: Top-left corner must be '+'
18 if strings[i][j] != '+':
19 continue
20
21 # Check all possible bottom-right corners
22 for k in range(i+1, rows):
23 for l in range(j+1, cols):
24 # Edge Case: Bottom-right corner must be '+'
25 if strings[k][l] != '+':
26 continue
27
28 # Edge Case: Top-right corner must be '+'
29 if strings[i][l] != '+':
30 continue
31
32 # Edge Case: Bottom-left corner must be '+'
33 if strings[k][j] != '+':
34 continue
35
36 # Check horizontal edges
37 valid_rectangle = True
38
39 # Edge Case: Check top edge
40 for col in range(j+1, l):
41 if strings[i][col] != '-' and strings[i][col] != '+':
42 valid_rectangle = False
43 break
44 if not valid_rectangle:
45 continue
46
47 # Edge Case: Check bottom edge
48 for col in range(j+1, l):
49 if strings[k][col] != '-' and strings[k][col] != '+':
50 valid_rectangle = False
51 break
52 if not valid_rectangle:
53 continue
54
55 # Edge Case: Check left edge
56 for row in range(i+1, k):
57 if strings[row][j] != '|' and strings[row][j] != '+':
58 valid_rectangle = False
59 break
60 if not valid_rectangle:
61 continue
62
63 # Edge Case: Check right edge
64 for row in range(i+1, k):
65 if strings[row][l] != '|' and strings[row][l] != '+':
66 valid_rectangle = False
67 break
68 if not valid_rectangle:
69 continue
70
71 count += 1
72
73 return count
74# Handled Edge Cases: empty diagram, single row/column, invalid corners, invalid 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.