将具有单个或多个参数的函数传递给另一个函数
我正在对一些简单的阶乘算法进行一些算法运行时间分析,为了避免代码重复,我尝试创建一个单一的时间分析函数,它将不同的阶乘算法函数作为参数。
当我传入仅接受一个参数的阶乘函数(例如迭代)时,这工作得很好,但作为我的作业的一部分,我还必须使用尾递归算法,该算法接受 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码:
您可以为此创建一个装饰器。
Code:
You can create a decorator for this.