Answer the question
In order to leave comments, you need to log in
What does compare=operator mean. lt in this code? And how does the compare operator work here?
In one Internet resource I found an implementation of merge sort:
import operator
def merge_sort(L, compare=operator.lt):
if len(L) < 2:
return L[:]
else:
middle = int(len(L) / 2)
left = merge_sort(L[:middle], compare)
right = merge_sort(L[middle:], compare)
print(left)
return merge(left, right, compare)
def merge(left, right, compare):
result = []
i, j = 0, 0
while i < len(left) and j < len(right):
if compare(left[i], right[j]):
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
while i < len(left):
result.append(left[i])
i += 1
while j < len(right):
result.append(right[j])
j += 1
return result
Answer the question
In order to leave comments, you need to log in
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from operator import lt
>>> help(lt)
Help on built-in function lt in module _operator:
lt(...)
lt(a, b) -- Same as a<b.
>>>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question