Question: Consider the following functions: def get_number_of_duplicates(data, item): Returns the number of times a given item appears in the list duplicates = 0 for element in
Consider the following functions:
def get_number_of_duplicates(data, item): """Returns the number of times a given item appears in the list""" duplicates = 0 for element in data: if item == element: duplicates += 1 return duplicates def max_duplicates_in_list(data): """Returns the maximum number of items that are repeated in the list""" max_duplicates = 0 for element in data: result = get_number_of_duplicates(data, element) if max_duplicates < result: max_duplicates = result return max_duplicates
Modify the functions so they both *return* the total number of statements that are executed inside the respective function. Do not count any additional statements that you add to the code when you modify it.
The statement :
result = get_number_of_duplicates(data, element)
should be counted as 1 statement. But you should also add the count of any statements executed inside that function.
Each function should return a tuple, where the first value is the return value of the original function (e.g., duplicates), and the second value is the count of the number of statements executed in that function. You could call the function using:
maximum, count = max_duplicates_in_list([1, 1, 2, 2, 2, 3, 4, 4]): print(maximum) print(count)
where count is an integer value representing the total number of statements required to calculate the maximum duplicates.
For example:
| Test | Result |
|---|---|
result, count = get_number_of_duplicates([1], 0) print(f'Duplicates: {result}, Statements: {count}') | Duplicates: 0, Statements: 5 |
result, count = max_duplicates_in_list([1]) print(f'Duplicates: {result}, Statements: {count}') | Duplicates: 1, Statements: 13 |
the function:
def get_number_of_duplicates(data, item): """Returns the number of times a given item appears in the list""" duplicates = 0 for element in data: if item == element: duplicates += 1 return duplicates
def max_duplicates_in_list(data): """Returns the maximum number of items that are repeated in the list""" max_duplicates = 0 for element in data: result = get_number_of_duplicates(data, element) if max_duplicates < result: max_duplicates = result return max_duplicates
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
