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à:
Đợ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.
Ở đó, 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"):
Nế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ử:
Khô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, đó 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:
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
ĐọcBàn luậnXem 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
3def 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
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
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
9def 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
3Output:
Adding 2 to 5 and multiplying the result with 2: 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
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
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
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à 14Cá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
3def 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
8def 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
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
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
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 = 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
7Output:
Adding 2 to 5 and multiplying the result with 2: 14
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
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
3def 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
8def 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
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
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
8Giả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à 14real_min_max
8real_min_max
9
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
01def 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
3def 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
7Output:
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
8def 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)
2Adding 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
9def 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
3Biê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
3def 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
8def 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
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
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
8Giả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à 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
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
56def 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
01def 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
9def 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
7Output:
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))).