Menu

(Solved) : Consider Following Functions Def Getnumberofduplicates Data Item Returns Number Times Give Q34296435 . . .

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 duplicatesdef 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 ofstatements that are executed inside the respective function. Do notcount any additional statements that you add to the code when youmodify it.  

The statement :

result = get_number_of_duplicates(data, element)

should be counted as 1 statement. But you should also add thecount of any statements executed inside that function.

Each function should return a tuple, where the first value isthe return value of the original function (e.g., duplicates), andthe second value is the count of the number of statements executedin 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 ofstatements required to calculate the maximum duplicates.

For example:

TestResultresult, count = get_number_of_duplicates([1], 0)print(f’Duplicates: {result}, Statements: {count}’)Duplicates: 0, Statements: 5result, 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 thelist”””
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 thelist”””
max_duplicates = 0
for element in data:
result = get_number_of_duplicates(data, element)
if max_duplicates < result:
max_duplicates = result
return max_duplicates

Expert Answer


Answer to Consider Following Functions Def Getnumberofduplicates Data Item Returns Number Times Give Q34296435 . . .

OR