-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Description
https://github.com/alexch/learn_ruby/tree/master/04_pig_latin
All right, 6-8 hours over 3 days on this one.... could use a clue...
I feel I'm very close. The idea is to split the string into words, and then split the words into letters. Find the first vowel in each letter array - with a special case if inculde?("q")
repl.it appears to give me the correct return, but rspec does not.
Example from rspec:
expected: "appleay"
got: "["a", "p", "p", "l", "e"][]ay" (using ==)
Any easy way to turn "a", "p"\ into "ap"? array.join and array.flatten don't do it.
My code - which I'm pretty proud of after all this time:
def translate(string)
string.downcase!
split_array = string.split(" ")
split_array_final_composite = []
split_array_letter_end = []
split_array.each do |word|
if word.include?("q")
#here I'm trying to get an index for the first vowel. Has to be a better way, but it works!
string_copy = word.clone
string_copy[/[aeio]/] = ''
string_index = string_copy.index("")
split_array_letter = word.split("")
split_array_letter_end = split_array_letter.shift(string_index)
split_array_final_composite<<"#{split_array_letter}#{split_array_letter_end}ay"
else
#here I'm trying to get an index for the first vowel. Has to be a better way, but it works!
string_copy = word.clone
string_copy[/[aeiou]/] = '*'
string_index = string_copy.index("*")
split_array_letter = word.split("")
split_array_letter_end = split_array_letter.shift(string_index)
split_array_final_composite<<"#{split_array_letter}#{split_array_letter_end}ay"
end
end
split_array_final_composite.flatten #this is un-necessary
return split_array_final_composite.join(" ")
end
translate("the quick red fox jumps over the lazy dog")
repl.it returns:
=> "ethay ickquay edray oxfay umpsjay overay ethay azylay ogday"
** It just now occurs to me that perhaps I don't have to split into letters any more, since now I've found a different way to find that vowel index, which was the original purpose of splitting into letters..... I'll try again without that step and see what happens.....
** No, that's right, I did need to split into letters because "shift" works for entire array elements, not for each contstituent part of the element.... still stumped....