Skip to content

Instantly share code, notes, and snippets.

@RickGriff
Created January 21, 2018 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RickGriff/78f8acdf83f25275b8b30a8ccecdf4be to your computer and use it in GitHub Desktop.
Save RickGriff/78f8acdf83f25275b8b30a8ccecdf4be to your computer and use it in GitHub Desktop.
Codewars Challenge: Evil Autocorrect Prank
# Your friend won't stop texting his girlfriend. It's all he does. All day. Seriously. The texts are so mushy too! The whole situation just makes you feel ill. Being the wonderful friend that you are, you hatch an evil plot. While he's sleeping, you take his phone and change the autocorrect options so that every time he types "you" or "u" it gets changed to "your sister."
# Write a function called autocorrect that takes a string and replaces all instances of "you" or "u" (not case sensitive) with "your sister" (always lower case).
# Return the resulting string.
# Here's the slightly tricky part: These are text messages, so there are different forms of "you" and "u".
# For the purposes of this kata, here's what you need to support:
# "youuuuu" with any number of u characters tacked onto the end
# "u" at the beginning, middle, or end of a string, but NOT part of a word
# "you" but NOT as part of another word like youtube or bayou
#-----
#My Solution:
def autocorrect(input)
words = input.split
words.map! do |word|
if word == "u"
"your sister"
elsif word[0..2].downcase == "you" && word[3..-1] == "u" * (word.length - 3) #replace "you", "youu", "youuu" - etc
"your sister"
elsif word[0..2].downcase == "you" && word.length == 4 && ( ",.?!".include? word[-1] ) #replace 'you' and keep end punctuation
last = word[-1]
"your sister" + last
else
word
end
end
words.join(" ")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment