-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern_6.py
More file actions
63 lines (49 loc) · 943 Bytes
/
pattern_6.py
File metadata and controls
63 lines (49 loc) · 943 Bytes
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
"""
rows = 5
____*
___*_*
__*___*
_*_____*
* * * * *
10
*
*_*
*___*
*_____*
*_______*
*_________*
*___________*
* *
* *
* * * * * * * * * *
# main pattern
# spaces: n - 1 - i
i : 0 1 2 3
s : 4 3 2 1
# star: 1
# spaces: 2i - 1
i : 0 1 2 3 4
s : -1 1 3 5
# star: 1 except for first row
# last row
star: n
"""
"""
Time Complexity: O(n^2)
Space Complexity: O()
"""
n = int(input())
# main pattern
for i in range(n - 1): # [n + (n - 1) + (n - 2) + (n - 3) + 3 + 2 + 1] + [0 + 1 + 3 + 5 + 7 + .. + 2n - 1] = n(n + 1) / 2 + n^2 = O(n^2)
# n + (n + 1) + (n + 2) + .. (2n - 1) + 2n = s(2n) - s(n) 4n^2 - n^2 = 3n^2 = O(n^2)
# n - 1 - i + 2i - 1 = n + i
# spaces
print(' ' * (n - 1 - i), end='')
# star
print(end='*')
# spaces
print(end=' ' * (2 * i - 1))
# star
print('' if i == 0 else '*')
# last row
print('* ' * n)