-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_histogram_unit.pas
More file actions
56 lines (47 loc) · 993 Bytes
/
errors_histogram_unit.pas
File metadata and controls
56 lines (47 loc) · 993 Bytes
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
{
6 kyu
Errors : histogram
https://www.codewars.com/kata/59f44c7bd4b36946fd000052
}
unit errors_histogram_unit;
{$mode objfpc}{$H+}
interface
function Hist(strng: string): string;
implementation
uses
SysUtils,
StrUtils;
function HistLine(c: char; cnt: integer): string;
var
scnt: string;
begin
if cnt > 0 then
begin
scnt := IntToStr(cnt);
Result := c + ' ' + scnt + DupeString(' ', 6 - Length(scnt)) +
DupeString('*', cnt) + '\r';
end
else
Result := '';
end;
function Hist(strng: string): string;
var
c: char;
countu: integer = 0;
countw: integer = 0;
countx: integer = 0;
countz: integer = 0;
begin
for c in strng do
case c of
'u': countu += 1;
'w': countw += 1;
'x': countx += 1;
'z': countz += 1;
end;
Result := HistLine('u', countu) + HistLine('w', countw) +
HistLine('x', countx) + HistLine('z', countz);
if Length(Result) >= 2 then
Result := Copy(Result, 1, Length(Result) - 2);
end;
end.