forked from benhoyt/countwords
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple.pas
70 lines (56 loc) · 1.24 KB
/
simple.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{$MODE DELPHI}
{$IFDEF WINDOWS}
{$APPTYPE CONSOLE}
{$ENDIF}
program Simple;
uses
SysUtils,
Generics.Collections,
Generics.Defaults;
type
TEntry = TPair<String, Integer>;
function Compare(constref Left, Right: TEntry): Integer;
begin
Result := Right.Value - Left.Value;
end;
function CreateComparer(): IComparer<TEntry>;
begin
Result := TComparer<TEntry>.Construct(Compare);
end;
var
Line: String;
WordArray: TStringArray;
WordStr: String;
Key: String;
Dict: TDictionary<String, Integer>;
Count: Integer;
EntryArray: TArray<TEntry>;
Entry: TEntry;
begin
Dict := TDictionary<String, Integer>.Create();
try
while not EOF() do
begin
Readln(Line);
if Length(Line) = 0 then
continue;
WordArray := Line.Split(' ');
for WordStr in WordArray do
begin
if Length(WordStr) = 0 then
continue;
Key := Lowercase(WordStr);
if Dict.TryGetValue(Key, Count) then
Dict[Key] := Count + 1
else
Dict.Add(Key, 1);
end;
end;
EntryArray := Dict.ToArray();
finally
Dict.Free();
end;
TArrayHelper<TEntry>.Sort(EntryArray, CreateComparer());
for Entry in EntryArray do
Writeln(Entry.Key, ' ', Entry.Value);
end.