-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion 27-
89 lines (40 loc) · 1.2 KB
/
Question 27-
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
83
84
85
86
87
88
89
"""
Question: Print Words Vertically
Given a string s. Return all the words vertically in the same order in which they appear in s.
Words are returned as a list of strings, complete with spaces when is necessary. (Trailing spaces are not allowed).
Each word would be put on only one column and that in one column there will be only one word.
Example 1:
Input: s = "HOW ARE YOU"
Output: ["HAY","ORO","WEU"]
Explanation: Each word is printed vertically.
"HAY"
"ORO"
"WEU"
Example 2:
Input: s = "TO BE OR NOT TO BE"
Output: ["TBONTB","OEROOE"," T"]
Explanation: Trailing spaces is not allowed.
"TBONTB"
"OEROOE"
" T"
"""
def return_vertically(input_string):
li=input_string.split()
n=max(len(x) for x in li)
m=len(li)
res=[[" " for j in range(m)] for i in range(n)]
for j in range(m):
x=li[j]
for i in range(len(x)):
res[i][j]=x[i]
ans=[]
for x in res:
y="".join(map(str,x))
y=y.rstrip()
ans.append(y)
return ans
if __name__ == "__main__":
input_string = "HOW ARE YOU"
print(return_vertically(input_string))
input_string = "TO BE OR NOT TO BE"
print(return_vertically(input_string))