Skip to content

Instantly share code, notes, and snippets.

@h2rd
Created April 12, 2012 08:48
Show Gist options
  • Select an option

  • Save h2rd/2365677 to your computer and use it in GitHub Desktop.

Select an option

Save h2rd/2365677 to your computer and use it in GitHub Desktop.
Алгоритм генерації стрічок 1 | 11 | 21 | 1211 | 111221 etc
<?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