3

For my code, I have to make a function that counts the number of vowels in the odd positions of a string.

For example, the following will produce an output of 2.

st = "xxaeixxAU"
res = countVowelsOdd(st)
print (res)

For my code, the only problem I have is figuring out how to tell python to count the vowels in the ODD positions.

This is found in the second part of the "if statement" in my code where I tried to make the index odd by putting st[i] %2 == 1. I get all types of errors trying to fix this.

Any idea how to resolve this?

def countVowelsOdd(st):
    vowels = "aeiouAEIOU"
    count = 0
    for i, ch in enumerate(st):
        if i in vowels and st[i] % 2 == 1:
            count += 1
    return count
Max Voitko
  • 1,542
  • 1
  • 17
  • 32

4 Answers4

1
if i in vowels ...

i is the index, you want the letter

if ch in vowels ...

and then since you have the index, that is what you find the modulo on

if ch in vowels and i % 2 == 1:
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • No I already completed the part where it counts vowels anywhere spotted in the string. I need help with the second part which is counting the vowels in odd positions of the string. –  Nov 07 '19 at 13:57
  • because `st[i]` is a letter within the string (the same letter as `ch`), you can't find the modulo of a letter – Sayse Nov 07 '19 at 14:01
  • So "i" on it's own means just any number/position of a string? –  Nov 07 '19 at 14:02
  • [What does enumerate mean?](https://stackoverflow.com/q/22171558/1324033) - i is the variable you've assigned to the index enumerate returns – Sayse Nov 07 '19 at 14:02
1

enumerate provides you first argument i as position.

def countVowelsOdd(st):
    vowels = "aeiouAEIOU"
    count = 0
    for i, ch in enumerate(st):
        if ch in vowels and i % 2 == 1:
            count += 1

    return count
Max Voitko
  • 1,542
  • 1
  • 17
  • 32
  • 1
    You want to check if `ch` is in `vowels`. `if ch in vowels`, not `if i in vowels`. – IMP1 Nov 07 '19 at 14:14
0

I don't know if your assignment/project precludes the use of regex, but if you are open to it, here is one option. We can first do a regex replacement to remove all even-positioned characters from the input. Then, do a second replacement to remove all non-vowel characters. Finally, what remains gives us correct vowel count.

st = "xxaeixxAU"
st = re.sub(r'(.).', '\\1', st)
print(st)
st = re.sub(r'[^aeiou]', '', st, flags=re.IGNORECASE)
print(len(st))

This prints:

xaixU
3
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Please, have a look at this

In [1]: a = '01234567'                                                                    

In [2]: print(*(c for c in a[0::2]))                                                       
0 2 4 6

In [3]: print(*(c for c in a[1::2]))                                                      
1 3 5 7

In [4]: print(*(c in '12345' for c in a[1::2]))                                           
True True True False

In [5]: print(sum(c in '12345' for c in a[1::2]))                                         
3

does it help with your problem?

gboffi
  • 22,939
  • 8
  • 54
  • 85