Skip to content

Instantly share code, notes, and snippets.

@mshock mshock/pig_latin.rb
Created Mar 7, 2016

Embed
What would you like to do?
Pig Latin
def translate(str)
words = str.split(" ")
result_array = Array.new
words.each do |word|
if ( word.match(/^[aeiou]/) )
result_array.push( word + 'ay' )
else
done = false
consonants = ''
until(done)
working = word[0]
if (working == 'q')
consonants = consonants + 'qu'
word[0] = ''
word[0] = ''
break
end
if ( working.match(/^[aeiou]/) )
break
end
consonants = consonants + working
word[0] = ''
end
result_array.push( word + consonants + 'ay' )
end
end
result_array.join(' ')
end
# def translate(str)
# words = str.split(" ")
# vowels = ["a","e","i","o","u"]
# result_string = ""
# result_array = []
#
# words.each do |word|
# letters = word.split
# truncated_word = ""
# consonants = []
#
# i = 0
# unless vowels.include?(letters.first)
# consonants[0] = letters.first
#
# until vowels.include?(letters[i])
# consonants[i] = letters[i]
# i += 1
# end
#
# if consonants.last == 'q'
# consonants[i] = letters[i]
# i += 1
# end
# end
#
# truncated_word[i] = letters[i]
# i += 1
# while i < word.length
# truncated_word += letters[i]
# i += 1
# end
#
# translated_word = truncated_word + consonants + "ay"
# result_array << translated_word
# end
# result_string = result_array.join(" ")
# result_string
# end
translate("the quick brown fox")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.