Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

Zusammenfassung in 5 Zahlen (Anweisungen und Abfolgen)

Die Zusammenfassung in 5 Zahlen (englisch Five-Number-Summary) enthält:

  1. den Minimalwert,
  2. das untere Quartil,
  3. den Median,
  4. das höhere Quartil und
  5. den Maximalwert

einer Datenmenge.

Manche Programmiersprachen liefern die 5-Zahlen-Zusammenfassung per Bordmittel, aber bitte ignorieren Sie das.

Entwickeln Sie einen Algorithmus für die 5-Zahlen-Zusammenfassung. 

Das obere Quartil ist der Median der oberen, das untere Quartil der Median der unteren Hälte der Datenmenge.

0 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

5 Lösung(en)

// C++ 20 | VS-2022
#include <iostream>
#include <algorithm>
#include <vector>
#include <format> // C++ 20!

struct quartile {
    const std::vector<double> lowQ, uppQ;
};

template<typename _Con>
inline constexpr auto get_median(const _Con& con_) {
    const auto ctr{ [&con_]() { return int(con_.size() / 2) - 1; } };
    if (con_.size() == 0) return 0.0;
    else if (con_.size() & 1) return double(con_[ctr() + 1ll]);
    else return (con_[ctr()] + con_[ctr() + 1ll]) / 2.0;
}

template<typename _Con>
inline constexpr auto get_quartile(const _Con& con_) {
    const auto it{ con_.size() / 2 };
    const auto half{ con_.size() % 2 != 0 };
    return quartile({ con_.begin(), con_.begin() + it + half }, { con_.begin() + it , con_.end() }); // C++ 20!
}

template<typename _Con>
inline constexpr auto get_five_number_summary(_Con con_) {
    std::sort(con_.begin(), con_.end());
    const auto min{ *std::min_element(con_.begin(), con_.end()) };
    const auto [lowQ, uppQ] = get_quartile(con_); // C++ 20!
    const auto low{ get_median(lowQ) };
    const auto med{ get_median(con_) };
    const auto upp{ get_median(uppQ) };
    const auto max{ *std::max_element(con_.begin(), con_.end()) };
    return std::make_tuple(min, low, med, upp, max);
}

int main() {
    const std::vector nums{ 15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43 };
    const auto [min, low, med, upp, max] = get_five_number_summary(nums); // C++ 20!

    // Ausgabe 'format' wird - trotz C++ 20 - nicht von allen Compilern unterstützt!
    std::cout << std::format("[{}, {}, {}, {}, {}]\n", min, low, med, upp, max); // C++ 20!

    // Diese funktioniert immer:
    //std::cout << "[" << min << ", " << low << ", " << med << ", " << upp << ", " << max << "]\n";
}
                

Lösung von: Jens Kelm (@JKooP)

function median(arr) {
  let mid = Math.floor(arr.length / 2);
  return (arr.length % 2 == 0)
    ? (arr[mid-1] + arr[mid]) / 2
    : arr[mid];
}

Array.prototype.fiveNumSummary = function() {
  this.sort(function(a, b) { return a - b} );
  let mid = Math.floor(this.length / 2),
      loQ = (this.length % 2 == 0)
        ? this.slice(0, mid)
        : this.slice(0, mid+1),
      hiQ = this.slice(mid);
  return [ this[0],
           median(loQ),
           median(this),
           median(hiQ),
           this[this.length-1]
  ];
}

// testing
let verification1 = [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43],
    verification2 = [
       0.14082834,  0.09748790,  1.73131507,  0.87636009, -1.95059594,
       0.73438555, -0.03035726,  1.46675970, -0.74621349, -0.72588772,
       0.63905160,  0.61501527, -0.98983780, -1.00447874, -0.62759469,
       0.66206163,  1.04312009, -0.10305385,  0.75775634, 0.32566578
    ],
    oneToTwenty = [
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
    ],
    populationOfSwissCantons = [  // quelle: https://en.wikipedia.org/wiki/Cantons_of_Switzerland
      694_072,    16_293,  55_309, 1_043_132, 292_955, 201_156, 325_496, 506_343,
       40_851,   200_096,  73_709,   416_347, 175_894,  43_520,  38_108, 514_504,
       83_107,   277_462, 162_157,   282_909, 350_986,  36_819, 814_762, 348_503,
      128_794, 1_553_423
    ];
console.log( verification1.fiveNumSummary() );
console.log( verification2.fiveNumSummary() );
console.log( oneToTwenty.fiveNumSummary() );
console.log( populationOfSwissCantons.fiveNumSummary() );

                

Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)

// NET 7.x | C# 11.x | VS-2022

var lst = new List<int> { 15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43 };
var (min, low, med, upp, max) = GetFiveNumberSummary(lst);
Console.WriteLine($"[{min}, {low}, {med}, {upp}, {max}]");

double GetMedian<T>(List<T> lst) {
    var ctr = lst.Count / 2 - 1;
    if (!lst.Any()) return 0;
    else if (lst.Count % 2 != 0) return Convert.ToDouble(lst[ctr + 1]);
    else return (Convert.ToDouble(lst[ctr]) + Convert.ToDouble(lst[ctr + 1])) / 2.0;
}

(List<T>, List<T>) GetQuartile<T>(List<T> lst) =>
    (lst.Take(lst.Count / 2 + (lst.Count % 2 != 0 ? 1 : 0)).ToList(), lst.Skip(lst.Count / 2).ToList());

(double min, double low, double med, double upp, double max)GetFiveNumberSummary<T>(List<T> lst) {
    lst = lst.OrderBy(x => x).ToList();
    var (lowQ, uppQ) = GetQuartile(lst);
    return (Convert.ToDouble(lst.Min()), GetMedian(lowQ), GetMedian(lst), GetMedian(uppQ), Convert.ToDouble(lst.Max()));
}
                

Lösung von: Jens Kelm (@JKooP)

// NET 7.x | C# 11.x | VS-2022

var lst = new List<double> { 15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43 }.OrderBy(x => x).ToList();

Console.WriteLine($"[{lst.Min()}, {GetMedian(lst.Take(lst.Count / 2 + (lst.Count % 2 != 0 ? 1 : 0)).ToList())}, " +
    $"{GetMedian(lst)}, {GetMedian(lst.Skip(lst.Count / 2).ToList())}, {lst.Max()}]");

static double GetMedian(List<double> lst) {
    var ctr = lst.Count / 2 - 1;
    return lst.Count % 2 != 0 ? lst[ctr + 1] : (lst[ctr] + lst[ctr + 1]) / 2;
}
                

Lösung von: Jens Kelm (@JKooP)

// C++23 | VS-2022

#include <algorithm>
#include <format>
#include <print>
#include <ranges>
#include <vector>

namespace rng = std::ranges;
namespace vws = std::views;

static inline constexpr auto get_median(const auto& con_) {
    const auto ctr{ [&con_]() { return int(con_.size() / 2) - 1; } };
    if (con_.size() == 0) return 0.0;
    else if (con_.size() & 1) return double(con_[ctr() + 1LL]);
    else return (con_[ctr()] + con_[ctr() + 1LL]) / 2.0;
}

static inline constexpr auto get_quartile(auto&& con_) {
    const auto mid{ con_.size() / 2 };
    const auto add{ con_.size() % 2 != 0 };
    const auto lowQ{ con_ | vws::take(mid + add) };
    const auto uppQ{ con_ | vws::drop(mid) };
    return std::make_tuple(lowQ, uppQ);
}

static inline constexpr auto get_five_number_summary(const auto& con_) {
    auto con{ con_ };
    rng::sort(con);
    const auto [min, max]{ rng::minmax(con) };
    const auto [lowQ, uppQ]{ get_quartile(con) };
    const auto low{ get_median(lowQ) };
    const auto med{ get_median(con) };
    const auto upp{ get_median(uppQ) };
    return std::make_tuple(min, low, med, upp, max);
}

int main() {
    const std::vector nums{ 15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43 };
    const auto [min, low, med, upp, max] { get_five_number_summary(nums)};
    std::println("[{}, {}, {}, {}, {}]\n", min, low, med, upp, max); // C++23
}
                

Lösung von: Jens Kelm (@JKooP)

Verifikation/Checksumme:

fiveNums([15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43]) // > [ 6, 25.5, 40, 42.5, 49 ]
fiveNums(test = [0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726,  1.46675970, -0.74621349, -0.72588772,0.63905160,  0.61501527, -0.98983780, -1.00447874, -0.62759469,0.66206163,  1.04312009, -0.10305385,  0.75775634, 0.32566578];

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit: 0.5
Schwierigkeit: Leicht
Webcode: 0xrz-3pi0
Autor: ()

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen