How to resolve the algorithm Fivenum step by step in the Ruby programming language
How to resolve the algorithm Fivenum step by step in the Ruby 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 Ruby programming language
The provided Ruby code defines a method fivenum
that takes an array of numbers as input and calculates the five-number summary for that array. The five-number summary includes the minimum, first quartile, median, third quartile, and maximum of the array.
Let's go through the code step by step:
-
The
fivenum
method starts by sorting the input array in ascending order using thesort
method. The sorted array is stored in thesorted_arr
variable. -
It calculates the size of the sorted array and stores it in the variable
n
. -
It calculates the index of the first quartile, Q1, using the formula:
n4 = (((n + 3).to_f / 2.to_f) / 2.to_f).floor
This formula ensures that
n4
represents the index that divides the sorted array into four equal parts. -
It calculates the following five indices:
d = Array.[](1, n4, ((n.to_f + 1) / 2).to_i, n + 1 - n4, n)
These indices represent the positions of the five values in the sorted array that will be used to calculate the five-number summary.
-
It initializes an empty array called
sum_array
to store the five values. -
It enters a loop that iterates from 0 to 4. During each iteration of the loop:
-
It calculates the floor and ceiling indices for the current position in the
d
array using the formulas:index_floor = (d[e] - 1).floor index_ceil = (d[e] - 1).ceil
-
It calculates the average of the values at the floor and ceil indices and stores it in the
sum_array
.
-
-
Finally, the
sum_array
containing the five values is returned as the five-number summary. -
In the provided test cases, the
fivenum
method is called with different input arrays, and the resulting five-number summaries are printed using thep
method.
Source code in the ruby programming language
def fivenum(array)
sorted_arr = array.sort
n = array.size
n4 = (((n + 3).to_f / 2.to_f) / 2.to_f).floor
d = Array.[](1, n4, ((n.to_f + 1) / 2).to_i, n + 1 - n4, n)
sum_array = []
(0..4).each do |e| # each loops have local scope, for loops don't
index_floor = (d[e] - 1).floor
index_ceil = (d[e] - 1).ceil
sum_array.push(0.5 * (sorted_arr[index_floor] + sorted_arr[index_ceil]))
end
sum_array
end
test_array = [15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [36, 40, 7, 39, 41, 15]
tukey_array = fivenum(test_array)
p tukey_array
test_array = [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]
tukey_array = fivenum(test_array)
p tukey_array
You may also check:How to resolve the algorithm Ternary logic step by step in the Free Pascal programming language
You may also check:How to resolve the algorithm Character codes step by step in the Ada programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the zkl programming language
You may also check:How to resolve the algorithm CRC-32 step by step in the Objeck programming language
You may also check:How to resolve the algorithm Harshad or Niven series step by step in the Frink programming language