misc corrections/optimizations in top() and similarity()
This commit is contained in:
parent
42b49c7ecc
commit
6f4444f67e
|
@ -825,7 +825,7 @@ def copy_recursive(src, dest):
|
||||||
memoized_similarities = {}
|
memoized_similarities = {}
|
||||||
|
|
||||||
def similarity(a, b):
|
def similarity(a, b):
|
||||||
if len(a) < len(b):
|
if len(a) < len(b) or (len(a) == len(b) and a < b):
|
||||||
shorter = a
|
shorter = a
|
||||||
longer = b
|
longer = b
|
||||||
else:
|
else:
|
||||||
|
@ -849,18 +849,17 @@ def similarity(a, b):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def top(items, n, key):
|
def top(items, n, key):
|
||||||
if len(items) == 0:
|
winners = []
|
||||||
return []
|
for item in items:
|
||||||
winners = items[:1]
|
score = key(item)
|
||||||
for item in items[1:]:
|
if len(winners) < n or score > winners[-1][1]:
|
||||||
if len(winners) < n or key(item) > key(winners[-1]):
|
for i in range(len(winners) + 1):
|
||||||
for i in range(len(winners)):
|
if i == len(winners) or score > winners[i][1]:
|
||||||
if key(item) > key(winners[i]):
|
winners.insert(i, (item, score))
|
||||||
winners.insert(i, item)
|
|
||||||
break
|
break
|
||||||
if len(winners) > n:
|
while len(winners) > n:
|
||||||
winners.pop()
|
winners.pop()
|
||||||
return winners
|
return [item for (item, score) in winners]
|
||||||
|
|
||||||
def generate(args):
|
def generate(args):
|
||||||
jenv = Environment(
|
jenv = Environment(
|
||||||
|
|
Loading…
Reference in a new issue