Skip to content

Instantly share code, notes, and snippets.

@bootcoder
Created April 13, 2016 04:15
Show Gist options
  • Select an option

  • Save bootcoder/91284e64ffdaed5ca05df92c9a2cbf9b to your computer and use it in GitHub Desktop.

Select an option

Save bootcoder/91284e64ffdaed5ca05df92c9a2cbf9b to your computer and use it in GitHub Desktop.
Ruby solution for pig latin demonstrating iteratation and regular expressions.
def regex_solution(word)
match = word.match(/[aeiou]/)
return word unless match
pre = match.pre_match
post = match.post_match
return match.to_s + post + pre + "ay"
end
def regex_grouping_solution(word)
result = word.split(" ").map do |input_word|
input_word.gsub!(/^([^aeiou]*)(.*)/,'\2\1')
end
result[0] == word ? word : result[0] + "ay"
end
def iterative_solution(word)
word_array = word.split("")
vowels = ["a","e","i","o","u"]
result = []
word_array.each_with_index do |letter, index|
if vowels.include? letter
result = word_array[index..-1] + word_array[0..index-1] + ["ay"]
return result.join("")
end
end
return word
end
def convert_word_to_pig_latin(word)
return regex_solution(word)
# return regex_grouping_solution(word)
# return iterative_solution(word)
end
# p convert_word_to_pig_latin("banana")
p convert_word_to_pig_latin("banana") == "ananabay"
# p convert_word_to_pig_latin("strotch")
p convert_word_to_pig_latin("strotch") == "otchstray"
p convert_word_to_pig_latin("klck")
p convert_word_to_pig_latin("klck") == "klck"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment