-2

Here is my code:

def getRow(rowIndex):

    if rowIndex == 0:
        return [1]
    if rowIndex == 1:
        return [1,1]

    def f(n,r):
        if n == 1:
            print(r)
            return r
        r = [1]+[r[i] + r[i+1] for i in range(len(r)-1)]+[1]
        f(n-1,r)

    return f(rowIndex,[1,1])

print(getRow(4))

That print(r) function worked perfectly and gave the right answer, however, the return r statement just didn't work at all. Anyone knows why is this happening?

Desired output: [1,4,6,4,1]

Actual output: null

I would like to know why doesn't the return r statement in the f function return anything through the return f(rowIndex,[1,1]) statement.

Joshua
  • 40,822
  • 8
  • 72
  • 132
Hang Lin
  • 51
  • 4
  • 8
    You need to `return f(n-1,r)` – OneCricketeer Jul 31 '18 at 23:57
  • 1
    Your question is not clear, because your statements "the right answer" and "just didn't work at all" are vague. You showed your code and the input--now clearly state the desired output, actual output, and just how the actual output is wrong. Show the full traceback for any errors. Read and follow [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Rory Daulton Aug 01 '18 at 00:14

1 Answers1

0

the return r statement just didn't work at all

If the print works, then the return works. However, it only works when n == 1.

In all other cases, your function will essentially return None (not null) since you have no other return statement within the f function body.

Perhaps you can try return f(n-1,r) rather than simply calling the function and ignoring the return result

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245