How to resolve the algorithm Fivenum step by step in the Kotlin programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Fivenum step by step in the Kotlin programming language

Table of Contents

Problem Statement

Many big data or scientific programs use boxplots to show distributions of data.   In addition, sometimes saving large arrays for boxplots can be impractical and use extreme amounts of RAM.   It can be useful to save large arrays as arrays with five numbers to save memory. For example, the   R   programming language implements Tukey's five-number summary as the fivenum function.

Given an array of numbers, compute the five-number summary.

While these five numbers can be used to draw a boxplot,   statistical packages will typically need extra data. Moreover, while there is a consensus about the "box" of the boxplot,   there are variations among statistical packages for the whiskers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Fivenum step by step in the Kotlin programming language

The provided Kotlin code defines two functions, median and fivenum to calculate the median and five-number summary of an input array of doubles, respectively. The following is a breakdown of the code:

  1. median Function:

    • This function calculates the median of a given slice of a double array.
    • It takes three arguments:
      • x: The input double array
      • start: The starting index of the slice (inclusive)
      • endInclusive: The ending index of the slice (inclusive)
    • It calculates the median as follows:
      • If the slice size is odd, the median is the element at index start + size / 2.
      • If the slice size is even, the median is the average of the two elements at indices start + size / 2 - 1 and start + size / 2.
  2. fivenum Function:

    • This function calculates the five-number summary of a given double array. The five-number summary consists of the minimum, first quartile (Q1), median (Q2), third quartile (Q3), and maximum values.
    • It takes one argument:
      • x: The input double array
    • It calculates the five-number summary as follows:
      • It first checks that the array contains no NaN (Not-a-Number) values.
      • It sorts the array in ascending order.
      • It calculates the minimum (x[0]) and maximum (x[x.lastIndex]) values.
      • It calculates the median (Q2) using the median function on the entire array.
      • It calculates the first quartile (Q1) as the median of the lower half of the sorted array (indices 0 to x.size / 2 - 1 if the array size is even, or 0 to x.size / 2 if the array size is odd).
      • It calculates the third quartile (Q3) as the median of the upper half of the sorted array (indices x.size / 2 to x.size - 1 if the array size is even, or x.size / 2 to x.size - 1 if the array size is odd).
    • It returns a double array containing the five-number summary: [minimum, Q1, Q2, Q3, maximum].
  3. main Function:

    • The main function demonstrates the usage of the fivenum function.
    • It defines a list of double arrays (xl) and iterates through it, printing the five-number summary of each array.

Source code in the kotlin programming language

// version 1.2.21

fun median(x: DoubleArray, start: Int, endInclusive: Int): Double {
    val size = endInclusive - start + 1
    require (size > 0) { "Array slice cannot be empty" }
    val m = start + size / 2
    return if (size % 2 == 1) x[m] else (x[m - 1] + x[m]) / 2.0
}

fun fivenum(x: DoubleArray): DoubleArray {
    require(x.none { it.isNaN() }) { "Unable to deal with arrays containing NaN" }
    val result = DoubleArray(5)
    x.sort()
    result[0] = x[0]
    result[2] = median(x, 0, x.size - 1)
    result[4] = x[x.lastIndex]
    val m = x.size / 2
    var lowerEnd = if (x.size % 2 == 1) m else m - 1
    result[1] = median(x, 0, lowerEnd)
    result[3] = median(x, m, x.size - 1)
    return result
}

fun main(args: Array<String>) {
    var xl = listOf(
        doubleArrayOf(15.0, 6.0, 42.0, 41.0, 7.0, 36.0, 49.0, 40.0, 39.0, 47.0, 43.0),
        doubleArrayOf(36.0, 40.0, 7.0, 39.0, 41.0, 15.0),
        doubleArrayOf(
             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
        )
    )
    xl.forEach { println("${fivenum(it).asList()}\n") }
}


  

You may also check:How to resolve the algorithm User input/Text step by step in the min programming language
You may also check:How to resolve the algorithm Terminal control/Inverse video step by step in the COBOL programming language
You may also check:How to resolve the algorithm Haversine formula step by step in the BASIC programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the Clojure programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the Hy programming language