I am a beginner in Python, and am doing some simple problems. However, I am having trouble with the following question:
Define a simple "spelling correction" function correct() that takes a
string and sees to it that:
1) two or more occurrences of the space character is compressed into
2) inserts an extra space after a period if the period is directly
followed by a letter. E.g. correct("This (multiple spaces) is (multiple spaces) very funny (multiple spaces) and (multiple spaces) cool.Indeed!") should return "This is very funny and cool. Indeed!"
Tip: Use regular expressions!
y = x.split()
y = list(y)
for i in y:
if i == " ":
for o in y:
if o == " ":
if i == ".":
y = " ".join(y)
print(correct("This is very funny and cool.Indeed!"))
Strings are immutable, but act similarly to lists. You don't necessarily need to "convert" them to a list. You can do something like:
myString = "Insert Here -><- Insert There" myString = myString[:14] + "Inserted" + mystring[14:] # => myString == "Insert Here ->Inserted<- Insert There"
However, yes, strings are immutable and cannot be 'inserted' into like a list can. For that, you must convert them to a list.
In your case, you may find use in the
re regular expression module. In order to compress multiple occurances together, you can do something like:
import re answer = re.sub(r' +', r' ', myString)
Replace, however, does work because it creates a new instance.
>>> s = "Test" >>> id(s) 41325568 >>> s = s.replace("s", "x") >>> id(s) 41325600 >>> s 'Text' >>>
So you can easily replace "." with ". " and then run the regular expression to compress it. The end result would look something like:
def correct(myString): myString = myString.replace(".", ". ") # Even if there is already a space, it doesn't matter import re return re.sub(r' +', r' ', myString)
If you don't want, or are not allowed, to use the
re module, you can use this one-liner
>>> def correct(myString): ... return ' '.join(myString.replace('.', '. ').split()) ... >>> myString = "This is very funny and cool.Indeed!" >>> correct(myString) 'This is very funny and cool. Indeed!' >>>
Note that this does not take into account multiple periods or other punctuation types, or if the last character is a period. It will also collapse other forms of whitespace (tabs, etc). So:
"In a galaxy far,far away..."
"In a galaxy far,far away. . ."
We'd recommend using
re for this. It's also significantly faster.