forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhalf triangle pattern.py
More file actions
82 lines (62 loc) · 1.67 KB
/
half triangle pattern.py
File metadata and controls
82 lines (62 loc) · 1.67 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# (upper half - repeat)
#1
#22
#333
# (upper half - incremental)
#1
#12
#123
# (lower half - incremental)
#123
#12
#1
# (lower half - repeat)
#333
#22
#1
def main():
lines = int(input("Enter no.of lines: "))
pattern = input("i: increment or r:repeat pattern: ").lower()
part = input("u: upper part or l: lower part: ").lower()
match pattern:
case "i":
if part == "u":
upper_half_incremental_pattern(lines)
else:
lower_half_incremental_pattern(lines)
case "r":
if part == "u":
upper_half_repeat_pattern(lines)
else:
lower_half_repeat_pattern(lines)
case _:
print("Invalid input")
exit(0)
def upper_half_repeat_pattern(lines):
t = 1
for column in range(1, (lines +1)):
print(f"{str(t) * column}")
t += 1
def upper_half_incremental_pattern(lines):
for column in range(1, (lines +1)):
row = ""
for ii in range(1, column +1):
row += str(ii)
print(row)
def lower_half_incremental_pattern(lines):
for row_length in range(lines, 0, -1):
row = ""
column = 1
for _ in range(row_length):
column = 0 if column == 10 else column
row = f"{row}{column}"
column += 1
print(row)
def lower_half_repeat_pattern(lines):
for row_length in range(lines, 0, -1):
row = ""
for _ in range(1, row_length+1):
row += str(row_length)
print(row)
if __name__ == "__main__":
main()