Filling in a symmetric 2D array

Question:

I have this code, but I don’t know how can I make it looks like a python code and not a c-like code:

n=10

a = [[0 for row in xrange(n)]for col in range(n) ]
for i in xrange(n):
    a[i][0] = 1
    a[0][i] = 1


for i in xrange(1,n):
    for j in xrange(1,n):
        a[i][j] = a[i-1][j] + a[i][j-1]
Asked By: 0x90

||

Answers:

maybe using dictionaries would be cleaner? not sure though..

a = {}
for i in xrange(n):
    a[i, 0] = a[0, i] = 1

for i in xrange(1, n):
    for j in xrange(1, n):
        a[i, j] = a[i-1, j] + a[i, j-1]

or you can just write a function for this and letting the recursion figure things out.

@memoize
def a(i, j):
     if i == 0 or j == 0:
           return 1
     else:
           return a(i-1, j) + a(i, j-1)
Answered By: Doboy

It looks fine to me, except that since you appear to be working with a 2D array, maybe numpy would be useful.

Then you could write e.g.

a = numpy.zeros((n,n))
a[0,:] = 1
a[:,0] = 1
Answered By: aquavitae
from itertools import product
n = 10

a = [[1 if i==0 or j==0 else 0 for i in range(n)] for j in range(n)]

for i,j in product(range(1, n), repeat=2):
         a[i][j] = a[i-1][j] + a[i][j-1]

You could also generate a like this:

a = [[1 - (i > 0 < j) for i in range(n)] for j in range(n)]
Answered By: John La Rooy

You seem to be generating a grid of binomial coefficients.

Look how this is done with Scipy:

>>> from scipy import *
>>> n = 10
>>> fromfunction(lambda i,j: comb(i+j, j), shape=(n,n))
array([[  1.00e+00,   1.00e+00,   1.00e+00,   1.00e+00,   1.00e+00,   1.00e+00,   1.00e+00,   1.00e+00,   1.00e+00,   1.00e+00],
       [  1.00e+00,   2.00e+00,   3.00e+00,   4.00e+00,   5.00e+00,   6.00e+00,   7.00e+00,   8.00e+00,   9.00e+00,   1.00e+01],
       [  1.00e+00,   3.00e+00,   6.00e+00,   1.00e+01,   1.50e+01,   2.10e+01,   2.80e+01,   3.60e+01,   4.50e+01,   5.50e+01],
       [  1.00e+00,   4.00e+00,   1.00e+01,   2.00e+01,   3.50e+01,   5.60e+01,   8.40e+01,   1.20e+02,   1.65e+02,   2.20e+02],
       [  1.00e+00,   5.00e+00,   1.50e+01,   3.50e+01,   7.00e+01,   1.26e+02,   2.10e+02,   3.30e+02,   4.95e+02,   7.15e+02],
       [  1.00e+00,   6.00e+00,   2.10e+01,   5.60e+01,   1.26e+02,   2.52e+02,   4.62e+02,   7.92e+02,   1.29e+03,   2.00e+03],
       [  1.00e+00,   7.00e+00,   2.80e+01,   8.40e+01,   2.10e+02,   4.62e+02,   9.24e+02,   1.72e+03,   3.00e+03,   5.01e+03],
       [  1.00e+00,   8.00e+00,   3.60e+01,   1.20e+02,   3.30e+02,   7.92e+02,   1.72e+03,   3.43e+03,   6.43e+03,   1.14e+04],
       [  1.00e+00,   9.00e+00,   4.50e+01,   1.65e+02,   4.95e+02,   1.29e+03,   3.00e+03,   6.43e+03,   1.29e+04,   2.43e+04],
       [  1.00e+00,   1.00e+01,   5.50e+01,   2.20e+02,   7.15e+02,   2.00e+03,   5.00e+03,   1.14e+04,   2.43e+04,   4.86e+04]])

This is also much faster than iterating over each element indexes with for loops.


Simple is better than complex. PEP20

Answered By: ulidtko
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.