Sunday, December 29, 2013

Unwrapping A Spiral (Revisited)

#Works for uneven row & column length as well
 array=[[1, 2, 3, 4, 5],
       [16,17,18,19,6],
       [15,24,25,20,7],
       [14,23,22,21,8],
       [13,12,11,10,9],
       ]
cnt=0

left,right,down,top=0,0,0,0
dirLeft,dirRight,dirDown,dirTop=0,1,2,3

rowIndex,columnIndex=0,0

direction=dirRight

while cnt    if direction==dirRight:
        print array[rowIndex][columnIndex],"right"
        if columnIndex<(len(array[0])-left)-1:
            columnIndex+=1
        else:
            direction=dirDown
            right+=1
            rowIndex+=1
         
    elif direction==dirDown:
        print array[rowIndex][columnIndex],"down"
        if rowIndex<(len(array)-down)-1:
            rowIndex+=1
        else:
            direction=dirLeft
            down+=1
            columnIndex-=1
      
    elif direction==dirLeft:
        print array[rowIndex][columnIndex],"left"
        if columnIndex>left:
            columnIndex-=1
        else:
            direction=dirTop
            left+=1
            rowIndex-=1
        
    elif direction==dirTop:
        print array[rowIndex][columnIndex],"top"
        if rowIndex > top+1:
            rowIndex-=1
        else:
            direction=dirRight
            top+=1
            columnIndex+=1    
    cnt+=1

Tuesday, October 9, 2012

CUDA Diary Day 1

Hi !
Yesterday, installed gcc on fedora 17.
Today, up to now, installed NVIDIA device drivers.
Currently, trying to install CUDA Toolkit + SDK.
Thanks to : http://fedoraproject.org/wiki/Cuda.

Thursday, March 22, 2012

Pascal’s Triangle

In 1653 Blaise Pascal published his treatise Traité du triangle arithmétique describing his triangle computing the binomial coefficients, which are used in the study of probability; Pascal gets credit for the name even though both the coefficients and their arrangement in a triangle were known before him.

To compute the triangle, start with a row containing only 1. Then each succeeding row is built by adding the two numbers triangularly above the current number, assuming a temporary 0 at each end of the prior row.

Your task is to write a program to neatly display Pascal’s Triangle.

1:  i=1  
2: a=[]
3: b=[]
4: for j in xrange(10):
5: for k in xrange(j):
6: a.append(1)
7: if j>=3:
8: for l in xrange(1,j-1):
9: a[l]=b[l-1]+b[l]
10: m=str(a)
11: print m.center(76)
12: b=a
13: a=[]