Skip to content

Commit ed0afec

Browse files
committed
Update diamond.py
Add type hint for the return: -> list[str] to make it explicit. In the middle calculation, len(spaces) could be avoided since spaces is a str of known length—use (letter_index - i) * 2 directly for tiny perf gain, but it's negligible. Consider adding a check for letter == 'A' early to skip the loop if desired, but the current loop handles it fine.
1 parent c9b1bb4 commit ed0afec

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

diamond/diamond.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
CHARS: str = string.ascii_uppercase
1010

1111

12-
def rows(letter: str) -> list:
12+
def rows(letter: str) -> list[str]:
1313
"""
1414
Return the diamond rows from 'A' to ``letter``.
1515
@@ -26,12 +26,13 @@ def rows(letter: str) -> list:
2626

2727
for i, char in enumerate(CHARS[: letter_index + 1]):
2828
# All rows have as many trailing spaces as leading spaces.
29-
spaces: str = " " * (letter_index - i)
29+
spaces_length: int = letter_index - i
30+
spaces: str = " " * spaces_length
3031
# The first/last row contains one 'A'.
31-
if i == 0:
32+
if char == "A":
3233
result.append(spaces + char + spaces)
3334
else:
34-
middle: str = " " * (row_length - 2 - (len(spaces) * 2))
35+
middle: str = " " * (row_length - 2 - (spaces_length * 2))
3536
# All rows, except the first and last,
3637
# have exactly two identical letters.
3738
result.append(spaces + char + middle + char + spaces)

0 commit comments

Comments
 (0)