Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I saw a posting on Hacker News this morning that was ranting about people not being able to solve an interview question. I thought I would give it a shot, but I would like to know how my attempt could be improved.

def get_pascal_row(n):
    Returns the nth row of Pascal's Triangle for a given n. Uses
    Gray's algorithm.
    if n == 0: return []
    n -= 1
    row = [1]
    for i in xrange(1,n+1):
        row.append(row[-1] * n/i)
        n -= 1
    return row
share|improve this question

migrated from Jan 24 '13 at 3:11

This question came from our site for professional and enthusiast programmers.

Stop half way through and stick a copy of the first half reversed to the end of your return. – Jaime Jan 24 '13 at 1:45

You can memoize the rows you have already computed previously.

Alternatively, you could compute it in closed form using the binomial coefficients:

enter image description here

Start looking in scipy.special.binom.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.