-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPPL.py
More file actions
556 lines (521 loc) · 20.6 KB
/
PPL.py
File metadata and controls
556 lines (521 loc) · 20.6 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
import os
import platform
# class for using this function
class LOP():
def __init__(self,number):
self.number = number
def lop(self):
return """
1. Write a Python program to count the number of even/odd numbers in a given list.
2. Write a Python program to calculate the number of digits and letters in a given text.
3. Write a Python program to check the validity of a password validation:
a. At least 1 letter between [a-z] and 1 letter between [A-Z].
b. At least 1 number between [0-9].
c. At least 1 character from [$#@].
d. Minimum length 6 characters.
e. Maximum length 16 characters.
4. Write a Python program to calculate the sum and average of n integer numbers (input from the user). Input 0 to finish.
5. Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters.
6. Write a Python program to calculate the Fibonacci series using while loop.
7. Write a python program to find the factorial of a given number using recursion function.
8. Write a python program to implement the binary search algorithm.
9. Write a python program to find the most frequent words in a text read from a file.
10. Write a python program to perform the matrix addition, subtraction and multiplication."""
class Aim(LOP):
def aim(self):
match self.number:
case 1: return """ Write a Python program to count the number of even/odd numbers in a given list."""
case 2: return """ Write a Python program to calculate the number of digits and letters in a given text."""
case 3: return """ Write a Python program to check the validity of a password validation:
a. At least 1 letter between [a-z] and 1 letter between [A-Z].
b. At least 1 number between [0-9].
c. At least 1 character from [$#@].
d. Minimum length 6 characters.
e. Maximum length 16 characters."""
case 4: return """ Write a Python program to calculate the sum and average of n integer numbers (input from the user). Input 0 to finish."""
case 5: return """ Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters."""
case 6: return """ Write a Python program to calculate the Fibonacci series using while loop."""
case 7: return """ Write a python program to find the factorial of a given number using recursion function."""
case 8: return """ Write a python program to implement the binary search algorithm."""
case 9: return """ Write a python program to find the most frequent words in a text read from a file."""
case 10: return """ Write a python program to perform the matrix addition, subtraction and multiplication."""
class Algorithm(Aim):
def algorithm(self):
match self.number:
case 1: return ' coming soon'
case 2: return ' coming soon'
case 3: return ' coming soon'
case 4: return ' coming soon'
case 5: return ' coming soon'
case 6: return ' coming soon'
case 7: return ' coming soon'
case 8: return ' coming soon'
case 9: return ' coming soon'
case 10: return ' coming soon'
class Program(Algorithm):
def one(self, l):
odd, even = 0, 0
for i in l:
if i%2==0: even+=1
else: odd+=1
return odd, even
def two(self, s):
digits, letters = 0, 0
for i in s:
if i.isdigit(): digits+=1
elif i.isalpha(): letters+=1
else: pass
return digits, letters
def three(self, s):
b = True
option = None
l = []
while True:
if not any(s.islower() for s in s): b = False; option = 'a'; break
elif not any(s.isupper() for s in s): b = False; option = 'b'; break
elif not any(s.isdigit() for s in s): b = False; option = 'c'; break
elif not any(s in ['$','#','@'] for s in s): b = False; option = 'd'; break
elif len(s) < 6: b = False; option = 'e'; break
elif len(s) > 16: b = False; option = 'f'; break
else: break
if b: return "Valid Password", option
else: return "Invalid Password", option
def four(self, i):
count, sum = 0, 0
if i != 0:
sum+=i
count+=1
while i !=0:
while True:
try:
i = int(input(' Enter the number (0 to finish): ')); break
except: print('\n Invalid Input (e.g. 1 or 2 or 3 etc.)\n')
sum = sum+i
count+=1
if count == 0:
return 0, 0
else: return sum, sum/(count-1)
def five(self, s):
upper, lower = 0, 0
for i in s:
if i.isupper(): upper+=1
elif i.islower: lower+=1
else: pass
return upper, lower
def six(self, i):
x, y, l = 0, 1, []
while y <= i:
l.append(y)
x, y = y, x+y
return str(l)[1:-1]
def seven(self, i):
if i == 1: return 1
else: return i * (self.seven(i-1))
def eight(self, l, i):
low, high = 0, len(l)-1
b = False
while(low <= high and not b):
mid = (low + high)//2
if l[mid] == i:
b = True
else:
if i < l[mid]:
high = mid - 1
else:
low = mid + 1
return b, mid, low, high
def nine(self, s):
file = open("text.txt", 'w')
file.write(s)
file = open("text.txt", 'r')
word = " "
freq = 0
words = []
for line in file:
splitted_words = line.replace(',',' ').replace('.',' ').split(" ")
for w in splitted_words:
words.append(w)
for i in range(0, len(words)):
count = 1
for j in range(i+1, len(words)):
if words[i] == words[j]:
count+=1
if(count>freq):
freq = count
word = words[i]
file.close()
os.remove('text.txt')
return word, freq
def ten(self, r, c):
mat, rix = [], []
rows, columns = r, c
print()
while rows > 0:
rows-=1
while True:
try:
l = list(map(int,input(' Enter the row for the first matrix (separated by spaces): ').split(" ")))
if len(l) != r: raise ValueError(); break
else: mat.append(l); break
except: print('\n Invalid Input (e.g. 1 2 3 etc.)\n')
rows = r
while rows > 0:
rows-=1
while True:
try:
l = list(map(int,input(' Enter the row for the second matrix (separated by spaces): ').split(" ")))
if len(l) != r: raise NameError(); break
else: rix.append(l); break
except NameError: print('\n Invalid Input (column limit is {})\n'.format(c))
except ValueError: print('\n Invalid Input (e.g. 1 2 3 etc.)\n')
rows = r
result = []
for i in range(rows):
row = []
for j in range(c):
row.append(0)
result.append(row)
while True:
print('\n[1 - addition | 2 - subtraction | 3 - multiplication | e - exit]\n')
operation = input('Select operation: ')
if operation == '1':
print('\nOutput:\n')
for i in range(r):
for j in range(c):
result[i][j] = mat[i][j] + rix[i][j]
for ii in result:
print(' {}'.format(''.join(str(ii).split(','))[1:-1]))
elif operation == '2':
print('\nOutput:\n')
for i in range(r):
for j in range(c):
result[i][j] = mat[i][j] - rix[i][j]
for ii in result:
print(' {}'.format(''.join(str(ii).split(','))[1:-1]))
elif operation == '3':
print('\nOutput:\n')
for i in range(r):
for j in range(c):
result[i][j] = mat[i][j] * rix[i][j]
for ii in result:
print(' {}'.format(''.join(str(ii).split(','))[1:-1]))
elif operation.lower() == 'e': break
else: print('\nInvalid Input')
def programs(self):
match self.number:
case 1: return ''' odd, even = 0, 0
for i in l:
if i%2==0: even+=1
else: odd+=1
print('Number of odd numbers: {}\\nNumber of even numbers: {}'.format(odd,even))'''
case 2: return ''' digits, letters = 0, 0
s = str(input('Enter the text: '))
for i in s:
if i.isdigit(): digits+=1
elif i.isalpha(): letters+=1
else: pass
print('Number of digits: {}\\nNumber of letters: {}'.format(digits,letters))'''
case 3: return ''' b = True
s = str(input('Enter the password: '))
while True:
if not any(s.islower() for s in s): b = False; break
elif not any(s.isupper() for s in s): b = False; break
elif not any(s.isdigit() for s in s): b = False; break
elif not any(s in ['$','#','@'] for s in s): b = False; break
elif len(s) < 6: b = False; break
elif len(s) > 16: b = False; break
else: break
if b: print('Valid Password')
else: print('Invalid Password')'''
case 4: return ''' count, sum = 0, 0
i = int(input('Enter the number (0 to finish): '))
if i != 0:
sum+=i
count+=1
while i !=0:
i = int(input(' Enter the number (0 to finish): '))
sum = sum+i
count+=1
if count == 0:
print('Sum of the above numbers: 0\\nAverage of the above numbers: 0')
else: print('Sum of the above numbers: {}\\nAverage of the above numbers: {}'.format(sum, sum/(count-1)))'''
case 5: return ''' upper, lower = 0, 0
s = str(input('Enter the text: '))
for i in s:
if i.isupper(): upper+=1
elif i.islower: lower+=1
else: pass
print('Number of upper case letters: {}\\nNumber of lower case letters: {}'.format(upper, lower))'''
case 6: return ''' x, y, l = 0, 1, []
i = int(input('Enter the number: '))
while y <= i:
l.append(y)
x, y = y, x+y
print('Fibonacci series: {}'.format(str(l)[1:-1]))'''
case 7: return ''' i = int(input('Enter the number: '))
def recursion(i):
if i == 1: return 1
else: return i * (recursion(i-1))
print('Factorial of {} is: {}'.format(i, recursion(i)))'''
case 8: return ''' l = list(map(int,input('Enter the list (separated by spaces): ').split(' ')))
low, high = 0, len(l)-1
b = False
i = int(input('Search: '))
while(low <= high and not b):
mid = (low + high)//2
if l[mid] == i:
b = True
else:
if i < l[mid]:
high = mid - 1
else:
low = mid + 1
if b: print('Number {} is present at index {} in the list'.format(i, mid))
else: print('Number {} is not present in the list'.format(i))'''
case 9: return ''' import os
s = str(input('Write (to file): '))
file = open("text.txt", 'w')
file.write(s)
file = open("text.txt", 'r')
word = " "
freq = 0
words = []
for line in file:
splitted_words = line.replace(',',' ').replace('.',' ').split(" ")
for w in splitted_words:
words.append(w)
for i in range(0, len(words)):
count = 1
for j in range(i+1, len(words)):
if words[i] == words[j]:
count+=1
if(count>freq):
freq = count
word = words[i]
file.close()
os.remove('text.txt')
print("Frequent word: '{}' at {} times".format(word, freq))'''
case 10: return ''' mat, rix = [], []
r = int(input('Enter the number of rows: '))
c = int(input('Enter the number of columns: '))
rows, columns = r, c
while rows > 0:
rows-=1
l = list(map(int,input('Enter the row for the first matrix (separated by spaces): ').split(" ")))
mat.append(l)
rows = r
while rows > 0:
rows-=1
l = list(map(int,input('Enter the row for the second matrix (separated by spaces): ').split(" ")))
rix.append(l)
rows = r
result = []
for i in range(rows):
row = []
for j in range(c):
row.append(0)
result.append(row)
while True:
print('\\n[1 - addition | 2 - subtraction | 3 - multiplication | e - exit]\\n')
operation = input('Select operation: ')
if operation == '1':
for i in range(r):
for j in range(c):
result[i][j] = mat[i][j] + rix[i][j]
for ii in result:
print(' {}'.format(''.join(str(ii).split(','))[1:-1]))
elif operation == '2':
for i in range(r):
for j in range(c):
result[i][j] = mat[i][j] - rix[i][j]
for ii in result:
print(' {}'.format(''.join(str(ii).split(','))[1:-1]))
elif operation == '3':
for i in range(r):
for j in range(c):
result[i][j] = mat[i][j] * rix[i][j]
for ii in result:
print(' {}'.format(''.join(str(ii).split(','))[1:-1]))
elif operation.lower() == 'e': break
else: print('\\nInvalid Input')'''
class Input(Program):
def one(self):
l = None
while True:
try:
l = list(map(int,input('\nInput:\n\n Enter the list (separated by spaces): ').split(' '))); break
except: print('\n Invalid Input (e.g. 1 2 3 etc.)')
return super().one(l)
def two(self):
s = None
while True:
try:
s = str(input('\nInput:\n\n Enter the text: '))
if s.isspace():
raise ValueError(); break
else: break
except: print('\n Invalid Input (e.g. he110 wor1d)')
return super().two(s)
def three(self):
s = None
while True:
try:
s = str(input('\nInput:\n\n Enter the password: ')); break
except: pass
return super().three(s)
def four(self):
i = None
while True:
try:
i = int(input('\nInput:\n\n Enter the number (0 to finish): ')); break
except: print('\n Invalid Input (e.g. 1 or 2 or 3 etc.)\n')
return super().four(i)
def five(self):
s = None
while True:
try:
s = str(input('\nInput:\n\n Enter the text: '))
if any(s.isdigit() for s in s):
raise ValueError(); break
elif s.isspace():
raise ValueError(); break
else: break
except: print('\n Invalid Input (e.g. heLLO worLd)')
return super().five(s)
def six(self):
i = None
while True:
try:
i = int(input('\nInput:\n\n Enter the number: ')); break
except: print('\n Invalid Input (e.g. 1 or 2 or 3 etc.)')
return super().six(i)
def seven(self):
i = None
while True:
try:
i = int(input('\nInput:\n\n Enter the number: ')); break
except: print('\n Invalid Input (e.g. 1 or 2 or 3 etc.)')
program = Program(self)
return i, program.seven(i)
def eight(self):
l, i = None, None
while True:
try:
l = list(map(int,input('\nInput:\n\n Enter the list (separated by spaces): ').split(' ')))
if not all(l[i] <= l[i+1] for i in range (len(l)-1)) | all(l[i] >= l[i+1] for i in range (len(l)-1)): raise NameError
break
except NameError: print('\n Invalid Input (sorted values only)')
except ValueError: print('\n Invalid Input (e.g. 1 2 3 etc.)')
while True:
try:
i = int(input('\n Search: ')); break
except: print('\n Invalid Input (e.g. 1 or 2 or 3 etc.)')
return super().eight(l, i), i
def nine(self):
s = None
while True:
try:
s = str(input('\nInput:\n\n Write (to file): ')); break
except: pass
return super().nine(s)
def ten(self):
r, c = None, None
while True:
try:
r = int(input('\nInput:\n\n Enter the number of rows: ')); break
except: print('\n Invalid Input (e.g. 1 or 2 or 3 etc.)')
while True:
try:
c = int(input('\n Enter the number of columns: ')); break
except: print('\n Invalid Input (e.g. 1 or 2 or 3 etc.)')
return super().ten(r,c)
class Output(Input):
def one(self):
one = super().one()
print('\nOutput:\n\n Number of odd numbers: {}\n Number of even numbers: {}'.format(one[0],one[1]))
def two(self):
two = super().two()
print('\nOutput:\n\n Number of digits: {}\n Number of letters: {}'.format(two[0],two[1]))
def three(self):
s = ''
three = super().three()
match three[1]:
case 'a': s = 'At least 1 letter between [a-z]'
case 'b': s = 'At least 1 letter between [A-Z]'
case 'c': s = 'At least 1 number between [0-9]'
case 'd': s = 'At least 1 character from [$#@]'
case 'e': s = 'Minimum length 6 characters'
case 'f': s = 'Maximum length 16 characters'
if s == '': print('\nOutput:\n\n {}'.format(three[0]))
else: print('\nOutput:\n\n {} ({})'.format(three[0],s))
while True:
if str(three[0]) == 'Invalid Password': self.three(); break
else: break
def four(self):
four = super().four()
print('\nOutput:\n\n Sum of the above numbers: {}\n Average of the above numbers: {}'.format(four[0], four[1]))
def five(self):
five = super().five()
print('\nOutput:\n\n Number of upper case letters: {}\n Number of lower case letters: {}'.format(five[0],five[1]))
def six(self):
six = super().six()
print('\nOutput:\n\n Fibonacci series: {}'.format(six))
def seven(self):
seven = super().seven()
print('\nOutput:\n\n Factorial of {} is: {}'.format(seven[0], seven[1]))
def eight(self):
eight = super().eight()
if eight[0][0]: print('\nOutput:\n\n Number {} is present at index {} in the list'.format(eight[1],eight[0][1]))
else: print('\nOutput:\n\n Number {} is not present in the list'.format(eight[1]))
def nine(self):
nine = super().nine()
print("\nOutput:\n\n Frequent word: '{}' at {} times".format(nine[0],nine[1]))
def ten(self):
ten = super().ten()
return (ten)
class Result(Output):
def result(self):
return ' Thus the above code is executed successfully.'
class PPL(Result):
def ppl(self):
print('\nAim:\n\n{}'.format(Algorithm(self.number).aim()))
print('\nAlgorithm:\n\n{}'.format(Algorithm(self.number).algorithm()))
print('\nProgram:\n\n{}'.format(Program(self.number).programs()))
match self.number:
case 1: Result(self).one()
case 2: Result(self).two()
case 3: Result(self).three()
case 4: Result(self).four()
case 5: Result(self).five()
case 6: Result(self).six()
case 7: Result(self).seven()
case 8: Result(self).eight()
case 9: Result(self).nine()
case 10: Result(self).ten()
print('\nResult:\n\n{}'.format(Result(self).result()))
#ppl = PPL(7)
#ppl.ppl()
def main():
if platform.system() == 'Windows':
os.system('"cls"')
os.system('"title Python Programing Lab"')
elif platform.system() == 'Linux':
os.system('"clear"')
#os.system('"title Python Programing Lab"')
else: pass #Mac - Darwin
print('\nWelcome to Python Programing Lab!')
while True:
print('\n[lop - list of programs | 1 to 10 - number of programs | cls - clear screen | e - exit]')
command = input('\nEnter your command: ')
if str(command).lower() == 'lop':
ppl = PPL(0)
print(ppl.lop())
elif str(command).lower() == 'e': break
elif str(command).lower() == 'cls':
os.system('"cls"') if platform.system() == 'Windows' else os.system('"clear"')
main(); break
else:
ppl = PPL(int(command))
ppl.ppl()
if __name__ == '__main__':
main()