Skip to content

Instantly share code, notes, and snippets.

@NimaGhaedsharafi
Last active May 12, 2021 07:31
Show Gist options
  • Save NimaGhaedsharafi/428aec62d9baee45b6f3a3b22e205687 to your computer and use it in GitHub Desktop.
Save NimaGhaedsharafi/428aec62d9baee45b6f3a3b22e205687 to your computer and use it in GitHub Desktop.
<?php
$input = 10;
$last = "1";
for ($i = 0; $i < $input; $i++) {
echo $last . PHP_EOL;
$last = lookNSay($last);
}
function lookNSay($last)
{
$row = "";
$cur = $last[0];
$count = 1;
for ($i = 1; $i <= strlen($last); $i++) {
if ($i == strlen($last) || $cur != $last[$i]) {
$row .= $count . $cur;
$cur = $last[$i] ?? $last[$i-1];
$count = 1;
} else {
$count++;
}
}
return $row;
}
/**
1
11
21
1211
111221
312211
13112221
1113213211
31131211131221
13211311123113112211
*/
<?php
$input = 10;
$result[0] = "1";
echo $result[0] . PHP_EOL;
for ($i = 1; $i < $input; $i++) {
$result[$i] = lookNSay($result[$i-1], $i);
echo $result[$i] . PHP_EOL;
}
function lookNSay($last, $c)
{
$row = "";
$cur = $last[0];
$count = 1;
for ($i = 1; $i <= strlen($last); $i++) {
if ($i == strlen($last) || $cur != $last[$i]) {
$row .= $count . $cur;
$cur = $last[$i] ?? $last[$i-1];
$count = 1;
} else {
$count++;
}
}
return $row;
}
/**
1
11
21
1211
111221
312211
13112221
1113213211
31131211131221
13211311123113112211
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment