Skip to content

Commit 0736700

Browse files
committed
Refactor flower counting logic in annotate function
Simplified the logic for counting adjacent flowers by replacing multiple helper functions with a single _calc_surrounding_flowers function. Introduced a COORDINATES tuple for neighbor offsets and updated annotate to use the new approach for improved readability and maintainability.
1 parent 9449d62 commit 0736700

1 file changed

Lines changed: 38 additions & 108 deletions

File tree

flower-field/flower_field.py

Lines changed: 38 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
adjacent (horizontally, vertically, diagonally) to each square.
1010
"""
1111

12+
COORDINATES: tuple = (
13+
(-1, -1), (-1, 0), (-1, 1),
14+
(0, -1), (0, 1),
15+
(1, -1), (1, 0), (1, 1),
16+
)
17+
1218

1319
def annotate(garden: list[str]) -> list[str]:
1420
"""
@@ -30,125 +36,49 @@ def annotate(garden: list[str]) -> list[str]:
3036
# empty list
3137
if not garden:
3238
return []
33-
3439
# raise an error when the board receives malformed input
3540
_validate(garden)
36-
37-
new_garden: list[str] = garden.copy()
38-
for i_row, row in enumerate(new_garden):
41+
new_garden: list[str] = []
42+
for i_row, row in enumerate(garden):
43+
new_row: str = ""
3944
for i_col, char in enumerate(row):
40-
if char == " ":
41-
flower_count = 0
42-
flower_count += _calc_flower_top(i_row, i_col, new_garden)
43-
flower_count += _calc_flower_bottom(i_row, i_col, new_garden)
44-
flower_count += _calc_flower_left(i_row, i_col, new_garden)
45-
flower_count += _calc_flower_right(i_row, i_col, new_garden)
46-
47-
if flower_count != 0:
48-
new_garden[i_row] = (
49-
new_garden[i_row][:i_col]
50-
+ str(flower_count)
51-
+ new_garden[i_row][i_col + 1 :]
52-
)
53-
45+
flower_count = _calc_surrounding_flowers(i_row, i_col, garden)
46+
if flower_count != 0:
47+
new_row += str(flower_count)
48+
else:
49+
new_row += char
50+
new_garden.append(new_row)
5451
return new_garden
5552

5653

57-
def _calc_flower_left(i_row: int, i_col: int, garden: list[str]) -> int:
58-
"""
59-
Check for a flower immediately to the left of the current position.
60-
61-
This helper only inspects the single neighbor at ``(i_row, i_col - 1)``
62-
and returns 1 if it contains ``*``; otherwise returns 0.
63-
64-
:param int i_row: Current row index.
65-
:param int i_col: Current column index.
66-
:param list garden: The garden as a list of strings.
67-
:returns: 1 if the left neighbor is a flower; otherwise 0.
68-
:rtype: int
69-
"""
70-
if i_col - 1 >= 0 and garden[i_row][i_col - 1] == "*":
71-
return 1
72-
73-
return 0
74-
75-
76-
def _calc_flower_right(i_row: int, i_col: int, garden: list[str]) -> int:
77-
"""
78-
Check for a flower immediately to the right of the current position.
79-
80-
This helper only inspects the single neighbor at ``(i_row, i_col + 1)``
81-
and returns 1 if it contains ``*``; otherwise returns 0.
82-
83-
:param int i_row: Current row index.
84-
:param int i_col: Current column index.
85-
:param list garden: The garden as a list of strings.
86-
:returns: 1 if the right neighbor is a flower; otherwise 0.
87-
:rtype: int
88-
"""
89-
if i_col + 1 < len(garden[i_row]) and garden[i_row][i_col + 1] == "*":
90-
return 1
91-
92-
return 0
93-
94-
95-
def _calc_flower_top(i_row: int, i_col: int, garden: list[str]) -> int:
96-
"""
97-
Count flowers in the three cells directly above the current position.
98-
99-
Checks the top-left, top, and top-right neighbors when the row above
100-
exists and contains any flowers.
101-
102-
:param int i_row: Current row index.
103-
:param int i_col: Current column index.
104-
:param list garden: The garden as a list of strings.
105-
:returns: Number of ``*`` cells among the three upper neighbors.
106-
:rtype: int
107-
"""
108-
flower_count = 0
109-
110-
if i_row - 1 >= 0 and "*" in garden[i_row - 1]:
111-
# top-left
112-
if i_col > 0 and garden[i_row - 1][i_col - 1] == "*":
113-
flower_count += 1
114-
# top
115-
if garden[i_row - 1][i_col] == "*":
116-
flower_count += 1
117-
# top-right
118-
if i_col + 1 < len(garden[i_row]) and garden[i_row - 1][i_col + 1] == "*":
119-
flower_count += 1
120-
return flower_count
121-
122-
123-
def _calc_flower_bottom(i_row: int, i_col: int, garden: list[str]) -> int:
54+
def _calc_surrounding_flowers(i_row: int, i_col: int, garden: list[str]) -> int:
12455
"""
125-
Count flowers in the three cells directly below the current position.
56+
Count flowers adjacent to the given cell.
12657
127-
Checks the bottom-left, bottom, and bottom-right neighbors when the row
128-
below exists and contains any flowers.
58+
Counts the eight neighboring positions around ``(i_row, i_col)`` when the
59+
current cell is empty (space). If the cell itself is a flower (``*``), the
60+
count remains zero as the caller preserves flowers unchanged.
12961
130-
:param int i_row: Current row index.
131-
:param int i_col: Current column index.
132-
:param list garden: The garden as a list of strings.
133-
:returns: Number of ``*`` cells among the three lower neighbors.
62+
:param int i_row: Row index of the target cell.
63+
:param int i_col: Column index of the target cell.
64+
:param list garden: The rectangular garden representation.
65+
:returns: Number of adjacent flowers (0–8).
13466
:rtype: int
13567
"""
136-
flower_count = 0
137-
138-
if i_row + 1 < len(garden) and "*" in garden[i_row + 1]:
139-
# bottom-left
140-
if i_col > 0 and garden[i_row + 1][i_col - 1] == "*":
141-
flower_count += 1
142-
# bottom
143-
if garden[i_row + 1][i_col] == "*":
144-
flower_count += 1
145-
# bottom-right
146-
if i_col + 1 < len(garden[i_row]) and garden[i_row + 1][i_col + 1] == "*":
147-
flower_count += 1
148-
return flower_count
149-
150-
151-
def _validate(garden: list[str]):
68+
...
69+
total: int = 0
70+
if garden[i_row][i_col] == " ":
71+
# Count flowers all around current position
72+
for row, col in COORDINATES:
73+
# Avoid IndexError
74+
if 0 <= i_row + row < len(garden) and 0 <= i_col + col < len(garden[0]):
75+
# Detect and count flower
76+
if garden[i_row + row][i_col + col] == "*":
77+
total += 1
78+
return total
79+
80+
81+
def _validate(garden: list[str]) -> None:
15282
"""
15383
Validate the garden shape and contents.
15484

0 commit comments

Comments
 (0)