Skip to content

Instantly share code, notes, and snippets.

@nbeers22
Last active August 29, 2015 14:06
Show Gist options
  • Save nbeers22/8702f4b60bdb2dc586a4 to your computer and use it in GitHub Desktop.
Save nbeers22/8702f4b60bdb2dc586a4 to your computer and use it in GitHub Desktop.
Solution for Pig-latin
# Solution for Challenge: Pig-latin. Started 2014-09-16T15:13:49+00:00
# Script: CONVERT TO PIG LATIN
# Iteration One: CONVERT SINGLE WORD
# GET a word from user input.
# IF the word starts with a vowel, don't change it.
# ELSE replace the word with its pig latin equivalent.
# GET all of the consonants before the first vowel in the word.
# SET the consonants at the end of the word and add the suffix "ay."
# ENDIF
# PRINT the pig-latin-ified word.
def convert_single_word(string)
string = string.chars
string if string[0].match(/[aeiou]/)
string.rotate.push("a","y").join if string[0].match(/[^aeiou]/)
end
p convert_single_word("hello")
# =>
# Iteration Two: CONVERT COMPLETE SENTENCE
# GET a sentence from user input.
# FOR each word in the sentence.
# CONVERT SINGLE WORD
# ENDFOR
# PRINT the converted sentence.
# DISPLAY the number of words converted.
def convert_complete_sentence(sentence)
sentence.split.map {|word|
word if sentence[word].match(/[aeiou]/)
if sentence[word].match(/[^aeiou]/)
word.chars.rotate.push("a","y")
end
}
end
p convert_complete_sentence("hello there man")
# =>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment