Skip to content

Instantly share code, notes, and snippets.

@mshock
Created March 7, 2016 08:18
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 mshock/a8f3737c20c725780455 to your computer and use it in GitHub Desktop.
Save mshock/a8f3737c20c725780455 to your computer and use it in GitHub Desktop.
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