-
Notifications
You must be signed in to change notification settings - Fork 1
/
05-fir-tree.py
52 lines (41 loc) · 1.13 KB
/
05-fir-tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import sys
def firtree(tree_size):
# Setup.
width = 1
minus = 2
foot_width = 1
space = ' '
wood = '|'
branch = '*'
# Create the top of the tree, first as uncentered rows.
rows = []
for i in range(1, tree_size + 1):
for j in range(1, i + 3):
rows.append(branch * width)
width = width + 2
rows.append(branch * width)
if i % 2 == 1 and i != 1:
minus += 2
width = width - minus
if i % 2 == 0:
foot_width += 2
# Convert the rows to centered rows.
max_width = len(rows[-1])
centered = [
space * (max_width // 2 - len(r) // 2) + r for r in rows
]
# Build the trunk.
trunk = [
space * int(max_width / 2 - foot_width // 2) + wood * foot_width
for i in range(1, tree_size + 1)
]
# Profit.
return centered + trunk
if __name__ == '__main__':
if len(sys.argv) < 2:
print("usage: ./solution.py PARAM")
elif sys.argv[1] != '0':
size = int(sys.argv[1])
fir = firtree(size)
for row in fir:
print(row)