1.Use the Design Recipe to write a function countW , which takes in a string as
ID: 3597885 • Letter: 1
Question
1.Use the Design Recipe to write a function countW, which takes in a string as an argument that counts the number of occurrence of the character "W" in the given string.
2.Use the Design Recipe to define a function count_vowels that returns the number of vowels in a given string argument. For these purposes, the vowels are a, e, i, o, and u, but never y. Note: count both upper and lower-case vowels!
3.Use the Design Recipe to write a function called largest_digit which consumes a string and returns the largest digit in the string as an int or None if there are no digits in the string. None is a keyword and type that can be returned (much like True or False).
Hint: You will need to check to see if a character is a numerical digit...try looking here: https://docs.python.org/3/library/stdtypes.html#string-methods
4.
Use the Design Recipe to define a function interleave that takes two strings as arguments. Return a string which is the two arguments interleaved together, i.e., the first character of the first string followed by the first character of the second string followed by the second character of the first string, etc.
Note: the arguments need not be of the same length! If one runs out, then append the rest of the other argument to the returned value.
5.A list called theLst has six string values. Append "hi" to the first value in the list.
Explanation / Answer
############ 1 ########### def countW(str): count=0 for i in str: if i=='W': count+=1 return count ############ 2 ########### def count_vowels(str): vowels=['a','e','i','o','u'] count=0 for i in str: i=i.lower() if i in vowels: count+=1 return count ############ 3 ########### def largest_digit(str): largest=-1 for i in str: if i.isdigit(): if int(i)>largest: largest=int(i) if largest==-1: return None else: return largest ############ 4 ########### def interleave(str1,str2): length_minimum=min(len(str1),len(str2)) interleavedString='' for i in range(length_minimum): a=str1[i] b=str2[i] interleavedString+=a+b if len(str1)==length_minimum: interleavedString+=str2[length_minimum:] else: interleavedString+=str1[length_minimum:] return interleavedString ############ 5 ########### theLst=['people','ok','no','fine','tt','sd'] theLst[0]='hi'+theLst[0]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.