-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek 6.py
More file actions
344 lines (231 loc) · 5.16 KB
/
Copy pathWeek 6.py
File metadata and controls
344 lines (231 loc) · 5.16 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
6.1 Count Chars
Write a python program to count all letters, digits, and special symbols respectively from a given string
For example:
Input Result
rec@123
3
3
1
a=input()
c,d,s=0,0,0
for i in range(len(a)):
if(a[i].isalpha()):
c+=1
elif(a[i].isdigit()):
d+=1
else:
s+=1
print(c,d,s,sep="\n")
6.2
Decompress the String
Assume that the given string has enough memory. Don't use any extra space(IN-PLACE)
Sample Input 1
a2b4c6
Sample Output 1
aabbbbcccccc
import re
a=input()
all=re.findall('\d+',a)
all_w=re.findall('[a-z]',a)
b=''
for i,j in zip(all,all_w):
b+=int(i)*j
print(b)
6.3
First N Common Chars
Two string values S1, S2 are passed as the input. The program must print first N characters present in S1 which are also present in S2.
Input Format:
The first line contains S1.
The second line contains S2.
The third line contains N.
Output Format:
The first line contains the N characters present in S1 which are also present in S2.
Boundary Conditions:
2 <= N <= 10
2 <= Length of S1, S2 <= 1000
Example Input/Output 1:
Input:
abcbde
cdefghbb
3
Output:
bcd
Note:
b occurs twice in common but must be printed only once.
a=input()
b=input()
C=''
d=int(input())
for i in range(len(a)):
if(len(C)-d==0):
break
else:
if(a[i]in b):
if(a[i] not in C):
C+=a[i]
print (C)
6.4
Remove Characters
Given two Strings s1 and s2, remove all the characters from s1 which is present in s2.
Constraints
1<= string length <= 200
Sample Input 1
experience
enc
Sample Output 1
xpri
def remove_chars(s1, s2):
return ''.join([char for char in s1 if char not in s2])
s1=input()
s2=input()
result = remove_chars(s1, s2)
print(result)
6.5
Remove Palindrome Words
String should contain only the words are not palindrome.
Sample Input 1
Malayalam is my mother tongue
Sample Output 1
is my mother tongue
For example:
a=[]
a=input()
b=a. split()
for i in b:
k=i.lower()
if k!=k[::-1]:
print(k,end=' ')
6.6
Return Second World in Uppercase
Write a program that takes as input a string (sentence), and returns its second word in uppercase.
For example:
If input is “Wipro Technologies Bangalore” the function should return “TECHNOLOGIES”
If input is “Hello World” the function should return “WORLD”
If input is “Hello” the program should return “LESS”
NOTE 1: If input is a sentence with less than 2 words, the program should return the word “LESS”.
NOTE 2: The result should have no leading or trailing spaces.
For example:
Input Result
Wipro Technologies Bangalore
TECHNOLOGIES
Hello World
WORLD
Hello
LESS
f=input()
s=f.split()
if len(s)>1:
c=s[1]
print(c.upper())
else:
print("LESS")
6.7
Revers String
Reverse a string without affecting special characters. Given a string S, containing special characters and all the alphabets, reverse the string without affecting the positions of the special characters.
Input:
A&B
Output:
B&A
Explanation: As we ignore '&' and
As we ignore '&' and then reverse, so answer is "B&A".
For example:
Input Result
A&x#
x&A#
def reverse_string(s):
s = list(s)
l, r = 0, len(s) - 1
while l < r:
if not s[l].isalpha():
l += 1
elif not s[r].isalpha():
r -= 1
else:
s[l], s[r] = s[r], s[l]
l += 1
r -= 1
return ''.join(s)
# Test Cases
print(reverse_string(input())) # Output: "B&A"
6.8
String characters balance Test
Write a program to check if two strings are balanced. For example, strings s1 and s2 are balanced if all the characters in the s1 are present in s2. The character’s position doesn’t matter. If balanced display as "true" ,otherwise "false".
For example:
Input Result
Yn
PYnative
True
a=input()
b=input()
if a in b:
print("True")
else:
print("False")
6.9
Unique Names
In this exercise, you will create a program that reads words from the user until the user enters a blank line. After the user enters a blank line your program should display each word entered by the user exactly once. The words should be displayed in the same order that they were first entered. For example, if the user enters:
Input:
first
second
first
third
second
then your program should display:
Output:
first
second
third
a,c=[],[]
for i in range(0,5):
b=input()
a.append(b)
for i in range(len(a)):
if(a[i] not in c):
c.append(a[i])
print(a[i])
6.10
Username Domain Extension
Given a string S which is of the format USERNAME@DOMAIN.EXTENSION, the program must print the EXTENSION, DOMAIN, USERNAME in the reverse order.
Input Format:
The first line contains S.
Output Format:
The first line contains EXTENSION.
The second line contains DOMAIN.
The third line contains USERNAME.
Boundary Condition:
1 <= Length of S <= 100
Example Input/Output 1:
Input:
vijayakumar.r@rajalakshmi.edu.in
Output:
edu.in
rajalakshmi
vijayakumar.r
a = input()
ext = a.split('@')[0]
dom = a.split('@')[1].split('.')[0]
userno = a.find('.')
user = a[userno+1:]
print(user)
print(dom, end='\n')
print(ext,end='\n')