Einfach 0 gelöst 0% Erfolgsquote

Median of a List

Find the middle value of a list after sorting.

Sort the values into ascending order. For an odd number of values, take the middle one. For an even number, take the arithmetic mean of the two middle values. This challenge accepts only nonempty lists.

Implement median_number(numbers).

Input

numbers: The nonempty list of values.

Output

Return the median as a number; it may end in .5.

Einschränkungen

1 ≤ list length ≤ 100,000. Each value is an integer between −1,000,000,000 and 1,000,000,000.

Beispiele

Beispiel 1
Eingabe:
numbers = [2, 8, 4, 10]

Erwartete Ausgabe:
expected = 6.0
Eingabe:
std::vector<long long> numbers = {2, 8, 4, 10};

Erwartete Ausgabe:
double expected = 6.0;
Eingabe:
long[] numbers = new long[] {2, 8, 4, 10};

Erwartete Ausgabe:
double expected = 6.0;

Erläuterung

Sorted order is 2, 4, 8, 10. The middle pair averages to (4 + 8) / 2 = 6.

Beispiel 2
Eingabe:
numbers = [9, 1, 4]

Erwartete Ausgabe:
expected = 4.0
Eingabe:
std::vector<long long> numbers = {9, 1, 4};

Erwartete Ausgabe:
double expected = 4.0;
Eingabe:
long[] numbers = new long[] {9, 1, 4};

Erwartete Ausgabe:
double expected = 4.0;

Erläuterung

Sorted order is 1, 4, 9, whose middle value is 4.

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.