Day 0 of the HackerRank Challenge, titled ’10 Days of Statistics,’ serves as an introduction to fundamental statistical concepts. This initial challenge focuses on three key measures: mean, median, and mode.
Understanding the statistical measures is crucial in various fields. In data analysis, they enable us to summarize and interpret data, draw meaningful conclusions, and make informed decisions. In research, they help us explore relationships, identify patterns, and validate hypotheses.
By mastering mean, median, and mode, we gain a solid foundation in statistical analysis. These concepts provide the building blocks for more advanced statistical techniques and machine learning algorithms.
On Day 0 of the challenge, we encounter two specific tasks.
- The first task revolves around exploring mean, median, and mode (triple M), allowing us to practice and deepen our understanding of these concepts.
- The second task focuses on weighted mean, which considers the importance or weight assigned to each value in a dataset.
As we embark on these challenges, let’s leverage the power of Python to solve them effectively and efficiently. Python’s flexibility and extensive libraries make it an excellent tool for statistical analysis.
Prepare to unlock the fascinating world of statistics and witness how these fundamental measures unveil insights and patterns hidden within data. Let’s dive into Day 0 with enthusiasm and embrace the exciting journey ahead!
1st Challenge — Triple M.
Mean — The mean is the average of all the numbers in the array.
Median — The median is the middle value in a sorted list of numbers. If the array has an odd number of elements, the median is the middle number. If the array has an even number of elements, the median is the average of the two middle numbers.
Mode — The mode is the number that appears most frequently in the array.
Question — You have an array of integers called X and Y. Calculate and print the mean, median, and mode of the array. If there are multiple mode values, choose the smallest one numerically
Solution —
from statistics import mode, mean , median
def mmm(arr) -> None:
arr.sort()
print(f"{mean(arr) : .1f}")
print(f"{median(arr) : .1f}")
print(mode(arr))
if __name__ == "__main__":
N= int(input())
arr = list(map(int, input().split()))
mmm(arr)
- def mmm(arr) -> None:: This line defines a function called mmm that takes an array arr as input and returns None. The -> None part in the function signature indicates that the function doesn’t return any value.
arr.sort()
: This line sorts the elements in thearr
list in ascending order. Sorting the list is necessary to calculate the median and mode accurately.
- f’’ is an f-string, allows you to embed expressions inside string literals by using curly braces {}. The f at the beginning of the string denotes that it is an f-string.
- The
:.1f
part specifies the formatting for one decimal place. if __name__ == "__main__":
: This line checks if the code is being executed as the main script (not imported as a module).arr = list(map(int, input().split()))
: This line prompts the user to enter space-separated integers from the console. It expects the user to input the elements of the array.- The
map()
function takes two arguments: the function you want to apply and the iterable object you want to iterate over. It then returns an iterator that yields the results of applying the function to each item in the iterable. - The
map(int, input().split())
part converts the input string into a list of integers. - We use the
split()
method on the input string to split it into a list of values based on whitespace characters
2nd Challenge — Weighted Mean
Weighted mean is a statistical concept used to calculate the average of a set of numbers, where each number is multiplied by a corresponding weight before calculating the mean. The weight represents the relative importance or significance assigned to each number in the dataset.
Steps —
- Multiply each number by its weight.
- Add up all the multiplied values.
- Add up all the weights.
- Divide the total sum of the weighted numbers by the total sum of the weights.
Why we use weighted mean?
By using the weighted mean, you give more weight or importance to certain numbers, and they have a bigger impact on the overall average. This is helpful when you want to reflect the significance of specific data points in your calculations.
Question — You have two arrays: X and Y, containing integers, and an array W representing the weights of X’s elements. Calculate and print the weighted mean of X’s elements. Round your answer to one decimal place.
import math
import os
import random
import re
import sys
# 1. INTEGER_ARRAY W
# 2. INTEGER_ARRAY X
def weightedMean(X, W):
numerator_weightednumbers=[]
for item_x,item_w in zip(vals,weights):
numerator_weightednumbers.append(item_x*item_w)
numerator=sum(numerator_weightednumbers)
denominator=sum(weights)
print(round(numerator/denominator,1))
if __name__ == '__main__':
n = int(input().strip())
vals = list(map(int, input().rstrip().split()))
weights = list(map(int, input().rstrip().split()))
weightedMean(vals, weights)
First we call the function which will give the output of weighted mean
In the first function, we calculated numerator then denominator
- The
zip()
function is used in the provided code to iterate over two lists,vals
andweights
, simultaneously. - The purpose of this iteration is to multiply each element from
vals
with its corresponding element fromweights
and store the results innumerator_weightednumbers
.
Numerator
- sum of numerator_weightednumbers (which is calculated using vals and weights
Denominator
- sum of weights
Main function
- To call it first and as a main function
- Input should be taken as integer
- The
strip()
method is used to remove leading and trailing whitespace characters from a string. - The
rstrip()
method is a string method used to remove trailing whitespace characters from a string. It specifically removes whitespace characters from the right (or end) of the string.