Created
April 12, 2012 08:48
-
-
Save h2rd/2365677 to your computer and use it in GitHub Desktop.
Алгоритм генерації стрічок 1 | 11 | 21 | 1211 | 111221 etc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Generator { | |
| private $start = '1'; | |
| function __construct($start = null) { | |
| if ($start != null) { | |
| $this->start = (string) $start; | |
| } | |
| } | |
| public function ff($string, $r) { | |
| $char = $string[0]; | |
| $count = 0; | |
| $index = 0; | |
| for ($i = 0, $total = strlen($string); $i < $total; $i++) { | |
| if ($string[$i] != $char) { | |
| $index = $i; | |
| break; | |
| } | |
| $count ++; | |
| } | |
| if ($index == 0) { | |
| $index = $total; | |
| } | |
| return array( | |
| $count . $char, | |
| ($index + $r) | |
| ); | |
| } | |
| public function run($n = 6) { | |
| $start = $this->start; | |
| while ($n > 0) { | |
| $this->log[] = $start; | |
| $end = ''; | |
| $i = 0; | |
| $total = strlen($start); | |
| while ($i < $total) { | |
| list($string, $i) = $this->ff(substr($start, $i), $i); | |
| $end .= $string; | |
| } | |
| $start = $end; | |
| $n --; | |
| } | |
| return $this; | |
| } | |
| public function getLog() { | |
| foreach ($this->log as $number => $value) { | |
| echo ($number + 1), " - ", $value, "\n"; | |
| } | |
| } | |
| } | |
| $g = new Generator(3); | |
| $g->run() | |
| ->getLog(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment