Skip to content

Commit 9dd9746

Browse files
bites 146
1 parent a54fa0b commit 9dd9746

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,4 @@
6262
/163/README.md
6363
/Pipfile
6464
/Pipfile.lock
65+
/146/README.md

146/rhombus.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
STAR = '*'
2+
3+
4+
def gen_rhombus(width):
5+
"""Create a generator that yields the rows of a rhombus row
6+
by row. So if width = 5 it should generate the following
7+
rows one by one:
8+
9+
gen = gen_rhombus(5)
10+
for row in gen:
11+
print(row)
12+
13+
output:
14+
*
15+
***
16+
*****
17+
***
18+
*
19+
"""
20+
21+
for rowz in range(1, (width * 2) + 1, 2):
22+
if rowz-width <= 0:
23+
yield '{:^{width}}'.format(STAR * rowz, width=width)
24+
else:
25+
multiplierOf4 = (rowz-width)//2
26+
yield '{:^{width}}'.format(STAR * (rowz-4*multiplierOf4), width=width)
27+
28+
gen = gen_rhombus(5)
29+
for row in gen:
30+
print(row)

146/test_rhombus.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from rhombus import gen_rhombus
2+
3+
4+
def test_rhombus_width3():
5+
# recommended: actual before expected
6+
# https://twitter.com/brianokken/status/1063337328553295876
7+
actual = list(gen_rhombus(3))
8+
expected = [' * ', '***', ' * ']
9+
assert actual == expected
10+
11+
12+
def test_rhombus_width5():
13+
actual = list(gen_rhombus(5))
14+
expected = [' * ', ' *** ', '*****',
15+
' *** ', ' * ']
16+
assert actual == expected
17+
18+
19+
def test_rhombus_width11():
20+
"""print('\n'.join(expected)) would give (ignore indents):
21+
*
22+
***
23+
*****
24+
*******
25+
*********
26+
***********
27+
*********
28+
*******
29+
*****
30+
***
31+
*
32+
"""
33+
actual = list(gen_rhombus(11))
34+
expected = [' * ', ' *** ', ' ***** ',
35+
' ******* ', ' ********* ', '***********', ' ********* ',
36+
' ******* ', ' ***** ', ' *** ', ' * ']
37+
assert actual == expected

0 commit comments

Comments
 (0)