Reputation: 13
The general question is to remove all extra spaces from a python string so that there is only one space between each word, and no spaces at the beginning or end of the string.
For example, ' Hello to the world ' will return as 'Hello to the world'
We are not allowed to use the split command, or any commands besides basic string operators (length and concantenation) and if and while commands.
I have written my program so that it removes the spaces at the beginning of the string as well as extra spaces between words, however, if I have even one space after the last word in the input string the program returns an error that "index[i] is out of range".
Here is my program:
def cleanstring (s):
i=0
while s[i]==' ':
i=i+1
s=s[i:len(s)]
i=0
while i<len(s):
if s[i]!=' ':
i=i+1
else:
if i<len(s) and s[i]==' ':
i=i+1
if s[i]==' ' and i+1<len(s):
s=s[0:i]+s[i+1:len(s)]
i=0
return(s)
Hopefully someone can help me determine what is wrong with it. It seems as though I've tried everything but I know that it's just my inexperience with python.
Upvotes: 0
Views: 384
Reputation: 3013
Here is your implementation structure, with the required changes:
def cleanstring (s):
i=0
while s[i]==' ':
i=i+1
s=s[i:len(s)]
i=0
while i<len(s):
if s[i]!=' ':
i=i+1
else:
if i<len(s) and s[i]==' ':
i=i+1
if i<len(s) and s[i]==' ':
s=s[0:i]+s[i+1:len(s)]
i=0
if s[-1] == ' ':
s = s[:-1]
return(s)
What is changed is:
if s[i]==' ' and i+1<len(s):
To:
if i<len(s) and s[i]==' ':
But this will keep one more space at the end, so
if s[-1] == ' ':
s = s[:-1]
Enjoy.
Upvotes: 1
Reputation: 3267
Using some recursion
def cleanString(word):
if word[0]==" ":
if len(word)>1:
return cleanString(word[1:])
return word[1:]
elif len(word)>1:
return word[0]+cleanString(word[1:])
return word
print cleanString(" abcd ") == "abcd" # True
print cleanString(" abcd ") # abcd
Upvotes: 1
Reputation: 2560
This seems to work just fine. Check it out. The repr() just gives the string in quotes, in order to see the actual number of spaces before and after the words.
def cleanstring (s):
hit_letter = False
space_counter = 0
tmp = list()
for letter in s:
if letter.isalpha():
space_counter = 0
hit_letter = True
tmp.append(letter)
print letter
elif hit_letter:
space_counter += 1
if space_counter < 2:
tmp.append(letter)
return ''.join(tmp[:-1])
print repr(cleanstring(' Hi to the world '))
Upvotes: 1
Reputation: 878
You need to check your i is in range in one more place. Give this a shot:
def cleanstring(s):
i=0
while s[i]==' ':
i=i+1
s=s[i:len(s)]
i=0
while i<len(s):
if s[i]==' ':
if (i+1)<len(s) and s[i+1]==' ':
s=s[0:i]+s[i+1:len(s)]
else:
i=i+1
else:
i=i+1
if s[len(s)-1]==' ':
s=s[0:len(s)-1]
return(s)
Upvotes: 1
Reputation: 2244
There is actually an easy and clever fix. Change
if s[i]==' ' and i+1<len(s):
to...
if i<len(s) and s[i]==' ':
This works because Python short-circuits and
if a falsy value is encountered at any point. This means that after i<len(s)
evaluates to False and Python encounters and
, it will immediately move on to the elif
or else
clauses if there are any. Thus, the second half is never evaluated so there is no IndexError
.
Now, this program does not work perfectly. There are other problems with it, but as this is a homework assignment, I am reluctant to offer any more help. Aside from this hint: you need another while loop.
Upvotes: 2