Array Coderbyte Data Structures and Algorithms Maths Python

[Coderbyte] – Mean Mode [Easy]

Let’s get back to Coderbyte again. This time we have the “Mean Mode” challenge. The challenge requirements are as follows:

Have the function MeanMode(arr) take the array of numbers stored in arr and return 1 if the mode equals the mean0 if they don’t equal each other (ie. [5, 3, 3, 3, 1] should return 1 because the mode (3) equals the mean (3)). The array will not be empty, will only contain positive integers, and will not contain more than one mode.

This solution makes use of several Python methods such as:

  • sum() – this function adds the elements of an iterable (e.g. list) and returns the sum
  • max() – this function returns the largest/biggest element from an iterable. The function also takes an optional key parameter that specifies the ordering value. For instance, in this solution, it checks the number of occurrences, and it returns the number that occurred the most, rather than the biggest number.
  • len() – when the object is a string, this function returns the number of characters in that string. When it is an object, it returns the number of elements in the object.

Let’s start solving this problem. The first thing is to find the mean. That is done by dividing the sum of the array by its length – sum(array)/length(array).

def MeanMode(arr): 
    mean = sum(arr)/len(arr)

Summing the array and finding its length could be done manually, without making use of any method. However, it is more convenient to use the built-in functions.

The next step is to find the mode from the given array. The max() function takes the given array as an argument, and it specifies the key to count the occurrences of each element from the array. That is, it returns the element that occurs the most in the list, rather than the biggest element. That is done by the line below:

    mode = max(arr, key=arr.count)

The last thing is to return one if the mean and the mode are equal. If they are not, it returns zero. The link to the repository where you can run the code – link. The full solution is as follows:

def MeanMode(arr): 
    mean = sum(arr)/len(arr)
    mode = max(arr, key=arr.count)
    if mean == mode: return 1
    else: return 0

The Coderbyte solution is below:

def MeanMode(arr): 

  # calculate the mean by dividing the sum by the
  # number of elements in the list
  mean = sum(arr) / len(arr)
  # the table that will contain each number as the key
  # and the number of times it occurs as the value
  # e.g. {"5": 1, "3": 3, "1": 1}
  table = {}
  # loop through each number in the list
  # see if this number already exists in table
  # if so add 1 to the count, if not set count = 1
  for i in range(0, len(arr)):
    number = arr[i]
    if number in table:
      table[number] += 1
      table[number] = 1
  # setup a new dictionary that will store the number with the highest count
  answer = {"number": '', "count": 0}
  # get the mode by determining what number appears most often in the table 
  for n in table:
    if (table[n] > answer["count"]):
      answer["count"] = table[n]
      answer["number"] = n
  if answer["number"] == mean:
    return 1
    return 0

Leave a Reply