I feel stoopid ... this code is to verbose

Max M maxm at mxm.dk
Mon Jun 17 09:24:47 CEST 2002


Joseph Wilhelm wrote:
> On Fri, 2002-06-14 at 09:19, Max M wrote:
> <snip> 
> 
>>def stringSlicer(string, chunkSize=3):
>>     chunkList = []
>>     reverseString = list(string)
>>     reverseString.reverse()
>>     for i in range(0, len(string), chunkSize):
>>         chunk = reverseString[i:i+chunkSize]
>>         chunk.reverse()
>>         chunk = ''.join(chunk)
>>         chunkList.append(chunk)
>>     chunkList.reverse()
>>     return '.'.join(chunkList)

> import string
> 
> def stringSlicer( str, chunkSize=3 ):
> 	parts = list( str )
> 	for x in range( ( len( parts ) - chunkSize ), 0, -chunkSize ):
> 		parts.insert( x, "." )
> 	return string.join( parts, "" )
> 
> print stringSlicer( '42000000' )

Yeah ... this was the one I was looking for. Why the h... didn't I do 
this one myself? Well I know why! I didn't take the time to do a 
pseudo-run on paper before coding. Let this be a lesson to me... once 
more :-)

I gotta stop eating all those simple Carbo hydrates. It makes the brain 
implode into uselessness.

Well I made a few stylechanges and it fit me perfectly:

def stringSlicer(str, chunkSize=3):
     parts = list(str)
     for x in range((len(parts) - chunkSize), 0, -chunkSize):
         parts.insert(x, ".")
     return ''.join(parts)

Easy on the eyes and the mind.

I find it really amusing that even in Python there is such a wide array 
of approaches to such a relatively simple and well defined problem.

Thanks to all for the suggestions.


regards Max M




More information about the Python-list mailing list