Sponsor Area
Write two methods in python using concept of Function Overloading (Polymorphism) to perform the following operations:
(i) A function having one argument as Radius, to calculate Area of Circle as 3.14#Radius#Radius
(ii) A function having two arguments as Base and Height, to calculate Area of right-angled triangle as 0.5#Base#Height.
def Area(R):
print 3.14*R*R
def Area(B,H):
print 0.5*B*H
What will be the status of the following list after the First, Second and Third pass of the bubble sort method used for arranging the following elements in ascending order ?
Note: Show the status of all the elements after each pass very clearly underlining the changes.
52, 42, -10, 60, 90, 20
I Pass
52 | 42 | -10 | 60 | 90 | 20 |
42 | 52 | -10 | 60 | 90 | 20 |
42 | -10 | 52 | 60 | 90 | 20 |
42 | -10 | 52 | 60 | 90 | 20 |
42 | -10 | 52 | 60 | 90 | 20 |
42 | -10 | 52 | 60 | 20 | 90 |
42 | -10 | 52 | 60 | 20 | 90 |
-10 | 42 | 52 | 60 | 20 | 90 |
-10 | 42 | 52 | 60 | 20 | 90 |
-10 | 42 | 52 | 60 | 20 | 90 |
-10 | 42 | 52 | 20 | 60 | 90 |
-10
|
42
|
52
|
20
|
60
|
90
|
-10
|
42
|
52
|
20
|
60
|
90
|
-10
|
42
|
52
|
20
|
60
|
90
|
-10
|
42
|
20
|
52
|
60
|
90
|
Write Addnew(Member) and Remove(Member) methods in python to Add a new Member and Remove a Member from a List of Members, considering them to act as INSERT and DELETE operations of the data structure Queue.
class queue:
Member=[]
def Addnew(self):
a=input('enter member name: ')
queue.Member.append(a)
def Remove(self):
if (queue.Member==[]):
print 'Queue empty'
else:
print 'deleted element is: ',queue.Member[0]
del queue.Member[0] # queue.Member.delete()
Write definition of a Method MSEARCH(STATES) to display all the state names from a list of STATES, which are starting with alphabet M.
For example:
If the list STATES contains
['MP','UP','WB','TN','MH','MZ','DL','BH','RJ','HR']
The following should get displayed
MP
MH
MZ
def MSEARCH(STATES):
for i in STATES:
if i[0]=='M':
print i
Write a method in python to read lines from a text file MYNOTES.TXT, and display those lines, which are starting with an alphabet ‘K’.
def display():
file=open('MYNOTES.TXT','r')
line=file.readline()
while line:
if line[0]=='K' :
print line
line=file.readline()
file.close()
Considering the following definition of class FACTORY, write a method in Python to search and display the content in a pickled file FACTORY.DAT,where FCTID is matching with the value '105'.
class Factory:
def __init__(self,FID,FNAM):
self.FCTID = FID # FCTID Factory ID
self.FCTNM = FNAM # FCTNM Factory Name
self.PROD = 1000 # PROD Production
def Display(self):
print self.FCTID,':',self.FCTNM,':',self.PROD
import pickle
def ques4c( ):
f=Factory( )
file=open('FACTORY.DAT','rb')
try:
while True:
if=pickle.load(file)
if f.FCTID==105:
f.Display()
except EOF Error:
pass
file.close()
What will be the status of the following list after the First, Second and Third pass of the insertion sort method used for arranging the following elements in descending order?
22, 24, 64,
34, 80, 43
Note: Show the status of all the elements after each pass very clearly underlining the changes.
22 | 24 | -64 | 34 | 80 | 43 | |
Pass 1 | 24 | 22 | -64 | 34 | 80 | 43 |
Pass 2 | 24 | 22 | -64 | 34 | 80 | 43 |
Pass 3 | 34 | 24 | 22 | -64 | 80 | 43 |
For a given list of values in descending order, write a method in python to search for a value with the help of Binary Search method. The method should return the position of the value and should return -1 if the value not present in the list.
def binarysrch(nums,x):
high = len(nums)
low =0
while low < high:
mid = (low + high)//2
midval = nums[mid]
if midval > x:
low = mid + 1
elif midval < x:
high = mid
else:
return mid
return 1
Write Insert(Place) and Delete(Place) methods in python to add Place and RemovePlace considering them to act as Insert and Delete operations of the data structure Queue.
class queue:
place = [ ]
def insert(self):
a = raw_input('Enter place')
queue.place.append(a)
def delete(self):
if (queue.place == [ ] ):
print('Queue empty')
else:
print('Deleted element is', queue.place[0])
queue.place.delete()
Write a method in python to find and display the prime numbers between 2 to N.Pass N as the argument to the method.
def prime(N):
for a in range(2,N):
for I in range(2,a):
if a%i ==0:
break
else:
print a
Illustrate the concept inheritance with the help of a python code.
The concept of inheritance is given below:
class Base:
def __init__ (self):
print 'Base Constructor at work...'
def show(self):
print 'Hello Base'
class Der(Base):
def __init__(self):
print 'Derived Constructor at work...'
def display(self):
print 'Hello from Derived'
Write a class PICTURE in Python with following specifications:
Instance Attributes
-Pno # Numeric value
-Category # String value
-Location # Exhibition Location with String value
Methods:
FixLocation () # A method to assign Exhibition
# Location as per Category as
# shown in the following table​
Category | Location |
Classic | Amina |
Modern | Jim Plaq |
Antique | Ustad Khan |
-Enter() # A function to allow user to
enter values
# Pno, Category and call
-FixLocation() method
-SeeAll() # A function to display all the
data members
class PICTURE:
Pno=0
Category=' '
Location=' '
def FixLocation():
if self.Category=='Classic':
self.Location='Amina'
elif self.Category=='Modern':
self.Location='Jim Plaq'
elif self.Category=='Antique':
self.Location='Ustad Khan'
def Enter():
self.Pno=int(input('Enter Pno:'))
self.Category=input('Enter Name:')
self.FixLocation()
def SeeAll()
print self.Pno,self.Category,self.Location
What is operator overloading with methods? Illustrate with the help of an example using a python code.
Operator overloading is an ability to use an operator in more than one form.
Examples:
In the following example operator + is used for finding the sum of two integers:
a = 7
b = 5
print(a+b) #gives the output: 12
Whereas in the next example, shown below the same + operator is used to add two strings:
a = 'Indian '
b = 'Government'
print(a+b) #gives the output: Indian Government
Write a method in python to display the elements of list thrice if it is a number and display the element terminated with '#' if it is not a number.
For example, if the content of the list is as follows:
ThisList=['41','DROND','GIRIRAJ','13','ZARA']
414141
DROND#
GIRlRAJ#
131313
ZARA#
def fun(L):
for I in L:
if I.isnumeric():
print(3*I) # equivalently:
print(I+I+I)
else:
print(I+'#')
Sponsor Area
Sponsor Area