ALL IN PYTHON d3 Write a function called nonvowels that take

ALL IN PYTHON: (d.3) Write a function called nonvowels that takes a string and returns a string containing all the characters in the parameter string that are not vowels. Since you\'re returning a string instead of printing a character at a time, you\'ll need to construct that string in your function. Start with a variable (let\'s call it result) whose value is the empty string. Each time you find a nonvowel, add it to the end of result. Then, once you\'ve gone through the whole parameter, result is what you return. [We just gave you the algorithm; your job is to implement it in Python.] Functions that return values are somewhat more convenient to test than functions that print values. You could do it the same way, printing out the tests and the results or printing out boolean expresions that are true if the test passes. Instead of print statements, though, you should use assert statements as shown below. Their advantage is that if the test passes, nothing happens, so you can leave the assert statements in your code after you\'re satisfied your function works, without their cluttering up your output. (It\'s good to keep the assert statements there because later you may make changes that \"break\" your existing code, or you may need to change your existing code, and with the tests still in place you\'ll find any problems at the earliest possible time.) Here are some assert statements for double and for is_vowel; note that the the assert statement expects a boolean expression., and that you\'ll have to define double if you actually want to run the assertions that call double. assert double(0) == 0 assert double(17.5) == 35 assert double(-223344) == -446688 assert is_vowel(\'a\') assert is_vowel(\'U\') assert not is_vowel(\'X\') assert not is_vowel(\'?\') To see what happens when a test fails, assert something false like assert double(2)==5. (Note that when an assertion fails, there could be two reasons: (i) Your function may be incorrect, or (ii) your assertion—what you think the right answer is—may be wrong. You should consider both possibilities). Write some tests for nonvowels using assert statements. (d.4) Write a function called consonants that takes a string and returns a string containing all the letters in the parameter string that are not vowels. (This is not the same as nonvowels, whose definition refers to \"characters,\" which include digits and spaces and punctuation. Did you test nonvowels with strings including non-letters? If not, go back and do it, changing the function\'s definition if necessary to make it work correctly.) Before you write the body of the function, follow the \"design recipe\" steps: specify the types of the parameter and return value; include a short purpose statement as a docstring; write examples in the form of assert statements. (d.5) Write a function called select_letters that takes two parameters, both strings, and returns a string. If the first parameter is \'v\', it returns a string containing all the vowels in the second parameter; if the first parameter is \'c\', it returns a string containing all the consonants in the second parameter. If the first parameter is anything else, it returns the empty string. So, select_letters(\'v\', \'facetiously\') would return aeiou and select_letters(\'c\', \'facetiously\') would return fctsly. [If you count Y as a vowel, your results will be slightly different.] (d.6) Write a function called hide_vowels that takes a string and returns a string in which every vowel in the parameter is replaced with a hyphen (\"-\") and all other characters remain unchanged. After your testing shows that it\'s correct, try running it with a couple of sentences; you may be able to understand the sentences even with all the vowels hidden.

Solution

print() print() vowel = \'aeiouAEIOU\' def is_vowel(x:str) -> bool: \'\'\'tells whether the one-character string is a vowel or not \'\'\' return x in vowel \"\"\" def is_vowel(x:str) -> bool: \'\'\'tells whether the one-character string is a vowel or not \'\'\' if x in vowel: return True else: return False return \"\"\" print(is_vowel(\'d\')) print(is_vowel(\'A\')) print() def print_nonvowels(x:str) -> None: \'\'\'prints all nonvowels in a string \'\'\' for b in x: if is_vowel(b) == False: print(b) return print_nonvowels(\'iobaqerfaew\') print_nonvowels(\'ABCDEFGHIJKLMNOPqrstuvwxyz\') print() def nonvowels(x:str) -> str: \'\'\'prints out all nonvowels, including spaces and punctuation marks \'\'\' result = \'\' for b in x: if is_vowel(b) == False: result = result + b return result assert nonvowels(\'bac\') == \'bc\' assert nonvowels(\'bacBOIC\') == \'bcBC\' assert nonvowels(\'work ing?\') == \'wrk ng?\' #append doesn\'t work for strings print(nonvowels(\'aouiberawerfa\')) print(nonvowels(\'.aewf?\')) print() consonant = \'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ\' def consonants(x:str) -> str: \'\'\'prints all consonants in a string, meaning no spaces or punctuation marks \'\'\' result = \'\' for b in x: if b in consonant: result = result + b return result assert consonants(\'abcd\') == \'bcd\' assert consonants(\'?abjJ.UI D\') == \'bjJD\' print(consonants(\'.[]noibguasdofubD\')) print() def select_letters(x:str, y:str) -> str: \'\'\'returns vowels in string y if x is v or returns consonants in string y if x is c\'\'\' result = \'\' if x == \'c\': for b in y: if b in consonant: result = result + b if x == \'v\': for b in y: if b in vowel: result = result + b return result assert select_letters(\'c\', \'facetious\') == \'fcts\' assert select_letters(\'v\', \'facetious\') == \'aeiou\' print(select_letters(\'c\', \'CharlesYoon\'), select_letters(\'v\', \'CharlesYoon\')) print() def hide_vowels(x:str) -> str: \'\'\'replaces everty vowel in string x with a hyphen \'-\' \'\'\' result = \'\' for b in x: if b in vowel: b = \'-\' result = result + b else: result = result + b return result assert hide_vowels(\'rat\') == \'r-t\' assert hide_vowels(\'tkinter is strange!\') == \'tk-nt-r -s str-ng-!\' print(hide_vowels(\'ICS31 is fun.\'))
ALL IN PYTHON: (d.3) Write a function called nonvowels that takes a string and returns a string containing all the characters in the parameter string that are n

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site