简体   繁体   English

仅使用 if 语句查找字符串的第一个元音

[英]Find first vowel of a string only using if statements

I need to find the first vowel in a string.我需要找到字符串中的第一个元音。 My code passes a few cases but when it's tested for the 'asked' it fails because it returns position 3 instead of 0 since a is the first vowel.我的代码通过了一些情况,但是当它测试“询问”时它失败了,因为它返回位置 3 而不是 0,因为 a 是第一个元音。 Also in the string does not contain vowels it should return the length of the string.同样在字符串中不包含元音它应该返回字符串的长度。

The other thing I tried was: if 'a' in s then give me the position of a but that failed for the word 'eat'.我尝试的另一件事是:如果 s 中的 'a' 然后给我 a 的位置,但是对于 'eat' 这个词失败了。 I'm not sure how to proceed.我不知道如何继续。 I have pasted my code below:我在下面粘贴了我的代码:

import introcs

def first_vowel(s):
    """
    Returns the position of the first vowel in s; it returns len(s) if there are no vowels.
    
    We define the vowels to be the letters 'a','e','i','o', and 'u'.  The letter
    'y' counts as a vowel only if it is not the first letter in the string.
    
    Examples: 
        first_vowel('hat') returns 1
        first_vowel('grrm') returns 4
        first_vowel('sky') returns 2
        first_vowel('year') returns 1
    
    Parameter s: the string to search
    Precondition: s is a nonempty string with only lowercase letters
    """
    result = len(s)
    c1 = introcs.count_str(s,'a')
    c2 = introcs.count_str(s,'e')
    c3 = introcs.count_str(s,'i')
    c4 = introcs.count_str(s,'o')
    c5 = introcs.count_str(s,'u')
    
    if (c1 or c2 or c3 or c4 or c5 )== -1:
        c1 == 0
        c2 == 0
        c3 == 0
        c4 == 0 
        c5 == 0

    sums = c1 + c2 + c3 + c4 + c5  
    
    if  0<sums<=1:
        pos_a = introcs.find_str(s,'a')
        result = pos_a
        
    if ('e' in s)  and (result == len(s)):
        pos_e = introcs.find_str(s,'e')
        result = pos_e
        
        
    return result

This looks a lot like homework here.这看起来很像这里的作业。

So I will point you in the the right direction instead of solving it for you.因此,我会为您指明正确的方向,而不是为您解决问题。

There is a string method that returns the lowest instance of a sub-string.有一个字符串方法返回子字符串的最低实例。 Called index.称为索引。 When you create a variable of the type string or str, you have some built-in methods available to you.当您创建 string 或 str 类型的变量时,您可以使用一些内置方法。

Perhaps you can use this string method within your if statements... with this you can come up with some if statements that work for you.也许你可以在你的 if 语句中使用这个字符串方法......通过这个你可以想出一些适合你的 if 语句。

>>> s = "red bat"
>>> s.index('a')
>>> 5
>>> if s.index('a') is 5:
...    print('yeet') 
>>> yeet

In this way you learn about this string method, which is already available to you by default in python as a built in for an object of class 'str', that can be used to easily return the first instance of the sub-string as an integer.通过这种方式,您可以了解这个字符串方法,默认情况下,您已经可以在 python 中使用它作为类 'str' 的对象的内置方法,它可以用来轻松地将子字符串的第一个实例作为整数。

EDIT: Do remember that 0 is an integer and a first class citizen in python.编辑:请记住 0 是一个整数,并且是 python 中的一等公民。 'foo' has 2 letters in python, not 3. [0,1,2] 'foo' 在 python 中有 2 个字母,而不是 3 个。 [0,1,2]

first_vowel=len(word)
current=None

for vowel in vowels:
    splitted_word=[]
    for i in word:
        splitted_word.append(i)
    
    try:
        current=splitted_word.index(vowel)
    except ValueError:
        continue

    if current<first_vowel:
        first_vowel=current

I tried it with this code:我用这个代码试了一下:

vowels=["a","e","i","o","u","y"]
words=["sky","alpha","test","ntd"]

for word in words:
    first_vowel=len(word)
    current=None

    for vowel in vowels:
        splitted_word=[]
        for i in word:
            splitted_word.append(i)
        
        try:
            current=splitted_word.index(vowel)
        except ValueError:
            continue

        if current<first_vowel:
            first_vowel=current

    print(f"{word} first wovel in position {first_vowel}")

output:输出:

sky first wovel in position 2
alpha first wovel in position 0
test first wovel in position 1
ntd first wovel in position 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM