Pages

Wednesday, 23 March 2016

Alphabet D Of Asterick '*'

# To print Alphabet D of Asterick '*'
# Set your font type As Courier New


n=int(raw_input("Enter a Number : "))
m=n
n=(n-1)/2
for i in range(n) :
    print "*",
print
for i in range(m-2) :
    print "*",
    for i in range(n-1) :
        print " ",
    print "*"
for i in range(n) :
    print "*",
print

OUTPUT
Enter a Number : 9
* * * *
*       *
*       *
*       *
*       *
*       *
*       *
*       *
* * * *

Sunday, 20 March 2016

Alphabet C Of Asterick '*'

# To print Alphabet C of Asterick '*'
# Set your font type As Courier New

n=int(raw_input("Enter a Number : "))
n=(n-1)/2
print " ",
for i in range(n) :
    print "*",
print
for i in range(2*n-1) :
    print "*"
print " ",
for i in range(n) :
    print "*",
print

OUTPUT
Enter a Number : 9
  * * * *
*
*
*
*
*
*
*
  * * * *

Tuesday, 15 March 2016

Alphabet B of Asterick "*"

# To print Alphabet B of Asterick '*'
# Set your font type As Courier

n=int(raw_input("Enter a Number : "))
n=(n-1)/2
for i in range(n) :
    print "*",
print
for i in range(n-1) :
    print "*",
    for i in range(n-1) :
        print " ",
    print "*"
for i in range(n) :
    print "*",
print
for i in range(n-1) :
    print "*",
    for i in range(n-1) :
        print " ",
    print "*"
for i in range(n) :
    print "*",
print

OUTPUT
Enter a Number : 9
* * * *
*       *
*       *
*       *
* * * *
*       *
*       *
*       *
* * * *

Alphabet A of Asterick "*"

# To print Alphabet A of Asterick '*'
# Set your font type As Courier
n=int(raw_input("Enter a Number : "))
m=n
n=(n-1)/2
a=-1
for i in range(n) :
    print (m-1)*" ",
    print "*",
    for k in range(a) :
        print " ",
    if a==-1 :
        print
    else :
        print "*"
    a=a+1
    m=m-1
print (m-1)*" ",
m=m-1
a=a+1
for i in range(n+1) :
    print "*",
print
for i in range(n) :
    print (m-1)*" ",
    print "*",
    for k in range(a) :
        print " ",
    print "*"
    a=a+1
    m=m-1

OUTPUT
Enter a Number : 9
         *
        * *
       *   *
      *     *
     * * * * *
    *         *
   *           *
  *             *
 *               *


Thursday, 10 March 2016

Pallindrome String

# WAP to check if a string is pallindrome or not
# Set Your Font as Courier New

z=raw_input("Enter a string: ")
z.lower()
for i in range(-len(z),0) :
x=x+z[i]
if x==z :
print "Given String is Pallindrome"
else :
print "Given String is not a Pallindrome"

Output 1
Enter a string: aIbohPhoBiA
Given String is Pallindrome

Output 2
Enter a string: palindrome
Given String is not a Pallindrome

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



Tuesday, 26 January 2016

How to make a program on a topic?

Wondered how easy can a program becomes when you think of the program
to be giving output in your mind.

Programming in python is one of the interesting and easiest thing I have ever
encountered in my life. So a program can be made with high quality and less quantity.

1. First write an output model of your program.

2. Then think of what could be the possible way for it to be done.

3. Then make use if your programming instinct to make your program on the run.

4. If needed you can refer to a book for the functions and statements needed to do what
    you thought earlier in step 2.

5. Finally saving all see the output of your hard work in the output window.

Thank You!

You can follow me on twitter @AnuragShukla47


Matrix addition


# To do addition in matrices # First change your font type as Courier

Wednesday, 20 January 2016

Hollow Diamond Of Asterick '*'

# To print a Hollow diamond of Asterick '*'
# Set your font type As Courier

n=int(raw_input("Enter a no"))
x,y=" ","*"
n=(n-1)/2
b=2
q=-1
m,o=n,n
print (m+1)*x,y
for i in range(n) :
    a=n*x
    n=n-1
    p=q*x
    q=q+1
    if q==0 :
        print a,y,y
    else :
        print a,y,p,p,y
for j in range(m,0,-1) :
    c=b*x
    b=b+1
    r=(m-3)*x
    m=m-1
    if m==0:
        print (o+1)*x,y
    elif m==1 :
        print (o)*x,y,y
    else :
        print c,y,r,r,y

Output
Enter a number 9
      *
     * *
    *   *
   *     *
  *       *
   *     *
    *   *
     * *
      *

Diamond OF Asterick '*'

# To print a diamond of Asterick '*'
# Set your font type As Courier
n=int(raw_input("Enter a no."))
n=(n-1)/2
w=n
x=1
for i in range(n) :
    for j in range(w,0,-1) :
        print " ",
    w=w-1
    for k in range(-1,i*2) :
        print "*",
    print
for i in range(n,-1,-1) :
    for j in range(0,w) :
        print " ",
    w=w+1
    for k in range(i*2-1,-2,-1) :
        print "*",
    print

Output
Enter a no. 9
        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *

Diamond Number 2

# To print a diamond of numbers
# Set your font type As Courier

n=int(raw_input("Enter a number"))
n=(n)/2
w=n
for i in range(1,n+2) :
    for j in range(w,0,-1) :
        print " ",
    w=w-1
    for l in range(i,0,-1) :
        print l,
    for k in range(2,i+1) :
        print k,

    print
for i in range(n,-1,-1) :
    for j in range(0,w+2) :
        print " ",
    w=w+1
    
    for l in range(i,0,-1) :
        print l,
    for k in range(2,i+1) :
        print k,
    print

Output
Enter a number 9
        1
      2 1 2
    3 2 1 2 3
  4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
  4 3 2 1 2 3 4
    3 2 1 2 3
      2 1 2
        1

         



Diamond number 1

# To print a diamond of numbers
# Set your font type As Courier
n=int(raw_input("Enter a number"))
n=(n)/2
w=n
for i in range(1,n+2) :
    for j in range(w,0,-1) :
        print " ",
    w=w-1
    for k in range(1,i) :
        print k,
    for l in range(i,0,-1) :
        print l,
    print
for i in range(n,-1,-1) :
    for j in range(0,w+2) :
        print " ",
    w=w+1
    for k in range(1,i) :
        print k,
    for l in range(i,0,-1) :
        print l,
    print

Output
Enter a number 9
        1
      1 2 1
    1 2 3 2 1
  1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
  1 2 3 4 3 2 1
    1 2 3 2 1
      1 2 1
        1

         

Tuesday, 19 January 2016

Substitute for Split function on strings in python 2.7

def anurag_split(z,x=" ") :
    z=z+x
    L=[]
    w=len(z)
    for i in range(w) :
        a=""
        n=0
        if z[i-1]==x :
            while(z[i+n]!=x) :
                a=a+z[i+n]
                n=n+1
            L.append(a)
    return L