In this Hackerrank Day 0: Weighted Mean 10 Days of Statistics problem we have Given an array of integers and an array representing the respective weights of the first array's elements, calculate and print the weighted mean of the first array's elements. Your answer should be rounded to a scale of 1 decimal place (i.e., 12.3 formats).

HackerRank Day 0: Weighted Mean | 10 Days of Statistics solution


Problem solution in Python programming.

N = map(int,input().split())
X = list(map(int, input().strip().split(' ')))
W = list(map(int, input().strip().split(' ')))
sum_X = sum([a*b for a,b in zip(X,W)])
print(round((sum_X/sum(W)),1))



Problem solution in Java Programming.

import java.util.Scanner;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Solution {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        scanner.nextLine();

        int[] arrayOne = Stream.of(scanner.nextLine().split(" ")).mapToInt(Integer::valueOf).toArray();
        int[] secondArray = Stream.of(scanner.nextLine().split(" ")).mapToInt(Integer::valueOf).toArray();


        calculateNominator(arrayOne, secondArray);

        double result = calculateNominator(arrayOne, secondArray) / (double) IntStream.of(secondArray).sum();

        System.out.println(String.format("%.1f", result));
    }

    private static int calculateNominator(int[] arrayOne, int[] secondArray) {
        int accumulator = 0;
        for (int i = 0; i < arrayOne.length; i++) {
            accumulator += arrayOne[i] * secondArray[i];
        }

        return accumulator;
    }
}


Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;


int main() {
    int length;
    vector<int> values;
    vector<int> weights;
    cin >> length;
    for(int i = 0; i < length; i++) {
        string input;
        cin >> input;
        values.push_back(stoi(input));
    }
    for(int i = 0; i < length; i++) {
        string input;
        cin >> input;
        weights.push_back(stoi(input));
    }
    float w_mean;
    float top = 0;
    float bottom = 0;
    for(int i = 0; i < length; i++) {
        top += (values[i] * weights[i]);
        bottom += weights[i];
    }
    cout << setprecision(1) << fixed << top/bottom;
    return 0;
}


Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    int N = 0;
    int *number;
    int *weight;
    float mean = 0.0;
    float sumW = 0.0;
    scanf("%d", &N);
    number = (int *)malloc(N*sizeof(int));
    weight = (int *)malloc(N*sizeof(int));
    for(int i = 0; i<N; i++)
        scanf("%d", &number[i]);
    for(int i = 0; i<N; i++)
        scanf("%d", &weight[i]);
    for(int i = 0; i<N; i++){
        sumW += weight[i];
        if(i == 0)
            mean = number[i];
        else
            mean = mean * (sumW - weight[i])/sumW + number[i]*weight[i]/sumW;
    }
    printf("%.1f\n", mean);
    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    var argv = input.split('\n'),
        len = parseInt(argv[0]),
        listX = parseList(argv[1]),
        listW = parseList(argv[2]);
    
    var sumW = sumList(listW);
    var sumXW = sumList(listX.map(function(item, i) {
        return item * listW[i];
    }));
    
    var weightedMean = parseFloat(sumXW / sumW).toFixed(1);
    
    console.log(weightedMean);
}

function parseList(list) {
    return list.split(' ').map(function(item) {
        return parseInt(item);
    });
};

function sumList(list) {
    return list.reduce(function(p, n) {
        return p + n;
    }, 0);
};

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});