Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As ~unutbu said, try the <a href="http://wiki.python.org/moin/PythonDecoratorLibrary#Memoize" rel="nofollow noreferrer">memoized decorator</a> and the following changes:</p> <pre><code>@memoized def search(a, b): # Initialize startup variables. nodes, index = [], [] a_size, b_size = len(a), len(b) # Begin to slice the sequences. for size in range(min(a_size, b_size), 0, -1): for a_addr in range(a_size - size + 1): # Slice "a" at address and end. a_term = a_addr + size a_root = list(a)[a_addr:a_term] #change to list for b_addr in range(b_size - size + 1): # Slice "b" at address and end. b_term = b_addr + size b_root = list(b)[b_addr:b_term] #change to list # Find out if slices are equal. if a_root == b_root: # Create prefix tree to search. a_pref, b_pref = list(a)[:a_addr], list(b)[:b_addr] p_tree = search(a_pref, b_pref) # Create suffix tree to search. a_suff, b_suff = list(a)[a_term:], list(b)[b_term:] s_tree = search(a_suff, b_suff) # Make completed slice objects. a_slic = Slice(a_pref, a_root, a_suff) b_slic = Slice(b_pref, b_root, b_suff) # Finish the match calculation. value = size + p_tree.value + s_tree.value match = Match(a_slic, b_slic, p_tree, s_tree, value) # Append results to tree lists. nodes.append(match) index.append(value) # Return largest matches found. if nodes: return Tree(nodes, index, max(index)) # Give caller null tree object. return Tree(nodes, index, 0) </code></pre> <p>For memoization, dictionaries are best, but they cannot be sliced, so they have to be changed to lists as indicated in the comments above.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload