0% found this document useful (0 votes)
70 views6 pages

Jaya

The document contains code for several Python functions: 1. A stringmethods function that performs various operations on strings like reversing, substring extraction, checking for substring presence, splitting into words, and counting word frequencies. 2. A classes and objects function that defines a ComplexNumber class with methods to add and subtract complex numbers. 3. A Bank ATM function that performs deposit and withdrawal transactions, handling exceptions for minimum balance and deposit amounts. 4. A Library function that calculates installment amounts, checks for available books, and handles exceptions for invalid installments or books. 5. A usingcalendar function that prints a calendar, extracts dates for a month/year, and finds most frequent day of
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views6 pages

Jaya

The document contains code for several Python functions: 1. A stringmethods function that performs various operations on strings like reversing, substring extraction, checking for substring presence, splitting into words, and counting word frequencies. 2. A classes and objects function that defines a ComplexNumber class with methods to add and subtract complex numbers. 3. A Bank ATM function that performs deposit and withdrawal transactions, handling exceptions for minimum balance and deposit amounts. 4. A Library function that calculates installment amounts, checks for available books, and handles exceptions for invalid installments or books. 5. A usingcalendar function that prints a calendar, extracts dates for a month/year, and finds most frequent day of
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#FUNCTION AND OOPS | 1 | STRING METHODS

import math
import os
import random
import re
import sys

def Reverse(lst):
lst.reverse()
return lst

def stringmethod(para, special1, special2, list1, strfind):


# Section 1 --> Ok
word1 = ""
for character in para:
if character not in special1:
word1 = word1 + character
rword2 = word1[0:70]
print(rword2[::-1])

# Section 2 --> Ok
rword3 = rword2[::-1]
rword3 = re.sub("\s+", "", rword3)
temp = ""
for character in rword3:
temp = temp + character + special2
print(temp[:-1])

# Section 3 --> Ok
a = True
for s in list1:
if s not in para:
a = False
if a:
print("Every string in ", end=" "),
print(list1, end=" "),
print("were present")
else:
print("Every string in ", end=" "),
print(list1, end=" "),
print("were not present")

# Section 4 --> OK
print(word1.split()[:20])

# Section 5
word_list = word1.split()

temp_dict = dict()
for str1 in word_list:
if word_list.count(str1) <= 3:
if str1 not in temp_dict:
temp_dict[str1] = 0
temp_dict[str1] += 1
temp_list = []
for value in list(reversed(list(temp_dict)))[0:20]:
temp_list.append(value)
print(Reverse(temp_list))

# Section 6
print(word1.rindex(strfind))

if __name__ == '__main__':
para = input() # a string
spch1 = input()
spch2 = input()
qw1_count = int(input().strip())
qw1 = []
for _ in range(qw1_count):
qw1_item = input()
qw1.append(qw1_item)
strf = input()
stringmethod(para, spch1, spch2, qw1, strf)

_______________________________________________________________

# FUNCTION AND OOPS | 5 | CLASSES AND OBJECTS 2

import math
import os
import random
import re
import sys

# Write your code here


class comp:
def __init__(self, real, imaginary):
self.real = real
self.imaginary = imaginary

def add(self, obj):


re = self.real + obj.real
im = self.imaginary + obj.imaginary
print("Sum of the two Complex numbers :"+str(re)+"+"+str(im)+"i")

def sub(self, obj):


re1 = self.real - obj.real
im1 = self.imaginary - obj.imaginary
if im1 >= 0:
print("Subtraction of the two Complex
numbers :"+str(re1)+"+"+str(im1)+"i")
else:
print("Subtraction of the two Complex numbers :" + str(re1) + str(im1)
+ "i")

if __name__ == '__main__':
real1 = int(input().strip())
img1 = int(input().strip())

real2 = int(input().strip())
img2 = int(input().strip())

p1 = comp(real1, img1)
p2 = comp(real2, img2)
p1.add(p2)
p1.sub(p2)

___________________________________________________________________________________
_____

#FUNCTION AND OOPS | 8 | HANDLING EXCEPTION

import math
import os
import random
import re
import sys

#
# Complete the 'Bank_ATM' function below.
#
# Define the Class for user-defined exceptions "MinimumDepositError" and
"MinimumBalanceError" here
class MinimumDepositError(Exception):
# Constructor method
def __init__(self, value):
self.value = value

# __str__ display function


def __str__(self):
return repr(self.value)

class MinimumBalanceError(Exception):
# Constructor method
def __init__(self, value):
self.value = value

# __str__ display function


def __str__(self):
return repr(self.value)

def Bank_ATM(balance, choice, amount):


isTrHappen = False

try:
if balance < 500:
raise ValueError("As per the Minimum Balance Policy, Balance must be at
least 500")

if choice == 1:
if amount < 2000:
raise MinimumDepositError("The Minimum amount of Deposit should be
2000.")
else:
isTrHappen = True
balance += amount
else:
balance -= amount
if balance < 500:
raise MinimumBalanceError("You cannot withdraw this amount due to
Minimum Balance Policy")
else:
isTrHappen = True

except ValueError as e:
print(e)
except MinimumDepositError as e:
print(e.value)
except MinimumBalanceError as e:
print(e.value)

if isTrHappen:
print("Updated Balance Amount: " + str(balance))

if __name__ == '__main__':

bal = int(input())
ch = int(input())
amt = int(input())

try:
Bank_ATM(bal, ch, amt)

except ValueError as e:
print(e)
except MinimumDepositError as e:
print(e)
except MinimumBalanceError as e:
print(e)

___________________________________________________________________________________
_____________________

# FUNCTION AND OPS | 9 | HANDLING EXCEPTION 4

import math
import os
import random
import re
import sys

#
# Complete the 'Library' function below.
#

def Library(memberfee, installment, book):


listOfBooks = ['Philosophers Stone', 'Chamber of Secrets', 'Prisoner of
Azkaban', 'Goblet of Fire', 'Order of Phoenix', 'Half Blood Prince', 'Deathly
Hallows 1', 'Deathly Hallows 2']
try:
if installment > 3:
raise ValueError("Maximum Permitted Number of Installments is 3")
elif installment == 0:
raise ZeroDivisionError("Number of Installments cannot be Zero.")
else:
print("Amount per Installment is " + str(float(memberfee /
installment)))

if book.lower() in (string.lower() for string in listOfBooks):


print("It is available in this section")
else:
raise NameError("No such book exists in this section")

except ValueError as e:
print(e)
except ZeroDivisionError as e:
print(e)
except NameError as e:
print(e)

if __name__ == '__main__':

memberfee = int(input())
installment = int(input())
book = input()

try:
Library(memberfee, installment, book)

except ZeroDivisionError as e:
print(e)
except ValueError as e:
print(e)
except NameError as e:
print(e)

___________________________________________________________________________________
_____________________

# MODULLE 4 CALENDER

import math
import os
import random
import re
import sys
import calendar
import datetime
from collections import Counter
#
# Complete the 'usingcalendar' function below.
#
# The function accepts TUPLE datetuple as parameter.
#

def most_frequent(List):
occurence_count = Counter(List)
return occurence_count.most_common(1)[0][0]

def usingcalendar(datetuple):
month = datetuple[1]
if calendar.isleap(datetuple[0]):
month = 2
print(calendar.month(datetuple[0], month))

obj = calendar.Calendar()
day_list = []
for day in obj.itermonthdates(datetuple[0], month):
day_list.append(day)
print(day_list[-7:])

day_name_list = []
for day in day_list:
if day.month == month:
day_name_list.append(day.strftime("%A"))
print(most_frequent(day_name_list))

if __name__ == '__main__':
qw1 = []

for _ in range(3):
qw1_item = int(input().strip())
qw1.append(qw1_item)

tup = tuple(qw1)

usingcalendar(tup)

You might also like