-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
03e0581
commit b9913d0
Showing
11 changed files
with
131 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
src/NetPackageAnalyzer/NetPackageAnalyzerObjects/Statistical.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/NetPackageAnalyzer/NetPackageAnalyzerTests/TestStatistics.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Statistical; | ||
|
||
namespace NetPackageAnalyzerTests; | ||
[TestClass] | ||
public class TestStatistics | ||
{ | ||
|
||
[TestMethod] | ||
public void TestMedian() | ||
{ | ||
var values = new int[] { 2, 100, 300, 400, 500 }; | ||
var median = StatisticalNumbers<int>.Median(values); | ||
Assert.AreEqual(300, median); | ||
} | ||
|
||
[TestMethod] | ||
public void TestMean() | ||
{ | ||
var values = new int[] { 5, 100, 300, 400, 500 }; | ||
var median = StatisticalNumbers<int>.ArithmeticMean(values); | ||
Assert.AreEqual(261, median); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System.Numerics; | ||
namespace Statistical; | ||
|
||
public static class StatisticalNumbers<T> | ||
where T : INumber<T>,IDivisionOperators<T, T, T> | ||
{ | ||
public static T Median(T[]? values) | ||
{ | ||
var size = values?.Length ?? 0; | ||
if (size == 0) | ||
{ | ||
return T.Zero; | ||
} | ||
ArgumentNullException.ThrowIfNull(values); | ||
if (size == 1) | ||
{ | ||
return values![0]; | ||
} | ||
Array.Sort(values); | ||
int mid = size / 2; | ||
T median = values[mid]; | ||
if (size % 2 == 0) | ||
{ | ||
median = (values[mid] + values[mid - 1]); | ||
median = median / T.CreateChecked(2); | ||
} | ||
return median; | ||
} | ||
public static T ArithmeticMean(T[]? values) | ||
{ | ||
var size = values?.Length ?? 0; | ||
if (size == 0) | ||
{ | ||
return T.Zero; | ||
} | ||
ArgumentNullException.ThrowIfNull(values); | ||
|
||
if(!T.TryParse(size.ToString(), null, out var mid )) { | ||
throw new ArgumentException("Cannot parse the size of the array to T"); | ||
}; | ||
|
||
|
||
if (size == 1) | ||
{ | ||
return values![0]; | ||
} | ||
T res= T.Zero; | ||
foreach (var value in values) | ||
{ | ||
res += value; | ||
} | ||
|
||
return res / mid; | ||
|
||
} | ||
|
||
public static Tuple<T[], int> Mode(T[]? values) | ||
{ | ||
var size = values?.Length ?? 0; | ||
if (size == 0) | ||
{ | ||
return Tuple.Create(new T[] { T.Zero },0); | ||
} | ||
ArgumentNullException.ThrowIfNull(values); | ||
|
||
var data = values | ||
.GroupBy(v=>v) | ||
.ToDictionary(v=>v,v=>v.Count()); | ||
|
||
var max = data.Max(it=>it.Value); | ||
var vals = data | ||
.Where(it => it.Value == max) | ||
.Select(it => it.Key.Key) | ||
.ToArray(); | ||
|
||
return Tuple.Create(vals ?? [] , max); | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |