-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest18.php
More file actions
43 lines (32 loc) · 866 Bytes
/
test18.php
File metadata and controls
43 lines (32 loc) · 866 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
<?php
function encode($string)
{
$charAux = '';
$count = 0;
$encodedText = '';
for ($i = 0; $i < strlen($string); $i++) {
if ($charAux == $string[$i]) {
$count += 1;
} else {
if ($count > 0) $encodedText = $encodedText . $count . $charAux;
$charAux = $string[$i];
$count = 0;
$count += 1;
}
}
$encodedText = $encodedText . $count . $charAux;
return $encodedText . "\n";
}
function decode($string)
{
$decodedText = '';
for ($i = 0; $i < strlen($string); $i++) {
if (is_numeric($string[$i])) {
$code = str_repeat($string[$i + 1], $string[$i]);
$decodedText = $decodedText . $code;
}
}
return $decodedText . "\n";
}
print_r(encode("AAAABBBCCDAA"));
print_r(decode(encode("AAAABBBCCDAA")));