You are on page 1of 13

Assignment 1

Shopping App
o Code
class item_info:
def __init__(self):
self.code,self.price,self.dic,self.qty,self.fprice=0,0,0,0,0
self.name=''

def buy(self,n,c,p,q):
self.name,self.code,self.price,self.qty=n,c,p,q
def find_disc(self):
if self.qty<=10:
self.fprice=self.price*self.qty
self.disc=0

elif self.qty>=11 and self.qty<=20:


self.fprice=self.price*(.85)*self.qty
self.dic=15

else:
self.fprice=self.price*(.8)*self.qty
self.dic=20

def show_all(self):
print "Item name","\t Item code",'\t Item price','\t Quantity','\t Discount','%\t Final price'

print self.name,'\t',self.code,'\t',self.price,'\t\t',self.qty,'\t',self.dic,'% \t ',self.fprice

print ("Welcome to Shopping app\n")


q=1
while q==1:
s=item_info()
n=raw_input("Enter Item Name\n")
c=input('Enter the item code\n')
p=input('Enter the price\n')
q=input('Enter the quantity\n')
s.buy(n,c,p,q)
s.find_disc()
s.show_all()
q=input("Press 1 to continue\n")
o Output
Person Student Inheritance
o Code
class person(object):
v=0
def __init__(self,nm='',a=0,na='',st=''):
self.nm, self.a, self.na=nm,a,na

def disp(self):
self.st= 'Name: '+str(self.nm)+ '\tAge: '+str(self.a)+ '\tNationality: '+str(self.na)
if person.v==0:
print self.st

def acc(self):
self.nm= raw_input("PLese enter your name\n")
self.a=input("Please enter your age\n")
self.na= raw_input("PLese enter your nationality\n")

class student(person):
def __init__(self,nm='',a=0,na='',s='',p=0):
person.__init__(self,nm,a,na)
self.s, self.p=s,p
def show(self):

if self.p>=90:
self.g='A'
elif self.p>=80 and self.p<90:
self.g='B'
elif self.p>=70 and self.p<80:
self.g='C'
elif self.p>=60 and self.p<70:
self.g='D'
else:
self.g='F'

person().v=1
person().disp()
self.pt= person().st+'Stream: '+str(self.s)+'\tPercentage: '+str(self.p)+'%'+'\tGrade: ',self.g
print pt
def take(self):
person().acc()
self.s=raw_input("Pease enter you stream")
self.p=input("Pease enter you percentage")

o Output
Assignment 2
Bubble sort
o Code
j=1
while j==1:
s=list(input("Enter the list to be sorted\n"))
n=len(s)
for i in range(n-1):
for j in range(n-i-1):
if s[j]>s[j+1]:
s[j],s[j+1]=s[j+1],s[j]
print 'Pass: ',i+1,'\t',s
print 'The sorted list is',s
j=input("Enter 1 to continue\n")

o Output
Selection Sort
o Code
x=1
while x==1:
s=list(input("Enter the list to be sorted\n"))
n=len(s)
for c in range(n-1):
m=s[c]
i=c
for p in range(c+1,n):
if m>s[p]:
m=s[p]
i=p
s[c],s[i]=s[i],s[c]
print 'Pass: ',c+1,s
print 'The sorted list is',s
x=input("Enter 1 to continue\n")
o Output
Assignment 3
Stack of objects
o Code
class experiment:
def accept(self):
self.expname=raw_input("enter the experiment name\n")
self.num=input("Enter experiment number\n")
def disp(self):
print "Experiment Name ",self.expname,' Experiment number',self.num
class stack:
def __init__(self):
self.s,self.ctr=[],0
def push(self,e):
self.s.append(e)
self.ctr=self.ctr+1
print "Object added successfully"
def pop(self):
if self.ctr!=0:
self.ret=self.s.pop()
self.ctr=self.ctr-1
print "The Name of the popped experiment is ",self.ret.expname,' The experiment
number is ',self.ret.num
else:
print 'Stack empty. Cannot pop'
j=0
print "Please choose one of the following actions \n1)Push\n2)Pop\n3)Exit\n4)To display these
options"
stack=stack()
while j!=3:
j=input()
if j==1:
ob=experiment()
ob.accept()
stack.push(ob)
elif j==2:
stack.pop()
elif j==3:
print "Exiting"
elif j==4:
print "Please choose one of the following actions \n1)Push\n2)Pop\n3)Exit\n4)To display
these options"
else:
print "Invalid input"
o Output
Assignment 4
Stack (function)
top=-1
def push(s,l,e):
global top
if top==l-1:
print "Stack's full. Can't add element"
else:
top=top+1
s.append(e)
print "Element added successfully"
def pop(s):
global top
if top==-1:
print "The stack's empty!! Cannot pop."
else:
top=top-1
print s.pop()
j,s=0,[]
l=input("Please enter the length of stack\n")
print "Please choose one of the following actions \n1)Push\n2)Pop\n3)Exit\n4)To display these
options"
while j!=3:
j=input()
if j==1:
e=input("Enter the element\n")
push(s,l,e)
elif j==2:
pop(s)
elif j==3:
print "Exiting program\n"
break
elif j==4:
print "Please choose one of the following actions \n1)Push\n2)Pop\n3)Exit\n4)To display
these options\n"
o Output
Stacks (Classes)
o Code
class stack:
def __init__(self):
self.s,self.ctr=[],0
def push(self,e):
self.s.append(e)
self.ctr=self.ctr+1
print "Object added successfully"
def pop(self):
if self.ctr!=0:
self.ctr=self.ctr-1
print "The popped element is",self.s.pop()
else:
print 'Stack empty. Cannot pop'
print "Please choose one of the following actions \n1)Push\n2)Pop\n3)Exit\n4)To display the
above options"
ob=stack()
j=0
while j!=3:
j=input()
if j==1:
e=input("Enter the element\n")
ob.push(e)
elif j==2:
ob.pop()
elif j==3:
print 'Exiting\n'
break
elif j==4:
print "Please choose one of the following actions \n1)Push\n2)Pop\n3)Exit\n4)To
display the above options\n"
else:
print 'Inavalid Input\n'
o Output

You might also like