Thunda
Thunda

Reputation: 13

How to I get the right output when there is more than vowel in each word? My code only works with one vowel in each word

Aba is a German children’s game where secret messages are exchanged. In Aba, after every vowel we add “b” and add that same vowel. Write a method aba_translate that takes in a sentence string and returns a new sentence representing its Aba translation. Capitalized words of the original sentence should be properly capitalized in the new sentence.

aba_translate(“Cats and dogs”) #=> “Cabats aband dobogs”

aba_translate(“Everyone can code”) #=> “Ebeveryobonebe caban cobodebe”

aba_translate(“Africa is Africa in German”) #=> “Abafribicaba ibis Abafribicaba ibin
Gebermaban”

My code:

def aba_translate(sentence)

  translation = []
  words = sentence.split(" ")
  vowels = "aeiou"
  vowel = ""
  before = ""
  after = ""
  full = ""
  
  words.each do |word|

    word.each_char.with_index do |char, idx|

      if vowels.include?(char)
          vowel = char
          before = word[0...idx]
          after = word[idx+1..-1]

          full = before + vowel + "b" + vowel + after
          translation << full
      end

    end

  end

  return translation.join(" ")

end

puts aba_translate("Cats and dogs")
puts aba_translate("Everyone can code") 
puts aba_translate("Africa is Africa in German")

Upvotes: 1

Views: 340

Answers (4)

itskoyii
itskoyii

Reputation: 21

Here's my beginner-friendly solution

def aba_translate(sent)
vowels = "AEIOUaeiou"

aba_sent = ""
sent.each_char do |char|
    if vowels.include?(char)
        aba_sent += char + "b" + char.downcase
    else 
        aba_sent += char
    end
end

return aba_sent

end

Upvotes: 0

KennySchl
KennySchl

Reputation: 61

I think the nested loops complicates it since you can solve the problem with just one loop. Here you just need to initialize a string and a string of vowels to check each character. While you iterate through each character you shovel it into the empty string, and then you check if it is a vowel you shovel b + that vowel's lowercase version to account for the uppercase instances. Then you finally return the new string.

def aba_translate(string)

    new_string = ""
    vowels = "AEIOUaeiou"

    string.each_char do |char|
       new_string << char
       if vowels.include?(char)
       new_string << "b" + char.downcase
       end
    end
return new_string
end

Upvotes: 0

J&#246;rg W Mittag
J&#246;rg W Mittag

Reputation: 369468

Every time you find a vowel, you take the entire string before the vowel and the entire string after the vowel and add it to the result.

So, for a word like "code", that means you first produce the output c + obo + de and then the output cod + ebe.

However, what you actually need to do is simply keep the part you have processed instead of duplicating it.

You can do this by either changing your logic to keep track of up to which index you have already processed the word, or alternatively by processing it character-by-character instead of chunk-by-chunk.

However for problems like this, Regex are usually a much better solution:

VOWELS = 'aeiou'

def aba_translate(sentence)
  sentence.gsub(Regexp.union(*VOWELS.chars), '\0b\0')
end

or just making VOWELS a Regexp in the first place:

VOWELS = /[aeiou]/.freeze

def aba_translate(sentence)
  sentence.gsub(VOWELS, '\0b\0')
end

Upvotes: 0

rohit89
rohit89

Reputation: 5773

Your code generates a whole new word every time it sees a vowel. Instead you need to build each word character by character and make changes when it sees a vowel.

def aba_translate(sentence)

  translation = []
  words = sentence.split(" ")
  vowels = "aeiouAEIOU"

  words.each do |word|
    full = ""
    word.each_char.with_index do |char, idx|
      full += char
      if vowels.include?(char)
          full = full + "b" + char.downcase
      end
    end
    translation << full
  end

  return translation.join(" ")
end

Upvotes: 1

Related Questions