Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pig Latin - End of My Rope! - [\"a\"] #10

Open
ianalexh opened this issue May 16, 2013 · 2 comments
Open

Pig Latin - End of My Rope! - [\"a\"] #10

ianalexh opened this issue May 16, 2013 · 2 comments

Comments

@ianalexh
Copy link

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....

@alexch
Copy link
Owner

alexch commented May 16, 2013

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//issues/10
.

Alex Chaffee - [email protected]
http://alexchaffee.com
http://codelikethis.com
http://twitter.com/alexch

@ianalexh
Copy link
Author

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

joshwcomeau added a commit to joshwcomeau/learn_ruby that referenced this issue Apr 7, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants