-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathathletic_association_statistics_unit.pas
More file actions
75 lines (65 loc) · 1.53 KB
/
athletic_association_statistics_unit.pas
File metadata and controls
75 lines (65 loc) · 1.53 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{
6 kyu
Statistics for an Athletic Association
https://www.codewars.com/kata/55b3425df71c1201a800009c
}
unit athletic_association_statistics_unit;
{$mode objfpc}{$H+}
interface
function Stat(s: string): string;
implementation
uses
Generics.Collections,
StrUtils,
SysUtils;
function HMStoSec(hms: string): integer;
var
slist: array of string;
nlist: array of integer = nil;
i: integer;
begin
hms := Trim(hms);
slist := SplitString(hms, '|');
SetLength(nlist, Length(slist));
for i := 0 to High(slist) do
nlist[i] := StrToInt(slist[i]);
Result := nlist[2] + 60 * (nlist[1] + 60 * nlist[0]);
end;
function SectoHMS(sec: integer): string;
var
h, m, s, r: integer;
begin
h := sec div 3600;
r := sec - 3600 * h;
m := r div 60;
s := r - m * 60;
Result := Format('%.02d|%.02d|%.02d', [h, m, s]);
end;
function Stat(s: string): string;
var
list: array of string;
secs: array of integer = nil;
i, l, range, average, median: integer;
begin
if s = '' then
Exit('');
list := SplitString(s, ',');
l := Length(list);
SetLength(secs, l);
average := 0;
for i := 0 to l - 1 do
begin
secs[i] := HMStoSec(list[i]);
average += secs[i];
end;
average := average div l;
specialize TArrayHelper<integer>.Sort(secs);
range := secs[l - 1] - secs[0];
if Odd(l) then
median := secs[(l - 1) div 2]
else
median := (secs[(l - 1) div 2] + secs[((l - 1)) div 2 + 1]) div 2;
Result := Format('Range: %s Average: %s Median: %s',
[SectoHMS(range), SectoHMS(average), SectoHMS(median)]);
end;
end.