Python Forum
function giving different result with @functool.cache
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function giving different result with @functool.cache
#3
You can also make recursion a lot faster by using less of it.
def pascal_re(n):
    def nextrow(r):
        '''gives next row of pascal's triangle from previous row'''
        prerow = [1].append(prerow)  # Makes a new list.  Fixes shared list problem.
        for i in range(1, len(prerow) - 1):
            prerow[i] += prerow[i+1]
        return prerow

    if n == 1:
        return [[1]]
    tmp = pascal_re(n - 1)  # Compute once and use twice
    return tmp + [nextrow(tmp[-1])]
 
print(pascal_re(25))
naughtysensei likes this post
Reply


Messages In This Thread
RE: function giving different result with @functool.cache - by deanhystad - Nov-23-2020, 08:42 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Function producing no result aditvaddi 2 2,801 Jul-10-2018, 03:23 AM
Last Post: Nwb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020