Rails Console Autocorrect


Collapse Content

This challenge combines the Rails console with the Missing Text challenge, with some new twists. People sometimes mistakenly skip spaces when typing and sometimes they accidentally add single characters before or after words. You're job will be to auto-correct such input when possible.

  • If someone tries to create a post with invalid words, output "Invalid post" and do not create the post.
  • However, if the post can be created by inserting new spaces, go ahead and create the post with the corrected text.
  • New Twist: If you cannot create a post just by inserting spaces, check if the input contains one or more 'mistaken' single characters at the beginning or end of any word. Remove that character and create the post with the corrected text.
  • Only add spaces if you cannot create a valid post otherwise. Only remove a character if you cannot create a valid post just by adding spaces.
  • When faced with two otherwise equal ways of splitting text, pick the longer words first. For example, "area" should stay as "area" instead of becoming "are" "a".

You are given the following list of valid words for this challenge:

["a", "ah", "able", "ad", "are", "area", "be", "been", "car", "care", "ear", "hello", "not", "note", "or", "read", "ready", "to", "the", "world"]

Examples:

input corrected text operation
"hello theworld" "hello the world" insert missing space
"hellozworld" "hello worlds" remove single character and replace with space
"helloz" "hello" remove single character
"carenotear" "care not ear" add 3 spaces
"carernotzear" "care not ear" removed "r" and "z"

The input below cannot be fixed, so just print out "Invalid post":

  • "hellozzworld" (cannot remove 2 continuous characters.)
  • "hezllo" (cannot remove characters from inside word.)
  • "zcareadyz" (no way to fix.)

Challenge

Create the Console Autocorrect as described above.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Contact Us