Write a function named `freq(l)` that takes a list `l` of numbers
and returns a tuple of the most frequent number and its frequency.
For example, `freq([1,2,3,4,5,2,3,4,5,3,4,5,4,5,4]) returns (4,5)
because number 4 appears 5 times in this list. If multiple numbers
appear as most frequent, then the first element of the returned
tuple is a list of all those numbers. For example,

freq([1,2,3,1,2,1,2,3,1,2]) will return ([1,2], 4) because both
1 and 2 appear 4 times in the list.

Respuesta :

The function named `freq(l)` that takes a list `l` of numbers and returns a tuple of the most frequent number and its frequency is as follows;

def freq(l):

    s = { x for x in l }  

    lt = [ (x, l.count(x)) for x in s ]

    mn = max(lt,key=lambda item:item[1])[1]

    r = ([ x for (x,v) in lt if v == mn], mn)

    return r

print(freq([1,2,3,4,5,2,3,4,5,3,4,5,4,5,4]))

Code explanation;

The code is written in python.

  • we defined a function named "freq" and it accept an argument named l.
  • The second line we get the unique values using sets.
  • The variable "lt" is use to store list of tuple containing unique values and their occurrences in the list.
  • "mn" is a variable use to store the maximum number of occurrences.
  • Then, the variable r is use to store the resulting tuple, filtering out all the items with less occurrences than the maximum.
  • we returned the variable "r".
  • Then we call our function with the parameter.

learn more on python here: https://brainly.com/question/4503928

Ver imagen vintechnology