-
Notifications
You must be signed in to change notification settings - Fork 7
/
UIntegerList.pas
55 lines (39 loc) · 918 Bytes
/
UIntegerList.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
unit UIntegerList;
interface
uses System.Generics.Collections;
type
TIntegerList = class(TList<Integer>)
votes, max, min: Integer;
avg, median, proximity: Extended;
procedure CalcStatistics;
end;
implementation
uses System.Generics.Defaults;
procedure TIntegerList.CalcStatistics;
var I, sum: Integer;
begin
votes := Count;
sum := 0;
Sort(TComparer<Integer>.Construct(
function(const Left, Right: Integer): Integer
begin
Result := Left-Right;
end)
);
for I in Self do
begin
if I>max then max := I;
if (min=0) or (I<min) then min := I;
Inc(sum, I);
end;
if Count>0 then
begin
avg := sum / Count;
if Odd(Count) then
median := Items[((Count+1) div 2)-1] //central value
else
median := ( Items[(Count div 2)-1] + Items[Count div 2] ) / 2; //avg of two central values
proximity := min / max * 100;
end;
end;
end.