python组合函数_需要将两个函数合并为一个(Python)

Here is my code-

def Max(lst):

if len(lst) == 1:

return lst[0]

else:

m = Max(lst[1:])

if m > lst[0]:

return m

else:

return lst[0]

def Min(lst):

if len(lst) == 1:

return lst[0]

else:

m = Min(lst[1:])

if m < lst[0]:

return m

else:

return lst[0]

print("Max number:",Max([5,4,100,0,2]))

print("Min number:",Min([5,4,100,0,2]))

Basically I need a single function that returns both the largest and smallest number and it needs to be recursively. How would would I change this code?

解决方案

Some types of recursive algorithms/implementations operating on a list input are very quite easy to come up with, if you know the "trick". That trick being:

Just assume you already have a function that can do what you want.

Wait, no, that doesn't really make sense, does it? Then we'd already be done.

Let's try that again:

Just assume you already have a function that can do what you want (but only for inputs 1 element smaller than you need).

There, much better. While a bit silly, that's an assumption we can work with.

So what do we want? In your example, it's returning the minimum and maximum elements of a list. Let's assume we want them returned as a 2-tuple (a.k.a. a "pair"):

lst = [5, 4, 100, 0, 2]

# Well, actually, we can only do this for a smaller list,

# as per our assumption above.

lst = lst[1:]

lst_min, lst_max = magic_min_max(l) # I want a pony!

assert lst_min == 0 # Wishful thinking

assert lst_max == 100 # Wishful thinking

If we have such a magic function, can we use it to solve the problem for the actual input size? Let's try:

def real_min_max(lst):

candidate = lst[0]

rest_of_the_list = lst[1:]

min_of_rest, max_of_rest = magic_min_max(rest_of_the_list) # Allowed because

# smaller than lst

min_of_lst = candidate if candidate < min_of_rest else min_of_rest

max_of_lst = candidate if candidate > max_of_rest else max_of_rest

return min_of_lst, max_of_lst

Not exactly easy, but pretty straight forward, isn't it? But let's assume our magic function magic_min_max has an additional restriction: It cannot handle empty lists. (After all, an empty list doesn't have neither a minimum nor a maximum element. Not even magic can change that.)

So if lst has size 1, we must not call the magic function. No problem for us, though. That case is easy to detect and easy to circumvent. The single element is both minimum and maximum of its list, so we just return it twice:

def real_min_max(lst):

candidate = lst[0]

if len(lst) == 1:

return candidate, candidate # single element is both min & max

rest_of_the_list = lst[1:]

min_of_rest, max_of_rest = magic_min_max(rest_of_the_list) # Allowed because

# smaller than lst

# but (if we get

# here) not empty

min_of_lst = candidate if candidate < min_of_rest else min_of_rest

max_of_lst = candidate if candidate > max_of_rest else max_of_rest

return min_of_lst, max_of_lst

So that's that.

But wait ... there is no magic. If we want to call a function, it has to actually exist. So we need to implement a function that can return the minimum and maximum of a list, so we can call it in real_min_max instead of magic_min_max. As this is about recursion, you know the solution: real_min_max is that function (once it's fixed by calling a function that does exist) so we can have it call itself:

def real_min_max(lst):

candidate = lst[0]

if len(lst) == 1:

return candidate, candidate # single element is both min & max

rest_of_the_list = lst[1:]

min_of_rest, max_of_rest = real_min_max(rest_of_the_list) # No magic needed,

# just recursion!

min_of_lst = candidate if candidate < min_of_rest else min_of_rest

max_of_lst = candidate if candidate > max_of_rest else max_of_rest

return min_of_lst, max_of_lst

Let's try it:

lst = [5, 4, 100, 0, 2]

real_min_max(lst) # returns (0, 100)

It works!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值