String manipulation in python..NEED HELP!!!!

Ross Ridge rridge at csclub.uwaterloo.ca
Tue Dec 11 15:52:30 EST 2012


John Gordon wrote:
> def encode(plain):
>     '''Return a substituted version of the plain text.'''
>     encoded = ''
>     for ch in plain:
>        encoded += key[alpha.index(ch)]
>     return encoded

Terry Reedy  <tjreedy at udel.edu> wrote:
>The turns an O(n) problem into a slow O(n*n) solution. Much better to 
>build a list of chars and then join them.

There have been much better suggestions in this thread, but John Gordon's
code above is faster than the equivilent list and join implementation
with Python 2.6 and Python 3.1 (the newest versions I have handy).
CPython optimized this case of string concatenation into O(n) back in
Python 2.4.

					Ross Ridge

-- 
 l/  //	  Ross Ridge -- The Great HTMU
[oo][oo]  rridge at csclub.uwaterloo.ca
-()-/()/  http://www.csclub.uwaterloo.ca/~rridge/ 
 db  //	  



More information about the Python-list mailing list