BlackBoxAuditing/repairers/calculators.py
def get_median(values, kdd):
  """
  Given an unsorted list of numeric values, return median value (as a float).
  Note that in the case of even-length lists of values, we apply the value to
  the left of the center to be the median (such that the median can only be
  a value from the list of values).
  Eg: get_median([1,2,3,4]) == 2, not 2.5.
  """

  if not values:
    raise Exception("Cannot calculate median of list with no values!")

  sorted_values = deepcopy(values)
  sorted_values.sort() # Not calling `sorted` b/c `sorted_values` may not be list.

  if kdd:
    return sorted_values[len(values)//2]
  else:
    if len(values) % 2 == 0:
      return sorted_values[len(values)//2-1]
    else:
      return sorted_values[len(values)//2]
def get_median(values, kdd):
  """
  Given an unsorted list of numeric values, return median value (as a float).
  Note that in the case of even-length lists of values, we apply the value to
  the left of the center to be the median (such that the median can only be
  a value from the list of values).
  Eg: get_median([1,2,3,4]) == 2, not 2.5.
  """

  if not values:
    raise Exception("Cannot calculate median of list with no values!")

  sorted_values = deepcopy(values)
  sorted_values.sort() # Not calling `sorted` b/c `sorted_values` may not be list.

  if kdd:
    return sorted_values[len(values)//2]
  else:
    if len(values) % 2 == 0:
      return sorted_values[len(values)//2-1]
    else:
      return sorted_values[len(values)//2]
Given an unsorted list of numeric values, return median value (as a float).
  Note that in the case of even-length lists of values, we apply the value to
  the left of the center to be the median (such that the median can only be
  a value from the list of values).
  Eg: get_median([1,2,3,4]) == 2, not 2.5.