나는 이렇게 학습한다/Algorithm & SQL

Vowel Count

daco2020 2022. 4. 22. 23:57
반응형

Description:

Return the number (count) of vowels in the given string.

We will consider a, e, i, o, u as vowels for this Kata (but not y).

The input string will only consist of lower case letters and/or spaces.

 

 

Solution:

1. Check whether the characters in the 'sentence' are included in 'aeiou'.
2. Returns the number of included characters.

 

def get_count(sentence):
    return sum([i in 'aeiou' for i in sentence])

 

 

It is more difficult to solve with a dictionary.

import collections

def get_count(sentence):
    dict = collections.Counter(sentence)
    return sum([dict.get(i, 0) for i in "aeiou"])

Using collentions.Counter to create a dictionary of the number of characters in the list,
Returns the sum of the number of 'aeiou' values ​​among the dictionary keys.

 

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

The Office I - Outed  (0) 2022.04.24
Shortest Word  (0) 2022.04.23
String ends with?  (0) 2022.04.21
Remove the minimum  (0) 2022.04.20
Sum of two lowest positive integers  (0) 2022.04.19