开发者

Optimize the code [closed]

Closed. This question is off-topic. It is not currently accepting answers.

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 11 years ago.

Improve this question

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 e开发者_如何学编程fficient . 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


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)


I think the code is quite pythonic, but as stated in the comments, it is a very bad idea to choose generic single letter variable words, and renders your code very hard to understand, and that is not pythonic.

This applies to every programming language, but in some cases even more so in python and other dynamically typed languages.

Try to be really generous and descriptive with variable names.

See the example (under "What is the meaning of 'Coding Horror'?") in this site creators blog.


You might want to consider factoring the memoization layer out of your class, along the lines of this decorator.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜