From 6f4444f67e8cabe5bf10c93c29e3e844cfa5fe04 Mon Sep 17 00:00:00 2001 From: xenofem Date: Sun, 3 Mar 2024 11:42:21 -0500 Subject: [PATCH] misc corrections/optimizations in top() and similarity() --- dlibrary/dlibrary.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/dlibrary/dlibrary.py b/dlibrary/dlibrary.py index 97d657c..25cccb5 100755 --- a/dlibrary/dlibrary.py +++ b/dlibrary/dlibrary.py @@ -825,7 +825,7 @@ def copy_recursive(src, dest): memoized_similarities = {} def similarity(a, b): - if len(a) < len(b): + if len(a) < len(b) or (len(a) == len(b) and a < b): shorter = a longer = b else: @@ -849,18 +849,17 @@ def similarity(a, b): return result def top(items, n, key): - if len(items) == 0: - return [] - winners = items[:1] - for item in items[1:]: - if len(winners) < n or key(item) > key(winners[-1]): - for i in range(len(winners)): - if key(item) > key(winners[i]): - winners.insert(i, item) + winners = [] + for item in items: + score = key(item) + if len(winners) < n or score > winners[-1][1]: + for i in range(len(winners) + 1): + if i == len(winners) or score > winners[i][1]: + winners.insert(i, (item, score)) break - if len(winners) > n: + while len(winners) > n: winners.pop() - return winners + return [item for (item, score) in winners] def generate(args): jenv = Environment(