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

  • SEARCH
  • Home
  • 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 5971797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:34:03+00:00 2026-05-22T20:34:03+00:00

I am new to python. I would like all the python gurus to suggest

  • 0

I am new to python. I would like all the python gurus to suggest some ways to make the following code more pythonic and hence more efficient .
Its a simple code to find the edit distance between two words.

P.S. I would like improvements in code, not in the logic or algorithm optimization.

class test:
    def __init__(self,a,b,I=1,D=1,R=1):
        self.a = a
        self.b = b
        self.mem = dict()
        self.la = len(a)
        self.lb = len(b)
        self.I = I
        self.D = D
        self.R = R

    def diff(self,i=0,j=0):
        T = self.diff
        memo = self.mem
        if j == self.lb: return self.D * i
        if i == self.la: return self.D * j
        if (i,j) in memo:
            return memo[(i,j)]
        if self.a[i] == self.b[j]:
            memo[(i,j)] = T( i+1,j+1 )
            return memo[ (i,j) ]
        memo[(i,j)] = min(self.R + T(i+1,j+1) , self.D + T(i+1,j) , self.I + T(i,j+1) ,
         self.D + T(i,j+1) , self.I + T(i+1,j) )
        return memo[(i,j)]

Variable explanation:

a,b are two string whose edit distance is to be found.
I,D,R Insertion Deletion and Replace cost of a single letter.
mem is dictionary used to memoize the recursive calls.
i and j are the pointers of the string a and b respectively

  • 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-22T20:34:04+00:00Added an answer on May 22, 2026 at 8:34 pm

    Pythonic would be:

    1. Write lots of unit tests.
    2. Don’t reinvent the wheel: search online for previous solutions to the problem. See the comments.
    3. Don’t optimise prematurely: profile your code to work out whether this really is
      a bottleneck and if it is improve the algorithm.
    4. Meaningful variable names
    5. Don’t start local variable names with capital letters
    6. Use normal whitespace inside parentheses T(i+1, j+1) not T( i+1,j+1 )
    7. Don’t use spurious parentheses round tuples: memo[i,j] not memo[(i,j)]
    8. Don’t optimise prematurely: self.diff(i+1, j+1) not T(i+1,j+1)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to python and I would like some help. I created some classes
I am new to Python, and I'm working on writing some database code using
I would like to use the python logging module to log all of the
I am relatively new to python (already did some 1h scripts like a little
I am new to python and am writing some scripts to automate downloading files
I'm writing a python module and I would like to unit test it. I
I'm developing a Python 2.6 package in which I would like to fetch a
I would like to automatically determine all of the properties (including the hidden ones)
I would like to know if there is a native datatype in Python that
I would like to display some content located in my models in some of

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.