-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathText_Alignment.py
More file actions
39 lines (33 loc) · 1.01 KB
/
Text_Alignment.py
File metadata and controls
39 lines (33 loc) · 1.01 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
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
def words_len(array):
return sum([len(x) for x in array])
def justify(sentence, width):
words = sentence.split(" ")
extra_space = width - words_len(words)
while extra_space > 0 and len(words) > 1:
for i in range(len(words) - 1):
words[i] += " "
extra_space -= 1
if extra_space < 1:
break;
return ''.join(words)
alignment = input()
n = int(input())
map_line = []
max_size = 0
for i in range(n):
text = input()
max_size = max(len(text), max_size)
map_line.append(text)
# Write an action using print
# To debug: print >> sys.stderr, "Debug messages..."
for t in map_line:
if alignment == "LEFT":
print(t.ljust(0))
if alignment == "RIGHT":
print(t.rjust(max_size))
if alignment == "CENTER":
print(t.center(max_size).rstrip())
if alignment == "JUSTIFY":
print(justify(t, max_size))