How to resolve the algorithm Averages/Median step by step in the PowerShell programming language
How to resolve the algorithm Averages/Median step by step in the PowerShell programming language
Table of Contents
Problem Statement
Write a program to find the median value of a vector of floating-point numbers. The program need not handle the case where the vector is empty, but must handle the case where there are an even number of elements. In that case, return the average of the two middle values. There are several approaches to this. One is to sort the elements, and then pick the element(s) in the middle. Sorting would take at least O(n logn). Another approach would be to build a priority queue from the elements, and then extract half of the elements to get to the middle element(s). This would also take O(n logn). The best solution is to use the selection algorithm to find the median in O(n) time. Quickselect_algorithm
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Averages/Median step by step in the PowerShell programming language
Source code in the powershell programming language
function Measure-Data
{
[CmdletBinding()]
[OutputType([PSCustomObject])]
Param
(
[Parameter(Mandatory=$true,
Position=0)]
[double[]]
$Data
)
Begin
{
function Get-Mode ([double[]]$Data)
{
if ($Data.Count -gt ($Data | Select-Object -Unique).Count)
{
$groups = $Data | Group-Object | Sort-Object -Property Count -Descending
return ($groups | Where-Object {[double]$_.Count -eq [double]$groups[0].Count}).Name | ForEach-Object {[double]$_}
}
else
{
return $null
}
}
function Get-StandardDeviation ([double[]]$Data)
{
$variance = 0
$average = $Data | Measure-Object -Average | Select-Object -Property Count, Average
foreach ($number in $Data)
{
$variance += [Math]::Pow(($number - $average.Average),2)
}
return [Math]::Sqrt($variance / ($average.Count-1))
}
function Get-Median ([double[]]$Data)
{
if ($Data.Count % 2)
{
return $Data[[Math]::Floor($Data.Count/2)]
}
else
{
return ($Data[$Data.Count/2], $Data[$Data.Count/2-1] | Measure-Object -Average).Average
}
}
}
Process
{
$Data = $Data | Sort-Object
$Data | Measure-Object -Maximum -Minimum -Sum -Average |
Select-Object -Property Count,
Sum,
Minimum,
Maximum,
@{Name='Range'; Expression={$_.Maximum - $_.Minimum}},
@{Name='Mean' ; Expression={$_.Average}} |
Add-Member -MemberType NoteProperty -Name Median -Value (Get-Median $Data) -PassThru |
Add-Member -MemberType NoteProperty -Name StandardDeviation -Value (Get-StandardDeviation $Data) -PassThru |
Add-Member -MemberType NoteProperty -Name Mode -Value (Get-Mode $Data) -PassThru
}
}
$statistics = Measure-Data 4, 5, 6, 7, 7, 7, 8, 1, 1, 1, 2, 3
$statistics
$statistics.Median
You may also check:How to resolve the algorithm Entropy step by step in the Euler Math Toolbox programming language
You may also check:How to resolve the algorithm Animation step by step in the Logo programming language
You may also check:How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the TypeScript programming language
You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the Sidef programming language
You may also check:How to resolve the algorithm Scope/Function names and labels step by step in the Tcl programming language