Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
18
Task/Run-length-encoding/PHP/run-length-encoding.php
Normal file
18
Task/Run-length-encoding/PHP/run-length-encoding.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
function encode($str)
|
||||
{
|
||||
return preg_replace_callback('/(.)\1*/', function ($match) {
|
||||
return strlen($match[0]) . $match[1];
|
||||
}, $str);
|
||||
}
|
||||
|
||||
function decode($str)
|
||||
{
|
||||
return preg_replace_callback('/(\d+)(\D)/', function($match) {
|
||||
return str_repeat($match[2], $match[1]);
|
||||
}, $str);
|
||||
}
|
||||
|
||||
echo encode('WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'), PHP_EOL;
|
||||
echo decode('12W1B12W3B24W1B14W'), PHP_EOL;
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue