将具有单个或多个参数的函数传递给另一个函数

发布于 2025-01-11 16:05:55 字数 1714 浏览 0 评论 0原文

我正在对一些简单的阶乘算法进行一些算法运行时间分析,为了避免代码重复,我尝试创建一个单一的时间分析函数,它将不同的阶乘算法函数作为参数。

当我传入仅接受一个参数的阶乘函数(例如迭代)时,这工作得很好,但作为我的作业的一部分,我还必须使用尾递归算法,该算法接受 2 个参数(n,累加器)。这是我的代码:

def iterFactorial(n):
    
    #running product
    factorial = 1
    
    #multiply each number in range 1 to n by the running product(i.e. the factorial)
    for i in range(1,n+1):
        
        factorial *= i
    #return the factorial
    return factorial 


def tail_Recur_Factorial(n,accumulator):
    
    if n == 1:
        return accumulator
    return tail_Recur_Factorial(n-1,n*accumulator)

running_times_1 = []
n_values = [i for i  in range(1,1000,100)]

def TimeAnalyis(function):
    
    #array for storing  multiple temporary running times of algorithm for a given n
    temp_time = [0]

    
    for n in n_values:
        
    
        #run fucntion 50 times for each given n value 
        for i in range(50):
            
            #take current timestamp
            start_time = time.time()
            
            #run function
            function(n)
            
            #take current timestamp
            end_time = time.time()
            
            #calculate time taken for function to run
            function_time = end_time - start_time
            
            #append to temp time array so mean can be taken
            temp_time.append(function_time)
            
        #take average running time
        running_times_1.append(statistics.mean(temp_time))
        
        #reset temp time
        temp_time = [0]

时间分析函数对于迭代阶乘函数运行良好,我在其中调用如下:

timeAnalysis(iterFactorial)

如何修改时间分析函数以允许我ALSO传入尾部带有两个参数的递归函数?

I am doing some algorithm running time analysis on some simple factorial algorithms, and to avoid code repetition I am trying to create a single time analysis function that will take the different factorial algorithm functions as arguements.

When I pass in factorial functions (e.g. iterative) that only take one argument this works fine, but as part of my assignment I have to use a tail recursive algorithm also, which takes 2 arguments (n,accumlator). Here is my code:

def iterFactorial(n):
    
    #running product
    factorial = 1
    
    #multiply each number in range 1 to n by the running product(i.e. the factorial)
    for i in range(1,n+1):
        
        factorial *= i
    #return the factorial
    return factorial 


def tail_Recur_Factorial(n,accumulator):
    
    if n == 1:
        return accumulator
    return tail_Recur_Factorial(n-1,n*accumulator)

running_times_1 = []
n_values = [i for i  in range(1,1000,100)]

def TimeAnalyis(function):
    
    #array for storing  multiple temporary running times of algorithm for a given n
    temp_time = [0]

    
    for n in n_values:
        
    
        #run fucntion 50 times for each given n value 
        for i in range(50):
            
            #take current timestamp
            start_time = time.time()
            
            #run function
            function(n)
            
            #take current timestamp
            end_time = time.time()
            
            #calculate time taken for function to run
            function_time = end_time - start_time
            
            #append to temp time array so mean can be taken
            temp_time.append(function_time)
            
        #take average running time
        running_times_1.append(statistics.mean(temp_time))
        
        #reset temp time
        temp_time = [0]

The time analysis fucntion works fine for the iterative factorial function, where I call like:

timeAnalysis(iterFactorial)

How can I modify the time analysis function to allow me to ALSO pass in the tail recrusive fucntion which takes two arguments?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

找回味觉 2025-01-18 16:05:55

代码:

您可以为此创建一个装饰器。

def TimeAnalyis(func):
    #Takes any number of args and kwargs
    def wrapper(*args,**kargs):
        best = float('inf')
        for i in range(50):
            start = time.time()
            # Calls function and stores returned values in result
            result = func(*args,**kargs)
            total = time.time()-start
            if total<best:
                best = total
        # Print best timing
        print(f'Best time taken is {best} seconds')
        # returns the values returned by function
        return result
    # returns wrapper function
    return wrapper

#decorator
@TimeAnalyis
def anyfunc(n,m):
    for i in range(n):
         print(i*m)

Code:

You can create a decorator for this.

def TimeAnalyis(func):
    #Takes any number of args and kwargs
    def wrapper(*args,**kargs):
        best = float('inf')
        for i in range(50):
            start = time.time()
            # Calls function and stores returned values in result
            result = func(*args,**kargs)
            total = time.time()-start
            if total<best:
                best = total
        # Print best timing
        print(f'Best time taken is {best} seconds')
        # returns the values returned by function
        return result
    # returns wrapper function
    return wrapper

#decorator
@TimeAnalyis
def anyfunc(n,m):
    for i in range(n):
         print(i*m)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文