Skip to content

Commit 98ad45e

Browse files
authored
Merge pull request #202 from ikostan/main
Merge from master
2 parents 833e7ff + f2d6697 commit 98ad45e

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

diamond/diamond.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
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
1616
Builds the upper half and mirrors it to form a symmetric diamond.
1717
18-
:param str letter: Uppercase letter (``'A'``-``'Z'``) marking the widest row.
18+
:param str letter: Uppercase letter (``'A'``-``'Z'``) marking the
19+
widest row.
1920
:returns: The full diamond as a list of strings, one per row.
2021
:rtype: list
2122
:raises ValueError: If ``letter`` is not an ASCII uppercase character.
@@ -26,12 +27,13 @@ def rows(letter: str) -> list:
2627

2728
for i, char in enumerate(CHARS[: letter_index + 1]):
2829
# All rows have as many trailing spaces as leading spaces.
29-
spaces: str = " " * (letter_index - i)
30+
spaces_length: int = letter_index - i
31+
spaces: str = " " * spaces_length
3032
# The first/last row contains one 'A'.
31-
if i == 0:
33+
if char == "A":
3234
result.append(spaces + char + spaces)
3335
else:
34-
middle: str = " " * (row_length - 2 - (len(spaces) * 2))
36+
middle: str = " " * (row_length - 2 - (spaces_length * 2))
3537
# All rows, except the first and last,
3638
# have exactly two identical letters.
3739
result.append(spaces + char + middle + char + spaces)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
The diamond kata takes as its input a letter, and outputs it in
3+
a diamond shape. Given a letter, it prints a diamond starting
4+
with 'A', with the supplied letter at the widest point.
5+
"""
6+
7+
import string
8+
9+
CHARS: str = string.ascii_uppercase
10+
11+
12+
def rows(letter: str) -> list:
13+
"""
14+
Return the diamond rows from 'A' to ``letter``.
15+
16+
Builds the upper half and mirrors it to form a symmetric diamond.
17+
18+
:param str letter: Uppercase letter (``'A'``-``'Z'``) marking the widest row.
19+
:returns: The full diamond as a list of strings, one per row.
20+
:rtype: list
21+
:raises ValueError: If ``letter`` is not an ASCII uppercase character.
22+
"""
23+
result: list = []
24+
letter_index: int = CHARS.index(letter)
25+
row_length: int = (letter_index * 2) + 1
26+
27+
for i, char in enumerate(CHARS[: letter_index + 1]):
28+
# All rows have as many trailing spaces as leading spaces.
29+
spaces: str = " " * (letter_index - i)
30+
# The first/last row contains one 'A'.
31+
if i == 0:
32+
result.append(spaces + char + spaces)
33+
else:
34+
middle: str = " " * (row_length - 2 - (len(spaces) * 2))
35+
# All rows, except the first and last, have exactly two identical letters.
36+
result.append(spaces + char + middle + char + spaces)
37+
# Mirror the list: the bottom half has the letters in descending order.
38+
result = result + result[::-1][1:]
39+
return result

0 commit comments

Comments
 (0)