ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Ruby] Most Vowels
    coding for food 2022. 8. 6. 11:45

    Write a method most_vowels that takes in a sentence string and returns the word of the sentence that contains the most vowels.

    def most_vowels(sentence)
    
    end
    
    print most_vowels("what a wonderful life") #=> "wonderful"
    # solution
    
    def most_vowels(sentence)
      counts = {}
    
      sentence.split.each do |word|
        counts[word] = vowel_count(word)
      end
    
      sorted = counts.sort_by { |k,v| v}
      return sorted[-1][0]
    end
    
    def vowel_count(word)
      count = 0
      vowels = "aeiou"
    
      word.each_char do |char|
        if vowels.include?(char)
          count += 1
        end
      end
    
      return count
    end
    
    print most_vowels("what a wonderful life") #=> "wonderful"
Designed by Tistory.