【每日一题1128】元音统计

给定一个纯小写字母的字符串,请统计出其中元音字母(a, e, i, o, u)出现的个数。

示例:
输入:aeiou,输出:5
输入:abracadabra,输出:5

题目难度:简单
题目来源:Vowel Count

def solution(words: str) -> int:
    pass

assert solution("aeiou") == 5
assert solution("abracadabra") == 5
assert solution("y") == 0
public long solution(String s){
        String[] strings = {"a", "e", "i", "o", "u"};
        return Arrays.stream(s.split("")).filter(item -> Arrays.asList(strings).contains(item)).count();
    }
def solution(words: str) -> int:
    return sum(1 for c in words if c in 'aeiou')

def count_numbers_vowels(args):
a = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
st= (args)
count = 0
for s in st:
if s in a:
count += 1
print(“这个字符串出现了%s 个元音字母”% count)

def solution(words: str) -> int:
    return len([None for w in words if w in 'aeiou'])


assert solution("aeiou") == 5
assert solution("abracadabra") == 5
assert solution("y") == 0
def solution(words: str) -> int:
    return len([i for i in words if i in ['a','e','i','o','u']])

assert solution("aeiou") == 5
assert solution("abracadabra") == 5
assert solution("y") == 0
def solution(words: str) -> int:
    tmp_arr = ['a', 'e', 'i', 'o', 'u']
    count = 0
    for char in words:
        if char in tmp_arr:
            count += 1
    return count
            

assert solution("aeiou") == 5
assert solution("abracadabra") == 5
assert solution("y") == 0
public class rampUp {
    public static void main(String[] args) {
        String str = "abc";
        rampUp test = new rampUp();
        System.out.println(test.solution("abc"));
    }
    public int solution(String words){
       String str = "aeoiu";
       int count = 0;
        for (int i=0; i < words.length(); i++) {
            if (str.indexOf(words.charAt(i)) >= 0) {
                count++;
            }
        }
        return count;
    }
}

哥哥,你这个好像没有返回东西阿。。。

sum(1 for c in words if c in 'aeiou')

这个就是返回值,是

sum([1 for c in words if c in 'aeiou'])

的缩略写法

sum函数:比如sum([1,2,3]) 会计算数组所有数字的和 ,结果是6
所以答案类似于 sum([1,1,1,1,1,1]) ,而计算出来的数字正好就是统计的数字了

def solution(words: str) -> int:
    vowel_list = ['a', 'e', 'i', 'o', 'u']
    # vowel_match = []
    # for i in words:
    #     if i in vowel_list:
    #         vowel_match.append(i)
    vowel_match = [i for i in words if i in vowel_list]
    return len(vowel_match)


assert solution("aeiou") == 5
assert solution("abracadabra") == 5
assert solution("y") == 0
def solution(words: str) -> int:
    str1 = 'aeiou'
    count = 0
    for i in words:
        for h in str1:
            if i == h:
                count += 1
                break
    return count


if __name__ == '__main__':
    assert solution("aeiou") == 5
    assert solution("abracadabra") == 5
    assert solution("y") == 0

好的吧,我表示这种一行的,对我这种菜鸡来说,有点吃力,或者看不懂哈哈

def solution(words: str) -> int:
    return words.count('a') + words.count('e') + words.count('i') + words.count('o') + words.count('u')


assert solution("aeiou") == 5
assert solution("abracadabra") == 5
assert solution("y") == 0

关闭