maximum recursion depth exceeded in comparison

后端 未结 6 1966
故里飘歌
故里飘歌 2021-01-06 13:21

I wrote this piece of code to compute the number of combinations:

def fact(n):
    return 1 if(n == 1) else n * fact(n - 1)

def combinations(n,k):
    retur         


        
6条回答
  •  醉话见心
    2021-01-06 14:07

    Try to replace:

    def fact(n):
        return 1 if(n == 1) else n * fact(n - 1)
    

    to:

    def fact(n):
        return 1 if(n <= 1) else n * fact(n - 1)
    

    Because if you pass 2 identical numbers, you would try to compute fact(0) (which would call fact(-1) and fact(-2), etc until the maximum recursion depth error).

提交回复
热议问题