Question: Exercise 1 Assume s is a string of characters. Write a program that counts the number of vowels contained in the string s . Valid

Exercise 1
Assume s is a string of characters. Write a program that counts the number of vowels contained in the string s. Valid vowels are both lower- and uppercase: 'a','e','i','o','u','A','E','I','O', and 'U'.
For example, if s = 'azcbobobegghAkl', your program should return 5.
# GRADED CELL: def countVowels(s): # Counts the number of vowels # input: string s of characters # output: number of vowels in s counter=0 # counts the number of vowels ### START CODE HERE ### ### END CODE HERE ### return counter
# Quick Check s1='azcbobobegghAkl' s2='mdorJKLbnajwyrh346$#%&aao7me' s3='hdgftygvetYqnjsvPOIciokjyly23MYOAsvx' t1= countVowels(s1) t2= countVowels(s2) t3= countVowels(s3) print('Output:',t1,',',t2,',',t3) print('Expected output: 5,6,7') if (t1,t2,t3)==(5,6,7): print("Congratulations, all tests passed successfully!") else: print("Sorry, try again!")
Exercise 2
Assume s is a string of characters. Write a program that outputs the number of times the string 'bob' in any combination of upper- and lowercase letters (i.e., 'bob', 'boB', 'bOb', 'bOB', 'Bob', 'BoB', 'BOb',and 'BOB') occurs in s.
# GRADED CELL: def countBob(s): # Counts the number of times 'bob' appears in s # input: string s # output: number of times 'bob' appears in s counter=0 # counts the number of vowels ### START CODE HERE ### ### END CODE HERE ### return counter
# Quick Check s1='azcbobobegghakl' s2='mBOBOBOBmnddbvbobnshbohetg' s3='bobmnfghbobOBbeyhrb2874bnbobob' t1= countBob(s1) t2= countBob(s2) t3= countBob(s3) print('Output:',t1,',',t2,',',t3) print('Expected output: 2,4,5') if (t1,t2,t3)==(2,4,5): print("Congratulations, all tests passed successfully!") else: print("Sorry, try again!")
Exercise 3
Assume s is a string of lowercase letters. Write a program that outputs the longest substring of s in which the letters occur in alphabetical order. If two or more such substrings are possible, return the most left one.
Example: s = 'abbampfxxaaa'. There are four longest substrings of s of length 3: 'abb', 'amp', 'fxx' and 'aaa'. Your code should return 'abb'.
Note: Lowercase letters are alphabetically ordered in Python, i.e. a < b< c <...< z.
# GRADED CELL: def longestAlphSubStr(s): # Returns the longest substring of s which is the in alphabetical order # input: string s composed of lowercase letters # output: longest substring of s which is the in alphabetical order longest = s[0] # stores the longest substring of s which is the in alphabetical order ### START CODE HERE ### ### END CODE HERE ### return longest
# Quick Check s1='azcbobobegghakl' s2='abbampfxxaaa' s3='bobmnfghbobbeqqrtxzyhrbbnbobob' t1= longestAlphSubStr(s1) t2= longestAlphSubStr(s2) t3= longestAlphSubStr(s3) print('Output:', t1,',',t2,',',t3) print('Expected output: '+'beggh, '+'abb, '+'bbeqqrtxz') if (t1,t2,t3)==('beggh','abb','bbeqqrtxz'): print("Congratulations, all tests passed successfully!") else: print("Sorry, try again!")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!