Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 920749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:46:10+00:00 2026-05-15T18:46:10+00:00

After finding the difflib.SequenceMatcher class in Python’s standard library to be unsuitable for my

  • 0

After finding the difflib.SequenceMatcher class in Python’s standard library to be unsuitable for my needs, a generic “diff”-ing module was written to solve a problem space. After having several months to think more about what it is doing, the recursive algorithm appears to be searching more than in needs to by re-searching the same areas in a sequence that a separate “search thread” may have also examined.

The purpose of the diff module is to compute the difference and similarities between a pair of sequences (list, tuple, string, bytes, bytearray, et cetera). The initial version was much slower than the code’s current form, having seen a speed increase by a factor of ten. How can memoization be applied to the following code? What is the best way to rewrite the algorithm to further increase any possible speed?


class Slice:

    __slots__ = 'prefix', 'root', 'suffix'

    def __init__(self, prefix, root, suffix):
        self.prefix = prefix
        self.root = root
        self.suffix = suffix

################################################################################

class Match:

    __slots__ = 'a', 'b', 'prefix', 'suffix', 'value'

    def __init__(self, a, b, prefix, suffix, value):
        self.a = a
        self.b = b
        self.prefix = prefix
        self.suffix = suffix
        self.value = value

################################################################################

class Tree:

    __slots__ = 'nodes', 'index', 'value'

    def __init__(self, nodes, index, value):
        self.nodes = nodes
        self.index = index
        self.value = value

################################################################################

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 = a[a_addr:a_term]
            for b_addr in range(b_size - size + 1):
                # Slice "b" at address and end.
                b_term = b_addr + size
                b_root = b[b_addr:b_term]
                # Find out if slices are equal.
                if a_root == b_root:
                    # Create prefix tree to search.
                    a_pref, b_pref = a[:a_addr], b[:b_addr]
                    p_tree = search(a_pref, b_pref)
                    # Create suffix tree to search.
                    a_suff, b_suff = a[a_term:], 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)

Reference: How to optimize a recursive algorithm to not repeat itself?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:46:10+00:00Added an answer on May 15, 2026 at 6:46 pm

    As ~unutbu said, try the memoized decorator and the following changes:

    @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)
    

    For memoization, dictionaries are best, but they cannot be sliced, so they have to be changed to lists as indicated in the comments above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 463k
  • Answers 463k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I implemented something very similar to what you describe in… May 16, 2026 at 12:46 am
  • Editorial Team
    Editorial Team added an answer [^/{1,2}] means "every character except /, {, 1, ,, 2… May 16, 2026 at 12:46 am
  • Editorial Team
    Editorial Team added an answer I have also the same problem. I think it has… May 16, 2026 at 12:46 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.