Question

Write a function count_vowels(s) that takes a string as an argument and returns the number of...

Write a function count_vowels(s) that takes a string as an argument and returns the number of vowels ('a', 'e', 'i' 'o', 'u') in the string. Should you use a for or while loop? (Implement this as a function, not as a class with a method.) Be sure to include unittest test cases to demonstrate that your code works properly, e.g Part 2: last_occurance(target, sequence) Write a function last_occurance(target, sequence) that takes two arguments: 1. target: A target item to find 2. sequence: A list. Your function should return the index (offset from 0) of the last occurrence of the target item or None if the target is not found. E.g. the last occurrence of 33 is at offset 3 in the list [ 42, 33, 21, 33 ] because 42 is offset 0, the first 33 is at offset 1, 21 is offset 2, and the last 33 is offset 3. Use unittest to test your function with several different target values and lists. Next, test your function with a character as the target, and a string as the list, e.g. find('p', 'apple'). What should happen? Be sure to use unittest to demonstrate that your code works properly. You should have the following assumption: 1. If type(sequence) in (list, tuple), the data type of elements in sequence will be the same. And each element’s data type will be the same as target. 2. If type(sequence) is str, the target will be a single charactre. A substr whose length greater than 1 is not requried to be considered.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def count_vowels(s):#method to count and return the number of vowels in a given string
c=0#to store count
for i in s:
if(i=='A' or i=='a' or i=='E' or i=='e' or i=='I' or i=='i' or i=='O' or i=='o' or i=='U' or i=='u'):
c+=1
return c#returning count
def last_occurance(target, sequence):
index=-1#to store the index value
k=0
for i in sequence:
if i==target:#if found then updating index
index=k
k+=1
return index
#testing count_vowels method
s="Hello"
print("Vowels in "+s+":",count_vowels(s))
s="Omg1"
print("Vowels in "+s+":",count_vowels(s))
s="123"
print("Vowels in "+s+":",count_vowels(s))
#testing last_occurance method
#checking with list
t=2
l=[2,1,4,2,5]
print("Last occurence of ",t," in ",l," at index:",last_occurance(t,l))
t=2
l=[1,45]
print("Last occurence of ",t," in ",l," at index:",last_occurance(t,l))
#checking with string
t='l'
l='Hello'
print("Last occurence of ",t," in ",l," at index:",last_occurance(t,l))
t='4'
l='Hello'
print("Last occurence of ",t," in ",l," at index:",last_occurance(t,l))
#checking with tuple
t=2
l=(2,1,4,2,5)
print("Last occurence of ",t," in ",l," at index:",last_occurance(t,l))
t=2
l=(1,45)
print("Last occurence of ",t," in ",l," at index:",last_occurance(t,l))

output:

Vowels in Hello: 2 Vowels in Omgi: 1 Vowels in 123: 0 Last occurence of 2 in 12, 1, 4, 2, 5] at index: 3 Last occurence of 2

3 main.py 1. def count vowels(s) : #method to count and return the number of vowels in a given string C=8#to store count for

Add a comment
Know the answer?
Add Answer to:
Write a function count_vowels(s) that takes a string as an argument and returns the number of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT