Question
Problem 14 [1 point autograded ] Use the Design Recipe to write a function, reverse_string that consumes a string and returns a string in reversed
Problem 14 [1 point autograded ]
Use the Design Recipe to write a function, reverse_string that consumes a string and returns a string in reversed order.
For example:
Test | Result |
---|---|
print(reverse_string('hello')) | 'olleh' |
Write 3 assertEqual statements to test your function.
-------------------------------------------------------------------------
Problem 15 [ 1 point autograded]
Use the design recipe to write a function locate_W, which consumes a string and returns the last index value of 'W' in a string.
For example:
Test | Result |
---|---|
print(locate_W('Where are you, Will?')) | 15 |
Write 3 assertEqual statements to test your function.
-----------------------------------------------------------------------
Problem 16 [1 point autograded ]
Use the Design Recipe to write a function count_W, which consumes a string and returns the number of times the character 'W' appeared in the string.
For example:
Test | Result |
---|---|
print(count_W('Where are you, Will?')) | 2 |
Write 3 assertEqual statements to test your function.
-----------------------------------------------------------------------------
Problem 17 [1 point autograded ]
Use the Design Recipe to write a function cut_string, which consumes a string. cut_string should cut the string into two parts the first time it encounters the character 'r'. Your function should return a list of the two strings, neither should contain that first 'r'.
Note: If there is no 'r', cut_string should return a list containing only the original string.
For example:
Test | Result |
---|---|
print(cut_string('how are you today?')) | ['how a', 'e you today?'] |
print(cut_string('hello')) | ['hello'] |
Write 3 assertEqual statements to test your function.
----------------------------------------------------------------------------------
Problem 18 [1 point autograded]
Use the Design Recipe to write a function interleave, which consumes two strings of the same length. Your function should return a string with 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.
For example:
Test | Result |
---|---|
print(interleave('hat','cat')) | 'hcaatt' |
Write 3 assertEqual statements to test your function.
-----------------------------------------------------------------------------------
Problem 19 [1 point autograded ]
Use the Design Recipe to write a function count_vowels, which consumes a string and returns the number of vowels in that string. For these purposes, the vowels are a, e, i, o, and u, but never y.
Note: Count both upper and lowercase vowels!
Write 3 assertEqual statements to test your function.
---------------------------------------------------------------------------------------
Problem 20 [1 point autograded ]
Use the Design Recipe to write a function bools_2_str, which consumes a list of booleans and returns a string of the same length as the list. Where the list has True, the string should have 'X', and where the list has False, the string should have ' '(i.e., a single space).
For example:
Test | Result |
---|---|
print(bools_2_str([True,False,True,False,True])) | 'X X X' |
Write 3 assertEqual statements to test your function.
----------------------------------------------------------------------------------------
Problem 21 [1 point autograded ]
Use the Design Recipe to write a function has_duplicates, which consumes a list and returns True if the list has any duplicate entries, False otherwise. Your function should return False if the list is empty.
For example:
Test | Result |
---|---|
print(has_duplicates([1,2,3,1,5])) | True |
Write 3 assertEqual statements to test your function.
-------------------------------------------------------------------------------------------
Problem 22 [1 point autograded ]
Use the Design Recipe to write a function mask_encode, which consumes a list of booleans and a string and returns the characters from the string whose position is True in the list of booleans. It is possible that the list of booleans is longer than the string.
Write 3 assertEqual statements to test your function.
-------------------------------------------------------------------------
Problem 23 [1 point autograded ] - NESTED LISTS
Use the Design Recipe to write a function count_evens_NxN, that consumes a nested list representing a matrix of size NxN. The function should return the number of even numbers in the matrix. For this function, 0 is considered an even number.
Note: You may assume the list argument passed to the function is a nested list of integers.
Write 3 assertEqual statements to test your function.
-------------------------------------------------------------------------
Problem 24 [ 1 point autograded] - NESTED LISTS
Use the Design Recipe to write a function diagonal_diff, that consumes an NxN matrix of numbers (nested list or list of lists) and returns the absolute difference between the sum of its diagonals.
Consider the following example matrix:
[ [11,2,4], [4, 5,6], [10,8,-12] ]
The primary diagonal is:
11 5 -12
Sum across the primary diagonal: 11 + 5 - 12 = 4
The secondary diagonal is:
4 5 10
Sum across the secondary diagonal: 4 + 5 + 10 = 19
Difference: |4 - 19| = 15
Hint: You can do this with a single loop.
For example:
Test | Result |
---|---|
matrix1 = [ [11,2,4],[4,5,6],[10,8,-12] ] print(diagonal_diff(matrix1)) | 15 |
Write 3 assertEqual statements to test your function.
-------------------------------------------------------------------------------------------
Problem 25 [1 point autograded ] - RECURSION
Use the Design Recipe to define a modified version of round_list from Lab 5. The function will consume that same arguments (a list, and a positive integer), but the list argument may contain any kind of data (including, in particular, other lists). The function should return None, and modify the provided list in-place.
Values of the list which are of float type should be rounded to a number of decimals according to the second parameter of the function. Items which are of list type should be passed recursively to round_list for processing. All other types should be left unchanged.
Input testing: if the first parameter is not a list, or the second parameter is not a positive integer, the function should leave the parameters unchanged (and still return None).
Write 3 assertEqual statements to test your function.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started