learn_ruby
learn_ruby copied to clipboard
Pig Latin - End of My Rope! - [\"a\"]
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....
not sure but it looks like you're interpolating an array:
"#{split_array_letter}"
where split_array_letter is an array will make a string containing brackets and such -- maybe you should join it
You can look for others' solutions here:
https://github.com/ultrasaurus/test-first-teaching/tree/master/learn_ruby/pig_latin/solution
On Thu, May 16, 2013 at 11:08 AM, ianalexh [email protected] wrote:
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.....
— Reply to this email directly or view it on GitHubhttps://github.com/alexch/learn_ruby/issues/10 .
Alex Chaffee - [email protected] http://alexchaffee.com http://codelikethis.com http://twitter.com/alexch
Yes, that's it!
Replace split_array_final_composite<<"#{split_array_letter}#{split_array_letter_end}ay"
With split_array_joining = split_array_letter.join("")+split_array_letter_end.join("") +"ay" split_array_final_composite<<split_array_joining
...and it works. And I can see why.
NOW I can look at others' solutions! Had to figure it out on my own first....
I'm sure in six months or a year I'll be looking at my own solutions and LAUGHING - same way I laugh at the way I used to use Photoshop when I first started.
Thanks so much!
- ian