That's Not How You Use Python

sivang | 28 March, 2010 10:09

I have dug an old code of mine a friend had to do a while back in a job interview. The interviewer specifically insisted on not using any of python's industry strength constructs to solve this problem.

 

Can you guess what this does and how to make it better given the constraints that are apparent from the code and the use of in place modification of the members of the list?

 

def compress_whitespace(thelist):
    my_index = 0
    for i in range(len(thelist)):
          if thelist[my_index-1]==' ' and thelist[my_index] == ' ':
             for t in range(my_index-1,len(thelist)-1):
                   thelist[t] = thelist[t+1]
             thelist[t+1] = ''
          my_index = my_index-1
 my_index += 1


 

if __name__ == "__main__":
 thelist = list(sys.argv[1])
 print compress_whitespace(thelist)
 print thelist
 

 

 

Actually, the code lost any indentation it had .....Let's change the subject, how do you insert code neatly in LifeType ? I googled and tried and did this and that and nothing... can anybody recommend a way ? :) 


RSSComments

Re: That's Not How You Use Python

calin2k | 28/03/2010, 13:06

it reduces double multiple consecutive spaces to a single space

You must login to post comments. Login
 
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved