Solution to: [Day 0: Weighted Mean](https://www.hackerrank.com/challenges/s10-weighted-mean)

<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
    - Equation
- Solution
    - Imports
    - Constants
    - Input
    - Weighted mean
    - Scale
    - Main


## Wiki definition
The weighted arithmetic mean is similar to an ordinary arithmetic mean (the most common type of average), 
except that instead of each of the data points contributing equally to the final average, some data points contribute more than others. 

The notion of weighted mean plays a role in descriptive statistics and also occurs in a more general form in several other areas of mathematics.

If all the weights are equal, then the weighted mean is the same as the arithmetic mean. 


## Equation

\begin{equation}
\huge
\mu_{w} = \frac
{\sum_{i=1}^{n}(x_{i} * w_{i})}
{\sum_{i=1}^{n}w_{i}}
\end{equation}


# Solution

## Imports


```python
from typing import List, Tuple, Union
```

## Constants


```python
SCALE = 1	# number of decimals
```

## Input


```python
def get_input() -> Tuple[int, list, list]:
	"""Returns 3 variabless:
		n: int, length of list
		nums: list, numbers to calculate
		weights: list, weight of numbers.
		"""
	n = int(input())
	nums = [int(val) for val in input().split()]
	weights = [int(val) for val in input().split()]
	return (n, nums, weights)
```

## Weighted mean


```python
def calc_weighted_mean(n: int, nums: List[int], weights: List[int]) -> float:
	"""Returns weighted mean from list of nums and weights with length n."""
	assert (len(nums) == n) and (len(weights) == n)

	weighted_total = 0
	for i in range(n):
		weighted_total += nums[i] * weights[i]
	
	return format_scale( weighted_total / sum(weights))
```

## Scale


```python
def format_scale(num: Union[int, float]) -> Union[int, float]:
	if isinstance(num, int):
		return num
	
	return round(num, 1)
```

## Main


```python
def main():
	n, nums, weights = get_input()
	print( calc_weighted_mean(n, nums, weights))
```


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

    5
    10 40 30 50 20
    1 2 3 4 5
    32.0


60555914a6fe04d14f2b6160c9a6e437cfe9bb79
ipynb
Jupyter Notebook
statistics/10_days/02_day0weightedmean.ipynb
jaimiles23/hacker_rank
0580eac82e5d0989afabb5c2e66faf09713f891b
Apache-2.0
statistics/10_days/02_day0weightedmean.ipynb
jaimiles23/hacker_rank
0580eac82e5d0989afabb5c2e66faf09713f891b
Apache-2.0
statistics/10_days/02_day0weightedmean.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