-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lesson_6_Universal_New_year_tree_functions_01.py
66 lines (55 loc) · 2.81 KB
/
Lesson_6_Universal_New_year_tree_functions_01.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def all_bloks_widths(heights: list) -> ():
"""Обчислити максимальну ширину ялинки.
Args:
heights (list): [description]
Returns:
[type]: [description]
"""
#
# Аналіз даних з метою вирахувати ширину ялинки
# Кожний рядок додає 2 до довжини попереднього рядка.
# Тобто ширина першого низу буде:
# 2 + 2 * "кількість_рядків_1"
# для першої частини.
# Зменшуємо цю шрину на 2 з кожного краю для краси. Маємо:
# 2 + 2 * "кількість_рядків_1" - 4
# Другий крок починається з цієї ширини і додає ще
# 2 * "кількість_рядків_2"
# рядків. Знову віднімаємо 4 зірочки (по 2 з краю).
# Тоді початкова ширина третьої частини буде:
# 2 + 2 * "кількість_рядків_1" - 4 + 2 * "кількість_рядків_2" - 4
# а "низ" третьої частинии буде дорівнювати:
# 2 + 2 * "кількість_рядків_1" - 4 \
# + 2 * "кількість_рядків_2" - 4 \
# + 2 * "кількість_рядків_3"
#
par_centre, i = 2, 0
bottom_widths = [ ]
for height in heights:
bottom_width = par_centre + 2 * height
bottom_widths.append(bottom_width)
par_centre = bottom_width - 4
par_centre += 4
return bottom_widths, par_centre + 4
def print_tree (heights: list, bottom_widths: list, par_centre: int) -> None:
s = '**' # ширина першого верху - 2
for i, height in enumerate(heights):
# Друк наступної частини ялинки
for _ in range(height):
print(s.center(par_centre))
s += "**"
s = (bottom_widths[i] - 4) * '*'
def print_tree_base(cent):
print("||".center(cent))
heights_input = input(
"""
Введіть висоту кожної з трьох частин ялинки.
Три цілих числа через пробіл починаючи з верхньоъ частини.
Увага! числа мають бути більше за 2. Наприклад: -> 4 3 7
-> """
).split()
heights = [int(el) for el in heights_input]
bottom_widths, par_center = all_bloks_widths(heights)
print_tree(heights, bottom_widths, par_center)
# Друк ніжки ялинки
print_tree_base(par_center)