Pages

Thursday 25 February 2016

Binary Search In Python

# Write A Program For Binary Search In Linear List….
# Set your Font type as Courier

def bsearch(L,z) :
    M=list(L)
    f=0
    l=len(L)
    for i in range(len(L)) :
        mid=(f+l)/2
        if L[mid]==z :
            return mid+1
        elif L[mid]!=z :
            if L[mid]>z :
                l=mid-1
            elif L[mid]<z :
                f=mid+1
    return 0
n=int(raw_input("Enter the length of list"))
L=[0]*n
for i in range(n) :
    L[i]=int(raw_input("Enter Element"+str(i+1)+" : "))
L.sort()
z=int(raw_input("Enter item to be searched for : "))
p=bsearch(L,z)
if p==0 :
    print"Not found"
else :
    print "Element Found at Position",p

Linear search in Python

# Write A Program For Linear Search In Linear List….
# Change your Font Type as Courier

def linear_search(L,z) :
    for i in range(len(L)) :
        if L[i]==z :
            print "Element found at index :",i,"Position :",i+1
    else :
        print "Element not in the List"
n=int(raw_input("Enter desired linear-list size (max. 50)..."))
print "Enter elements for Linear List"
L=[0]*n
for i in range(n) :
    L[i]=raw_input("Enter Element "+str(i)+" : ")
z=raw_input("Enter Element to be searched for . . . ") 
linear_search(L,z)

Monday 22 February 2016

Hybrid Inheritance

# Write A Program To Show Hybrid Inheritance….
# Set Your Font Type as Courier

class Vehicle :
    def __init__(self,name,model) :
        self.name=name
        self.model=model
    def details(self) :
        print "Name:",self.name,"\n","Model:",self.model
class Car(Vehicle) :
    def __init__(self,name,model,colour) :
        Vehicle.__init__(self,name,model)
        self.colour=colour
    def details(self) :
        Vehicle.details(self)
        print "colour:",self.colour
class Truck(Vehicle) :
    def __init__(self,name,model,hp) :
        Vehicle.__init__(self,name,model)
        self.hp=hp
    def details(self) :
        Vehicle.details(self)
        print "Horse Power:",self.hp
class MonsterTruck(Car,Truck) :
    def__init__(self,name,model,colour,hp,price):
        Car.__init__(self,name,model,colour)
        Truck.__init__(self,name,model,hp)
        self.price=price
        print "Initialized MonsterTruck:",
        print self.name
    def details(self) :
        Car.details(self)
        Truck.details(self)
        print "Price:",self.price
m=MonsterTruck("Mustang",2009,"Blue",900,4200000)

m.details()

Over-riding in Python

# Write A Program To Show Overriding With Super Function….
# Set Your font type as Courier

class A(object) :
    def __init__(self,a) :
        self.a=a
        print "A's __init__"
    def method1(self) :
        print "A's method1 : a is",self.a
class B(A) :
    def __init__(self,b,a) :
        self.b=b
        print "B's __init__"
        super(B,self).__init__(a)
    def method1(self) :
        print "B's method1 : b is",self.b
        super(B,self).method1()
obj1=B(17,22)

obj1.method1()

Multilevel Inheritance

# Write A Program To Show Multilevel Inheritance….
# Set Your font type as Courier

class Vehicle(object) :
    def __init__(self,l=0,w=0) :
        self.length=l
        self.width=w
    def define(self) :
        print "Vehicle with length",self.length,"in & width",self.width,"in"
class Car(Vehicle) :
    def __init__(self,clr,seats,l,w) :
        Vehicle.__init__(self,l,w)
        self.colour=clr
        self.seatingCapacity=seats
    def changeGears(self,gr) :
        print "Changed to Gear",gr
    def turn(self,direction) :
        print "Turned to",direction,"direction"
class RacingCar(Car) :
    def __init__(self,clr,seats,l,w,tr,spd) :
        Car.__init__(self,clr,seats,l,w)
        self.turnRadius=tr
        self.speed=spd
        print "Racing Car instance created"
    def start(self) :
        self.define()
        self.changeGears(2)
        print "Racing Car Starts - Ready to vroom!"
rcar=RacingCar("Blue",2,206,78.5,6,200)
rcar.start()

rcar.turn("left")

Hierarchical Inheritance

# Write A Program To Show Hierarchical Inheritance….
# Set Your font type as Courier

class Animal(object) :
    def __init__(self,name) :
        self.name=name
    def speak(self) :
        print self.name,"says",self.sound()
class Cow(Animal) :
    def __init__(self,name) :
        Animal.__init__(self,name)
    def sound(self) :
        return"moo"
class Horse(Animal) :
    def __init__(self,name) :
        Animal.__init__(self,name)
    def sound(self) :
        return"neigh"
class Sheep(Animal) :
    def __init__(self,name) :
        Animal.__init__(self,name)
    def sound(self) :
        return"baaaaa"
h=Horse("Chetak")
h.speak()
c=Cow("Rambha")
c.speak()
s=Sheep("Mithi")

s.speak()

Multiple Inheritance

# Write A Program To Show Multiple Inheritance….
# Set Your font type as Courier
class SpecialEngine(object) :
    def __init__(self,p) :
        self.power=p
    def ignition(self) :
        print "Engine Started"
class Car(object) :
    def __init__(self,clr,seats) :
        self.colour=clr
        self.seatingCapacity=seats
    def changeGears(self,gr) :
        print "Changed to Gear",gr
    def turn(self,direction) :
        print "Turned to",direction,"direction"
class RacingCar(SpecialEngine,Car) :
    def __init__(self,clr,seats,p,tr,spd) :
        SpecialEngine.__init__(self,p)
        Car.__init__(self,clr,seats)
        self.turnRadius=tr
        self.speed=spd
        print "Racing Car instance created"
    def start(self) :
        self.ignition()
        self.changeGears(2)
        print """Racing Car Starts - Ready to vroom!"""
rcar=RacingCar("Blue",2,750,6,200)
rcar.start()

rcar.turn("left")

Sunday 7 February 2016

Stack Operations in Python

#program for stack operations
def isempty(stk) :
    if len(stk)==0 :
        return True
    else :
        return False
def PUSH(stk) :
    x=[]
    n=raw_input("Enter Your Name : ")
    no=int(raw_input("Enter Your Number"))
    x.append(n)
    x.append(no)
    stk.append(x)       #x Stores the data in list [Name,Number]
    return stk
def POP(stk) :
    if isempty(stk) :
        print "Empty Stack"
    else :
        x=stk.pop()
        return x
def DISPLAY(stk) :
    y=len(stk)
    z=y
    for i in range(y) :
        if i==0 :
            x=POP(stk)
            print "Item number",z,"is",x,"<----Top"
            z=z-1
        elif i==y-1 :
            x=POP(stk)
            print "Item number",z,"is",x,"<----Bottom"
            z=z-1
        elif i!=0 :
            x=POP(stk)
            print "Item number",z,"is",x
            z=z-1
        no=x.pop()
        n=x.pop()
        print "Name : ",n
        print "Number : ",no
stk=[['Anurag', 9958220446],['Ayush', 8138987125],['Jagrat', 9956288854]]
while True :
    print """Choose from the following menu : -
1) PUSH
2) POP
3) DISPLAY
4) EXIT"""
    ch=int(raw_input("Enter Your Choice : "))
    if ch==1 :
        PUSH(stk)
    elif ch==2 :
        if isempty(stk) :
            x=None
            print "Empty Stack"
        else :
            x=POP(stk)
            print x,"is the popped item."   #To show the popped data
            no=x.pop()
            n=x.pop()
            print "Name : ",n
            print "Number : ",no
    elif ch==3 :
        DISPLAY(stk)
    else :
        break