Skip to content

Commit d1b274b

Browse files
committed
Refactor annotate function for conciseness
Simplified the annotate function by replacing the loop and intermediate list with a more concise list comprehension and string join. This improves readability and reduces code complexity.
1 parent 991c374 commit d1b274b

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

flower-field/flower_field.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,15 @@ def annotate(garden: list[str]) -> list[str]:
4343
return []
4444
# raise an error when the board receives malformed input
4545
_validate(garden)
46-
new_garden: list[str] = []
47-
for i_row, row in enumerate(garden):
48-
new_row = [
49-
str(_calc_surrounding_flowers(i_row, i_col, garden))
50-
if _calc_surrounding_flowers(i_row, i_col, garden) != 0
46+
return [
47+
"".join(
48+
str(count)
49+
if (count := _calc_surrounding_flowers(i_row, i_col, garden)) != 0
5150
else char
5251
for i_col, char in enumerate(row)
53-
]
54-
new_garden.append("".join(new_row))
55-
return new_garden
52+
)
53+
for i_row, row in enumerate(garden)
54+
]
5655

5756

5857
def _calc_surrounding_flowers(i_row: int, i_col: int, garden: list[str]) -> int:

0 commit comments

Comments
 (0)