MCPcopy Create free account
hub / github.com/google-research/language / _lcs

Function _lcs

language/table_text_eval/table_text_eval.py:263–287  ·  view source on GitHub ↗

Computes the length of the LCS between two seqs. The implementation below uses a DP programming algorithm and runs in O(nm) time where n = len(x) and m = len(y). Source: http://www.algorithmist.com/index.php/Longest_Common_Subsequence Args: x: collection of words y: collection of w

(x, y)

Source from the content-addressed store, hash-verified

261
262
263def _lcs(x, y):
264 """Computes the length of the LCS between two seqs.
265
266 The implementation below uses a DP programming algorithm and runs
267 in O(nm) time where n = len(x) and m = len(y).
268 Source: http://www.algorithmist.com/index.php/Longest_Common_Subsequence
269
270 Args:
271 x: collection of words
272 y: collection of words
273
274 Returns:
275 Table of dictionary of coord and len lcs
276 """
277 n, m = len(x), len(y)
278 table = dict()
279 for i in range(n + 1):
280 for j in range(m + 1):
281 if i == 0 or j == 0:
282 table[i, j] = 0
283 elif x[i - 1] == y[j - 1]:
284 table[i, j] = table[i - 1, j - 1] + 1
285 else:
286 table[i, j] = max(table[i - 1, j], table[i, j - 1])
287 return table
288
289
290def _ngrams(sequence, order):

Callers 1

_len_lcsFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…