Skip to content

Instantly share code, notes, and snippets.

@omas-public
Last active April 15, 2016 04:58
Show Gist options
  • Save omas-public/34f9e1356dd0443ed188b9e40061cc82 to your computer and use it in GitHub Desktop.
Save omas-public/34f9e1356dd0443ed188b9e40061cc82 to your computer and use it in GitHub Desktop.
AOJ ITP1_10_C: Standard Deviation
(function(stdin) {
'use strict';
var inputs = stdin.toString().trim();
var re = /\w+\n[\w\s]*?\n/g;
var lines = inputs.match(re).map(function(line) {
return line.split('\n')[1]
.split(' ')
.slice(0, line.split('\n')[0])
.map(Number);
});
var result = (function(lines) {
return lines.map(stdev);
/**
* [stdev description] 標準偏差値を求める
* @param {Array.<number>} scores 数値のリスト
* @return {number} 標準偏差
*/
function stdev(scores) {
var dev = variance(average(scores));
// return Math.sqrt(average(scores.map(dev)));
return [
[scores.map(dev)] // 分散を求める
.map(average) // 平均をとる
]
.map(Math.sqrt); // 平方根を求める
}
/**
* [variance description] 分散を求める
* @param {number} average 平均点
* @param {number} score 得点
* @return {number} 分散
*/
function variance(average) {
return function(score) {
return Math.pow(score - average, 2);
};
}
/**
* [average description] 平均点を求める
* @param {Array.<number>} scores 点数のリスト
* @return {number} 平均点
*/
function average(scores) {
return sum(scores) / scores.length;
/**
* [sum description] 点数リストの合計を求める
* @param {Array.<number>} scores 点数のリスト
* @return {number} 合計点
*/
function sum(scores) {
return scores.reduce(add);
}
/**
* [add description] パラメータ a,b の加算値を返す
* @param {number} a [description]
* @param {number} b [description]
* @return {number} 加算値
*/
function add(a, b) {
return a + b;
}
}
})(lines);
console.log(result.join('\n'));
})(require('fs').readFileSync('/dev/stdin', 'utf8'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment