-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern1.py
52 lines (33 loc) · 1.29 KB
/
pattern1.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
# Generalized function for this with 4 orientations-
# #
# #-#
# #---#
# #-----#
from sys import argv
HASH, SPACE, HYPHEN = "#", ' ', '-'
LF, BACKSPACE = "\n", '\b'
SCREEN_WIDTH = 60
SP = HASH
RP = HYPHEN+HYPHEN
EP = BACKSPACE+HASH
#ORIENTATIONS = 'RIGHT', 'PYRAMID', 'INVERSE', 'DIAMOND'
def pattern(size: int, orientation='PYRAMID', width=SCREEN_WIDTH):
if orientation == 'PYRAMID':
return LF.join([line(line_no, width) for line_no in range(size)])
elif orientation == 'RIGHT':
return LF.join([line(line_no, 0) for line_no in range(size)])
elif orientation == 'DIAMOND':
return LF.join([line(line_no, width) for line_no in range(size)])+LF+LF.join([line(line_no, width) for line_no in range(size-1)][::-1])
else: # orientation =="INVERSE"
return LF.join([line(line_no, width) for line_no in range(size)][::-1])
def line(n: int, width: int) -> str:
return leading_space(n, width)+start_pattern(n)+repeat_pattern(n)+end_pattern(n)
def leading_space(n: int, w: int) -> str:
return (w // 2 - n) * SPACE
def start_pattern(n: int):
return SP
def repeat_pattern(n: int):
return n*RP
def end_pattern(n: int):
return EP
print(pattern(int(argv[1]), argv[2]))