In this Hackerrank Day 1: Standard Deviation 10 Days of Statistics problem we have Given an array of integers, calculate and print the standard deviation. Your answer should be in decimal form, rounded to a scale of 1 decimal place

HackerRank Day 1: Standard Deviation | 10 Days of Statistics problem solution


Problem solution in Python programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
n = int(input().strip())
X = [int(x) for x in input().strip().split()]

mean = sum(X) / n
variance = sum([((x - mean) ** 2) for x in X]) / n
stddev = variance ** 0.5

print("{0:0.1f}".format(stddev))



Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(10);
        ArrayList<Integer> x = new ArrayList<>(n);
        for (int i = 0; i < n; ++i) {
            x.add(sc.nextInt(10));
        }
        double mean = 0;
        for (int i = 0; i < n; ++i) {
            mean += (double)x.get(i) / n;
        }
        double variance = 0;
        for (int i = 0; i < n; ++i) {
            double t = x.get(i) - mean;
            variance += (t * t) / n;
        }
        double stDev = Math.sqrt(variance);
        System.out.println(String.format("%.1f", stDev));
    }
}


Problem solution in C++ programming.

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

float mystd(vector<long long> &data) {
    float m = std::accumulate(data.begin(), data.end(), 0);
    m /= data.size();
    float res = 0;
    for (auto el: data) {
        float tmp = el - m;
        res += tmp * tmp;
    }
    return sqrt(res / data.size());
}

int main() {
    size_t n;
    cin >> n;
    vector<long long> data(n);
    for (auto &el: data) cin >> el;
    cout << fixed << setprecision(1) << mystd(data) << endl;
    return 0;
}


Problem solution in C programming.

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

int main() {
    int n, i;
    double mean, sum_sq_dist, dist;
    double std_dev;
    int *dataset;
    
    scanf ("%d", &n);

    dataset = (int *) malloc (n * sizeof(int));

    for (i=0; i<n; i++)
        scanf ("%d", (dataset + i));

    mean = 0;
    for (i=0; i<n; i++)
        mean += *(dataset + i);
    mean = mean / n;
    
    sum_sq_dist = 0;
    for (i=0; i<n; i++) {
        dist = *(dataset + i) - mean;
        sum_sq_dist += dist * dist;
    }
   
    std_dev = sqrt(sum_sq_dist / n);
    
    printf ("%0.1lf\n", std_dev);

    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    var aNumbers = input.split("\n")[1].split(" ").map(parseFloat);
    var n = aNumbers.length;
    var nSum = 0;
    var nSumX2 = 0;
    for (var i = 0; i < aNumbers.length; i++) {
        var x = aNumbers[i];
        nSum += x;
        nSumX2 += x*x;
    }
    var nMean = (nSum/n);
    var nM2 = nSumX2/n;
    var nStdDev = Math.sqrt(nM2 - nMean*nMean);
    var nRounded = Math.round(nStdDev*10)/10;
    if ((nRounded + "").indexOf(".") < 0) {
        console.log(nRounded + ".0");
    } else {
        console.log(nRounded);
    }
} 

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

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