-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaries_unit.pas
More file actions
61 lines (51 loc) · 1 KB
/
binaries_unit.pas
File metadata and controls
61 lines (51 loc) · 1 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
{
6 kyu
Binaries
https://www.codewars.com/kata/5d98b6b38b0f6c001a461198
}
unit binaries_unit;
{$mode objfpc}{$H+}
interface
function Code(str: string): string;
function Decode(str: string): string;
implementation
uses
SysUtils;
const
encode_tab: array[0..9] of string = (
'10', '11', '0110', '0111', '001100',
'001101', '001110', '001111', '00011000', '00011001'
);
delta: integer = 48;
function Code(str: string): string;
var
i: integer;
begin
Result := '';
for i := 1 to Length(str) do
Result := Result + encode_tab[Ord(str[i]) - delta];
end;
function Decode(str: string): string;
var
pos, l, prefixlen: integer;
s: string;
begin
Result := '';
l := Length(str);
pos := 1;
while pos <= l do
begin
prefixlen := 0;
while str[pos] = '0' do
begin
Inc(pos);
Inc(prefixlen);
end;
Inc(pos);
Inc(prefixlen);
s := Copy(str, pos, prefixlen);
Result := Result + Chr(delta + StrToInt('%' + s));
pos := pos + prefixlen;
end;
end;
end.