Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0bbdeb5

Browse files
Merge branch 'feature/41-add-median-function' into develop
Fixes #41
2 parents 4186f6e + 6372ee4 commit 0bbdeb5

File tree

5 files changed

+156
-2
lines changed

5 files changed

+156
-2
lines changed

‎collection/665.dat

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function Median(A: array of Double): Double; overload;
2+
var
3+
MiddleLo: Integer;
4+
begin
5+
if System.Length(A) = 0 then
6+
raise SysUtils.EArgumentException.Create('Array is empty');
7+
// optimisations for array lengths 1 & 2 to avoid sorting
8+
if System.Length(A) = 1 then
9+
Exit(A[0]);
10+
if System.Length(A) = 2 then
11+
Exit((A[0] + A[1]) / 2.0);
12+
Generics.Collections.TArray.Sort<Double>(A); // using standard comparer
13+
MiddleLo := System.Length(A) div 2 - 1;
14+
if System.Odd(System.Length(A)) then
15+
Result := A[MiddleLo + 1]
16+
else
17+
Result := (A[MiddleLo] + A[MiddleLo + 1]) / 2.0;
18+
end;

‎collection/666.dat

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function Median(A: array of Integer): Double; overload;
2+
var
3+
MiddleLo: Integer;
4+
begin
5+
if System.Length(A) = 0 then
6+
raise SysUtils.EArgumentException.Create('Array is empty');
7+
// optimisations for array lengths 1 & 2 to avoid sorting
8+
if System.Length(A) = 1 then
9+
Exit(A[0]);
10+
if System.Length(A) = 2 then
11+
Exit((A[0] + A[1]) / 2);
12+
Generics.Collections.TArray.Sort<Integer>(A); // using standard comparer
13+
MiddleLo := System.Length(A) div 2 - 1;
14+
if System.Odd(Length(A)) then
15+
Result := A[MiddleLo + 1]
16+
else
17+
Result := (A[MiddleLo] + A[MiddleLo + 1]) / 2;
18+
end;

‎collection/maths.ini

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,3 +1871,29 @@ AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tes
18711871
Snip=664.dat
18721872
DelphiXE=Y
18731873
Delphi12A=Y
1874+
1875+
[Median_Double]
1876+
DisplayName="Median (Double overload)"
1877+
DescEx="<p>Returns the median of an array of floating point values.</p><p>Raises an <var>EArgumentException</var> exception if the array is empty.</p>"
1878+
Kind=routine
1879+
Units=SysUtils,Generics.Collections
1880+
SeeAlso=Median_Integer
1881+
TestInfo=advanced
1882+
AdvancedTest.Level=unit-tests
1883+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1884+
Snip=665.dat
1885+
DelphiXE=Y
1886+
Delphi12A=Y
1887+
1888+
[Median_Integer]
1889+
DisplayName="Median (Integer overload)"
1890+
DescEx="<p>Returns the median of an array of integer values.</p><p>Raises an <var>EArgumentException</var> exception if the array is empty.</p>"
1891+
Kind=routine
1892+
Units=SysUtils,Generics.Collections
1893+
SeeAlso=Median_Double
1894+
TestInfo=advanced
1895+
AdvancedTest.Level=unit-tests
1896+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1897+
Snip=666.dat
1898+
DelphiXE=Y
1899+
Delphi12A=Y

‎tests/Cat-Maths/TestUMathsCatSnippets.pas

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ TestMathsCatSnippets = class(TTestCase)
8686
procedure TestIsNarcissistic;
8787
procedure TestLSE; // required by SoftMax
8888
procedure TestSoftMax;
89+
procedure TestMedian_Integer;
90+
procedure TestMedian_Double;
8991
end;
9092

9193
implementation
@@ -881,6 +883,38 @@ procedure TestMathsCatSnippets.TestMaxOfArray_Single;
881883
Check(SameValue(N, MaxOfArray(A)), 'Test 5');
882884
end;
883885

886+
procedure TestMathsCatSnippets.TestMedian_Double;
887+
const
888+
Fudge = 0.000001;
889+
A1: array[1..1] of Double = (436.57);
890+
A2: array[1..2] of Double = (-123.45, 170.05);
891+
A5: array[1..5] of Double = (1.234, 4256.12345, 7000000000.0, PI, 0.000006758493);
892+
A6: array[1..6] of Double = (4883.937382, 37473.0, 235.00001, -99.9282, 42.32654, 56.986382);
893+
A7: array[1..7] of Double = (938298.0837, 729837.3627, 80001.34, 79876.46372, 67012.1234, 38983.12, 3500.93937);
894+
begin
895+
CheckTrue(SameValue(436.57, Median(A1), Fudge), '#1');
896+
CheckTrue(SameValue(23.3, Median(A2), Fudge), '#2');
897+
CheckTrue(SameValue(PI, Median(A5), Fudge), '#5');
898+
CheckTrue(SameValue(145.993196, Median(A6), Fudge), '#6');
899+
CheckTrue(SameValue(79876.46372, Median(A7), Fudge), '#7');
900+
end;
901+
902+
procedure TestMathsCatSnippets.TestMedian_Integer;
903+
const
904+
A1: array[1..1] of Integer = (4);
905+
A2: array[1..2] of Integer = (-6, 1);
906+
A5: array[1..5] of Integer = (1, 3, 5, 7, 9);
907+
A6: array[1..6] of Integer = (4883, 37473, 235, -99, 42, 56);
908+
A7: array[1..7] of Integer = (77, 66, 55, 44, 33, 22, 11);
909+
begin
910+
CheckTrue(SameValue(4.0, Median(A1)), '#1');
911+
CheckTrue(SameValue(-2.5, Median(A2)), '#2');
912+
CheckTrue(SameValue(5.0, Median(A5)), '#5');
913+
CheckTrue(SameValue(145.5, Median(A6)), '#6');
914+
CheckTrue(SameValue(44.0, Median(A7)), '#7');
915+
916+
end;
917+
884918
procedure TestMathsCatSnippets.TestMid_Double;
885919
var
886920
A, B, C, D, E: Double;

‎tests/Cat-Maths/UMathsCatSnippets.pas

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* The unit is copyright © 2005-2024 by Peter Johnson & Contributors and is
77
* licensed under the MIT License (https://opensource.org/licenses/MIT).
88
*
9-
* Generated on : 2025年1月10日 09:52:09 GMT.
9+
* Generated on : 2025年1月10日 19:22:03 GMT.
1010
* Generated by : DelphiDabbler CodeSnip Release 4.24.0.
1111
*
1212
* The latest version of CodeSnip is available from the CodeSnip GitHub project
@@ -18,7 +18,7 @@
1818
interface
1919

2020
uses
21-
SysUtils, Math, Types, Windows;
21+
SysUtils, Math, Types, Windows, Generics.Collections;
2222

2323
{
2424
Encapsulates a point with double precision floating point coordinates.
@@ -268,6 +268,18 @@ function MaxOfArray(const A: array of Integer): Integer; overload;
268268
}
269269
function MaxOfArray(const A: array of Single): Single; overload;
270270

271+
{
272+
Returns the median of an array of floating point values.
273+
Raises an EArgumentException exception if the array is empty.
274+
}
275+
function Median(A: array of Double): Double; overload;
276+
277+
{
278+
Returns the median of an array of integer values.
279+
Raises an EArgumentException exception if the array is empty.
280+
}
281+
function Median(A: array of Integer): Double; overload;
282+
271283
{
272284
Returns the middle of three double precision floating point values.
273285
}
@@ -1314,6 +1326,52 @@ function MaxOfArray(const A: array of Single): Single; overload;
13141326
Result := A[Idx];
13151327
end;
13161328

1329+
{
1330+
Returns the median of an array of floating point values.
1331+
Raises an EArgumentException exception if the array is empty.
1332+
}
1333+
function Median(A: array of Double): Double; overload;
1334+
var
1335+
MiddleLo: Integer;
1336+
begin
1337+
if System.Length(A) = 0 then
1338+
raise SysUtils.EArgumentException.Create('Array is empty');
1339+
// optimisations for array lengths 1 & 2 to avoid sorting
1340+
if System.Length(A) = 1 then
1341+
Exit(A[0]);
1342+
if System.Length(A) = 2 then
1343+
Exit((A[0] + A[1]) / 2.0);
1344+
Generics.Collections.TArray.Sort<Double>(A); // using standard comparer
1345+
MiddleLo := System.Length(A) div 2 - 1;
1346+
if System.Odd(System.Length(A)) then
1347+
Result := A[MiddleLo + 1]
1348+
else
1349+
Result := (A[MiddleLo] + A[MiddleLo + 1]) / 2.0;
1350+
end;
1351+
1352+
{
1353+
Returns the median of an array of integer values.
1354+
Raises an EArgumentException exception if the array is empty.
1355+
}
1356+
function Median(A: array of Integer): Double; overload;
1357+
var
1358+
MiddleLo: Integer;
1359+
begin
1360+
if System.Length(A) = 0 then
1361+
raise SysUtils.EArgumentException.Create('Array is empty');
1362+
// optimisations for array lengths 1 & 2 to avoid sorting
1363+
if System.Length(A) = 1 then
1364+
Exit(A[0]);
1365+
if System.Length(A) = 2 then
1366+
Exit((A[0] + A[1]) / 2);
1367+
Generics.Collections.TArray.Sort<Integer>(A); // using standard comparer
1368+
MiddleLo := System.Length(A) div 2 - 1;
1369+
if System.Odd(Length(A)) then
1370+
Result := A[MiddleLo + 1]
1371+
else
1372+
Result := (A[MiddleLo] + A[MiddleLo + 1]) / 2;
1373+
end;
1374+
13171375
{
13181376
Returns the middle of three double precision floating point values.
13191377
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /