Pages

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

Bubble Sort in Python 2.7

def bubble_sort(L) :
    for j in range(len(L)-1) :
        print "------ Iteration",j+1,"------"
        for i in range(len(L)-1) :
            if L[i]>L[i+1] :
                a=L[i+1]
                L[i+1]=L[i]
                L[i]=a
            print "List after pass",i+1,":",L

Insertion Sort

def insertion_sort(L) :
    for j in range(1,len(L)) :
        a=L[j]
        i=j
        while L[i-1]>a and i>=1 :
            L[i]=L[i-1]
            i-=1
        L[i]=a
        print "List after pass",j,":",L

Selection Sort in python 2.7

def selection_sort(L) :
    s=0
    y=0
    for j in range(len(L)) :
        for i in range(y,len(L)) :
            if L[s]>L[i] :
                s=i
        a=L[s]
        L[s]=L[y]
        L[y]=a
        y+=1
        print "List after pass",j+1,":",L