Solution to: [Day 0: Mean, Median, and Mode](https://www.hackerrank.com/challenges/s10-basic-statistics/problem)


<h1 id="tocheading">Table of Contents</h1>
<div id="toc"></div>


```javascript
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')
```


    <IPython.core.display.Javascript object>


- Table of Contents
    - Wiki definition
    - Equations
    - Mean
    - Median
    - Mode
- Solution
    - Imports
    - Constants
    - Input
    - Measures of Central Tendency
    - Scale function
    - Main

# Wiki definition
In statistics, a central tendency (or measure of central tendency) is a central or typical value for a probability distribution.

The most common measures of central tendency are the arithmetic mean, the median, and the mode. 
A middle tendency can be calculated for either a finite set of values or for a theoretical distribution, such as the normal distribution. 
Occasionally authors use central tendency to denote "the tendency of quantitative data to cluster around some central value."

The central tendency of a distribution is typically contrasted with its dispersion or variability; 
dispersion and central tendency are the often characterized properties of distributions. 
Analysis may judge whether data has a strong or a weak central tendency based on its dispersion.


## Equations

### Mean
\begin{equation}
\Large
\mu = \frac{\sum_{i=1}^{n}x_{i}}{n} 
\end{equation}

<br/>

### Median

$$
\normalsize
\mathrm{Med}(X) = \begin{cases}
X[\frac{n}{2}] & \text{if n is even} \\
\Large
\frac{X[\frac{n-1}{2}] + X[\frac{n+1}{2}]}{2} & \text{if n is odd}
\end{cases}
$$

<br/>

### Mode

\begin{equation}
\normalsize
\mathrm{Mode}(X) = \mathrm{max}(\mathrm{frequency})
\end{equation}



# Solution
	- Print on separate lines (mean, median, mode)
	- Scale of 1 decimal place
	- With multiple mode values, select min.


## Imports


```python
from collections import Counter  # used to count instances for mode
from typing import List, Tuple, Union
```

## Constants


```python
SCALE = 1	# scale refers to number of decimal places
```

## Input


```python
def get_input() -> Tuple[int, list]:
    """Returns tuple of input:
    n: int, length of list.
    nums: list, nums to use.
    """
    n = int(input())
    nums = sorted(int(num) for num in input().split())
    return (n, nums)
```

## Measures of Central Tendency


```python
def calc_mean(nums: List[int], n: int) -> Union[int, float]:
	"""Returns mean of nums list with n elements."""
	return format_scale( sum(nums) / n)
```


```python
def calc_median(nums: List[int], n: int) -> Union[int, float]:
	"""Returns median of nums list with n elements."""
	mid, is_odd = divmod(len(nums), 2)
	if is_odd:
		median = nums[mid]
	else:
		median = calc_mean( nums[mid - 1: mid + 1], 2)
	return format_scale(median)
```


```python
def calc_mode(nums: List[int], n: int) -> int:
	"""Returns mode of nums list.
	
	Uses auxiliary get_mode_ties func.
	"""
	def get_mode_ties(num_counts: dict, max_freq: int) -> int:
		"""Generator function to return contenders for mode in dict."""
		for num, count in num_counts.items():
			if count == max_freq:
				yield num


	num_counts = Counter(nums)
	max_freq = num_counts[ max( num_counts, key = num_counts.get)]

	return min(list(get_mode_ties(num_counts, max_freq)))
```

## Scale function


```python
def format_scale(num: Union[int, float]) -> float:
	"""Returns float number ."""
	if isinstance(num, int):
		return num
	return round(num, SCALE)
```

## Main


```python
def main():
	measure_central_tendency = (
		calc_mean,
		calc_median,
		calc_mode
	)
	n, nums = get_input()
	for measure in measure_central_tendency:
		print(measure(nums, n))
```


```python
if __name__ == "__main__":
	main()
```

    10
    64630 11735 14216 99233 14470 4978 73429 38120 51135 67060
    43900.6
    44627.5
    4978


b0ba4804d2288cc366d9220ea80de63426068d4e
ipynb
Jupyter Notebook
statistics/10_days/01_day0mean,median,andmode.ipynb
jaimiles23/hacker_rank
0580eac82e5d0989afabb5c2e66faf09713f891b
Apache-2.0
statistics/10_days/01_day0mean,median,andmode.ipynb
jaimiles23/hacker_rank
0580eac82e5d0989afabb5c2e66faf09713f891b
Apache-2.0
statistics/10_days/01_day0mean,median,andmode.ipynb
jaimiles23/hacker_rank
0580eac82e5d0989afabb5c2e66faf09713f891b
Apache-2.0
2021-09-22T11:06:58.000Z
2022-01-25T09:29:24.000Z
Qwen/Qwen-72B
1. YES
2. YES