Ruby を使用したことはありませんが、このコードを Java に変換する必要があります。
誰でも私を助けることができますか?
これはRubyのコードです。
DEFAULT_PRIOR = [2, 2, 2, 2, 2]
## input is a five-element array of integers
## output is a score between 1.0 and 5.0
def score votes, prior=DEFAULT_PRIOR
posterior = votes.zip(prior).map { |a, b| a + b }
sum = posterior.inject { |a, b| a + b }
posterior.
map.with_index { |v, i| (i + 1) * v }.
inject { |a, b| a + b }.
to_f / sum
end
ここから入手したので、そこに何か手がかりがあるかもしれません。平均の計算について
これが解決策でした。万が一、必要な方がいらっしゃいましたら
static final int[] DEFAULT_PRIOR = {2, 2, 2, 2, 2};
static float score(int[] votes) {
return score(votes, DEFAULT_PRIOR);
}
private static float score(int[] votes, int[] prior) {
int[] posterior = new int[votes.length];
for (int i = 0; i < votes.length; i++) {
posterior[i] = votes[i] + prior[i];
}
int sum = 0;
for (int i = 0; i < posterior.length; i++) {
sum = sum + posterior[i];
}
float sumPlusOne = 0;
for (int i = 0; i < posterior.length; i++) {
sumPlusOne = sumPlusOne + (posterior[i] * (i + 1));
}
return sumPlusOne / sum;
}