TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Mean, Median, Mode

Probability and Statistics
Easy

Compute three measures of a nonempty one-dimensional numeric dataset. The mean is:

xˉ=1n∑i=1nxi\bar{x} = \frac{1}{n}\sum_{i=1}^{n}x_ixˉ=n1​i=1∑n​xi​

The median is the middle sorted value, or the average of the two middle values when nnn is even. The mode is the most frequent value. If several values share the highest frequency, choose the smallest. Return mean, median, and mode in a dictionary of Python floats.

Loading visualization...

Examples

Input: x = [1, 2, 3, 4, 5]

Output: {"mean": 3.0, "median": 3.0, "mode": 1.0}

Explanation: Every value appears once, so the smallest value wins the mode tie.

Input: x = [1, 2, 2, 3, 4]

Output: {"mean": 2.4, "median": 2.0, "mode": 2.0}

Input: x = [1, 2, 3, 4]

Output: {"mean": 2.5, "median": 2.5, "mode": 1.0}

Hint 1

Use np.mean and np.median for the first two values.

Hint 2

Count values with Counter, find the highest frequency, then take the smallest matching key.

Requirements

  • Compute the arithmetic mean and median
  • Choose the smallest value when the mode is tied
  • Return exactly mean, median, and mode in a dictionary of Python floats

Constraints

  • x is a nonempty one-dimensional numeric list
  • Use NumPy and the Python standard library only
Try Similar Problems
Sample Var StdPercentilesExpected Value DiscreteBootstrap MeanBinomial Pmf Cdf

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

You must run your code first.
PrevNext

Mean, Median, Mode

Probability and Statistics
Easy

Compute three measures of a nonempty one-dimensional numeric dataset. The mean is:

xˉ=1n∑i=1nxi\bar{x} = \frac{1}{n}\sum_{i=1}^{n}x_ixˉ=n1​i=1∑n​xi​

The median is the middle sorted value, or the average of the two middle values when nnn is even. The mode is the most frequent value. If several values share the highest frequency, choose the smallest. Return mean, median, and mode in a dictionary of Python floats.

Loading visualization...

Examples

Input: x = [1, 2, 3, 4, 5]

Output: {"mean": 3.0, "median": 3.0, "mode": 1.0}

Explanation: Every value appears once, so the smallest value wins the mode tie.

Input: x = [1, 2, 2, 3, 4]

Output: {"mean": 2.4, "median": 2.0, "mode": 2.0}

Input: x = [1, 2, 3, 4]

Output: {"mean": 2.5, "median": 2.5, "mode": 1.0}

Hint 1

Use np.mean and np.median for the first two values.

Hint 2

Count values with Counter, find the highest frequency, then take the smallest matching key.

Requirements

  • Compute the arithmetic mean and median
  • Choose the smallest value when the mode is tied
  • Return exactly mean, median, and mode in a dictionary of Python floats

Constraints

  • x is a nonempty one-dimensional numeric list
  • Use NumPy and the Python standard library only
Try Similar Problems
Sample Var StdPercentilesExpected Value DiscreteBootstrap MeanBinomial Pmf Cdf

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

You must run your code first.