Một số loại thuật toán/triển khai đệ quy hoạt động trên đầu vào danh sách là khá dễ dàng để đưa ra, nếu bạn biết "thủ thuật". Thủ thuật đó là:
Chỉ cần cho rằng bạn đã có một chức năng có thể làm những gì bạn muốn.
Đợi đã, không, điều đó không thực sự có ý nghĩa, phải không? Sau đó, chúng tôi đã được thực hiện.doesn't really make sense, does it? Then we'd already be done.
Hãy thử lại điều đó:
Chỉ cần cho rằng bạn đã có một chức năng có thể làm những gì bạn muốn (nhưng chỉ cho yếu tố đầu vào 1 nhỏ hơn bạn cần). (but only for inputs 1 element smaller than you need).
Ở đó, tốt hơn nhiều. Mặc dù hơi ngớ ngẩn, đó là một giả định chúng ta có thể làm việc.
Vậy chúng ta muốn gì? Trong ví dụ của bạn, nó sẽ trả về các yếu tố tối thiểu và tối đa của một danh sách. Giả sử chúng ta muốn họ được trả lại dưới dạng 2-tuple (a.k.a. một "cặp"):
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(lst) # I want a pony! assert lst_min == 0 # Wishful thinking assert lst_max == 100 # Wishful thinkingNếu chúng ta có một hàm ma thuật như vậy, chúng ta có thể sử dụng nó để giải quyết vấn đề cho kích thước đầu vào thực tế không? Hãy thử:
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_lstKhông thực sự dễ dàng, nhưng khá thẳng về phía trước, phải không? Nhưng hãy giả sử chức năng ma thuật của chúng tôi magic_min_max có một hạn chế bổ sung: nó không thể xử lý các danh sách trống. .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.)
Vì vậy, nếu lst có kích thước 1, chúng ta không được gọi hàm ma thuật. Không có vấn đề gì với chúng tôi, mặc dù. Trường hợp đó rất dễ phát hiện và dễ bị phá vỡ. Phần tử duy nhất là cả tối thiểu và tối đa của danh sách của nó, vì vậy chúng tôi chỉ trả lại hai lần:
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_lstVì vậy, đó là điều đó.
Nhưng chờ đã ... không có phép thuật. Nếu chúng ta muốn gọi một chức năng, nó phải thực sự tồn tại. Vì vậy, chúng tôi cần thực hiện một chức năng có thể trả về tối thiểu và tối đa của một danh sách, vì vậy chúng tôi có thể gọi nó trong ____99 thay vì magic_min_max. Vì đây là về đệ quy, bạn biết giải pháp: real_min_max là hàm đó (một khi nó được sửa bằng cách gọi một hàm tồn tại) để chúng ta có thể tự gọi nó: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_lstHãy thử nó:
lst = [5, 4, 100, 0, 2] real_min_max(lst) # returns (0, 100)Nó hoạt động!
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc reduce(), lambda
Bàn luận
Example:
Điều kiện tiên quyết: Giảm (), Lambda
Thành phần chức năng là cách kết hợp hai hoặc nhiều hàm theo cách mà đầu ra của một hàm trở thành đầu vào của hàm thứ hai, v.v. Ví dụ: hãy để có hai hàm () hàm số.
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 2 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 3
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 4def 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 5 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 6def 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 7 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 8
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 6def 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 7def 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 8def 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 9
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 0def 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 1def 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 2def 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 3
Output:
Adding 2 to 5 and multiplying the result with 2: 14def 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
2 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
0
First the 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
4function is called on input 5. The 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
5 adds 2 to the input and the output which is 7, is given as the input to 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
6 which multiplies it by 2 and the output is
14
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 4def 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 5 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 6def 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 4 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 8
Giải thíchFirst 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 4function được gọi là đầu vào 5. 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 5 thêm 2 vào đầu vào và đầu ra là 7, được đưa ra làm đầu vào cho 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 6 nhân với 2 và đầu ra là 14 là 14
Cách tốt hơn để thực hiện sáng tác
Có một cách tốt hơn để thực hiện thành phần của chức năng. Chúng ta có thể tạo một chức năng đặc biệt có thể kết hợp bất kỳ hai chức năng nào.
Điều kiện tiên quyết: Giảm (), Lambda
Thành phần chức năng là cách kết hợp hai hoặc nhiều hàm theo cách mà đầu ra của một hàm trở thành đầu vào của hàm thứ hai, v.v. Ví dụ: hãy để có hai hàm () hàm số.
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 2 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 3
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 4def 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 5 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 6def 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 7 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 8
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 2 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 0
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 4def 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 5 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 6def 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 4 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 8
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 0Adding 2 to 5 and multiplying the result with 2: 14 5def 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 2Adding 2 to 5 and multiplying the result with 2: 14 7
Output:
Adding 2 to 5 and multiplying the result with 2: 14Giải thíchFirst 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
4function được gọi là đầu vào 5. 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
5 thêm 2 vào đầu vào và đầu ra là 7, được đưa ra làm đầu vào cho 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
6 nhân với 2 và đầu ra là 14 là 14
We can compose any number of function by modifying the above method.
Cách tốt hơn để thực hiện sáng tác
Có một cách tốt hơn để thực hiện thành phần của chức năng. Chúng ta có thể tạo một chức năng đặc biệt có thể kết hợp bất kỳ hai chức năng nào.
Điều kiện tiên quyết: Giảm (), Lambda
Thành phần chức năng là cách kết hợp hai hoặc nhiều hàm theo cách mà đầu ra của một hàm trở thành đầu vào của hàm thứ hai, v.v. Ví dụ: hãy để có hai hàm () hàm số.
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 2 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 3
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 4def 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 5 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 6def 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 7 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 8
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 2 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 0
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 4def 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 5 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 6def 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 4 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 8
Giải thíchFirst 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 4function được gọi là đầu vào 5. 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 5 thêm 2 vào đầu vào và đầu ra là 7, được đưa ra làm đầu vào cho 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 6 nhân với 2 và đầu ra là 14 là 14
real_min_max8real_min_max9
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 00def 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 01
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 6def 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 7def 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 04Adding 2 to 5 and multiplying the result with 2: 14 3
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 0def 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 07def 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 2Adding 2 to 5 and multiplying the result with 2: 14 7
Output:
Cách tốt hơn để thực hiện sáng tác
Có một cách tốt hơn để thực hiện thành phần của chức năng. Chúng ta có thể tạo một chức năng đặc biệt có thể kết hợp bất kỳ hai chức năng nào.
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 2 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 8
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 4def 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 5 lst = [5, 4, 100, 0, 2] real_min_max(lst) # returns (0, 100) 1 lst = [5, 4, 100, 0, 2] real_min_max(lst) # returns (0, 100) 2
Adding 2 to 5 and multiplying the result with 2: 14 7Adding 2 to 5 and multiplying the result with 2: 14 8 Adding 2 to 5 and multiplying the result with 2: 14 9
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 6def 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 7def 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 8Adding 2 to 5 and multiplying the result with 2: 14 3
Biên soạn n số hàm của chức năng có thể tạo ra bất kỳ số lượng chức năng nào bằng cách sửa đổi phương thức trên.
Điều kiện tiên quyết: Giảm (), Lambda
Thành phần chức năng là cách kết hợp hai hoặc nhiều hàm theo cách mà đầu ra của một hàm trở thành đầu vào của hàm thứ hai, v.v. Ví dụ: hãy để có hai hàm () hàm số.
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 2 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 3
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 4def 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 5 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 6def 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 7 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 8
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 2 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 0
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 4def 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 5 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 6def 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 4 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 8
Giải thíchFirst 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 4function được gọi là đầu vào 5. 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 5 thêm 2 vào đầu vào và đầu ra là 7, được đưa ra làm đầu vào cho 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 6 nhân với 2 và đầu ra là 14 là 14
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 00def 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 56
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 00def 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 01
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 6def 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 7def 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 04def 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 9
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 0def 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 07def 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 2Adding 2 to 5 and multiplying the result with 2: 14 7
Output:
Cách tốt hơn để thực hiện sáng tác
Có một cách tốt hơn để thực hiện thành phần của chức năng. Chúng ta có thể tạo một chức năng đặc biệt có thể kết hợp bất kỳ hai chức năng nào.
The 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
67 function is taking first two function from *func and composing them using 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
68
and then composing the third function to the previous composed function and so on. Here the 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
6 and 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
70 is composed first (multiply(subtract(x)) and then add() is composed (multiply(subtract(add(x))).