簡単 0件解決済み 正答率0%

Simple Statistics

Return the minimum, maximum and average of a list of whole numbers.

このチャレンジで学べること

  • Track the smallest and largest value in one pass
  • Compute an average without integer division
  • Handle an empty list

問題

翻訳を表示

After a fundraiser, the club treasurer has the amount each member collected and wants three quick numbers: the smallest amount, the largest amount and the average.

Implement simple_statistics(numbers).

Input

numbers: The amounts collected.

Output

Return [minimum, maximum, average] as numbers. The average is the sum divided by the count, not rounded (checked to within 0.000001). For an empty list return an empty list.

制約

0 ≤ length of numbers ≤ 1000

−1,000,000 ≤ each number ≤ 1,000,000

例

例 1
入力:
numbers = [40, 15, 70, 35]

期待される出力:
expected = [15.0, 70.0, 40.0]
入力:
std::vector<long long> numbers = {40, 15, 70, 35};

期待される出力:
std::vector<double> expected = {15.0, 70.0, 40.0};
入力:
long[] numbers = new long[] {40, 15, 70, 35};

期待される出力:
double[] expected = new double[] {15.0, 70.0, 40.0};

解説

The smallest is 15, the largest is 70, and the average is 160 / 4 = 40, so [15.0, 70.0, 40.0].

例 2
入力:
numbers = [5, 6]

期待される出力:
expected = [5.0, 6.0, 5.5]
入力:
std::vector<long long> numbers = {5, 6};

期待される出力:
std::vector<double> expected = {5.0, 6.0, 5.5};
入力:
long[] numbers = new long[] {5, 6};

期待される出力:
double[] expected = new double[] {5.0, 6.0, 5.5};

解説

The average of 5 and 6 is 5.5, so [5.0, 6.0, 5.5].

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

Start the minimum and maximum at the first value, not at 0.

ヒント 2

Keep a running sum as you go.

ヒント 3

Convert to a decimal type before dividing the sum by the count.

目安の計算量

O(n) time, O(1) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。