You've just finished writing the last chapter for your novel when a virus suddenly infects your document. It has swapped the 'i's and 'e's in 'ei' words and capitalized random letters. In today's challenge, you will write a function which will:
a) remove the spelling errors in 'ei' words. (Example of 'ei' words: their, caffeine, deceive, weight)
b) only capitalise the first letter of each sentence. Make sure the rest of the sentence is in lower case.
Example: He haD iEght ShOTs of CAffIEne. => He had eight shots of caffeine.
Good luck and Happy Coding~!
This challenge comes from kkavita92 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge for a future post? Email email@example.com with your suggestions!
Top comments (20)
But this solution has the same problem that La battle mentions in another comment: if a word contains a valid "ie" string, it will be transformed into "ei" incorrectly. I was testing the sentence
He haD iEght ShOTs of CAffIEne. thEn hE dIeDand the last "dIeD" was being transformed into "deid". One option could be extending the function to check a dictionary or make a call to an API to verify the word is valid, but that could be a pain too.
Another possible extension would be to split not only by
., but also by other sentence delimiters such as
Also, if your sentence contains nouns such as
He haD iEght ShOTs of CAffIEne in Paris. thEn hE dIeD, you will loose the upper
edit: I changed my code to only consider periods at the end of a word so words like
nice.gifwouldn't be considered as the end of a sentence.
Nice, that way (checking words containing dots) you can check if it is needed to capitalize more than once in the same line, clever!!
I edited my answer so my code would not consider periods in the middle of a word to be the end of a sentence (ie:
This file is named nice.gif and it is weird.)
Well, this solution will work in every case but won't be able to handle the 21775 "ie" words:
I was doing something like this. Then I deid testing.
There's a known set of rules to avoid handle all the "ie" words:
After a day, I feel
def cureis better,
Ohh with all the new syntax from 2.7 ;))
Yes! I love to try new syntax😆 So I use 2.7 in #challenge to promote the goodness of ruby 2.7✨
My solution in js
As others mentioned this solution will change all the proper words with ie to ei and changes the first character of a noun to lowercase.
Python one-liner :
If it's a document, it will contains multiple sentences and capitalize is only uppercasing the first letter of a string
Ok then split document by lines and call the function :
Or capitalize each line of the document after lowering :)
Your solution is very good btw. Good job
Rust Solution: Playground