简体   繁体   中英

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. 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'. 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.

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.

>>> 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.

EDIT: Do remember that 0 is an integer and a first class citizen in python. 'foo' has 2 letters in python, not 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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