{"id":1514,"date":"2021-11-13T15:00:07","date_gmt":"2021-11-13T15:00:07","guid":{"rendered":"https:\/\/www.antpace.com\/blog\/?p=1514"},"modified":"2025-08-25T17:54:32","modified_gmt":"2025-08-25T17:54:32","slug":"look-and-say-in-php","status":"publish","type":"post","link":"https:\/\/www.antpace.com\/blog\/look-and-say-in-php\/","title":{"rendered":"Look-and-Say in PHP"},"content":{"rendered":"<p>The look-and-say sequence is a series of integers. It can grow indefinitely. <strong>It is generated by reciting a number phonetically, and writing what you spoke numerically.<\/strong> Its popularity is attributed to famed cryptographer Robert Morris. It was introduced by mathematician John Conway. It looks like this:<\/p>\n<pre>1\n11\n21\n1211\n111221\n312211\n13112221\n<\/pre>\n<p>The first line would be pronounced as &#8220;one 1&#8221;, and then written as &#8220;11&#8221; on the second line. That record would be spoken as &#8220;two 1&#8217;s&#8221;,\u00a0 giving us the third line &#8220;21&#8221;. The greatest individual symbol you&#8217;ll ever find in this consecution is a 3.<\/p>\n<p>This topic has lots of trivia, variations, and history that could be dug up and expounded upon. Here, I&#8217;ll explain a solution written in PHP to produce this chain of numerals. The input will be the count of how many lines, or iterations, in the series to generate. Below is the code:<\/p>\n<pre>&lt;?php\n\necho \"Count And Say: \\n\";\n\nfunction countAndSay($count=0){\n\t$value = 1; \/\/ initial seed\n\tfor($i=1;$i&lt;=$count;$i++){\n\t\techo $value . \"\\n\";\n\t\t$value = calcOutput($value);\n\t}\n}\nfunction calcOutput($value){\n\t$value = \"$value\";  \/\/ change it into a string, so we can iterate over each character\n\t$current = $value[0]; \/\/ first character\n\t$count = 1;\n        $return = '';\n\tfor ($i = 1; $i &lt;= strlen($value); $i++) { \/\/ keep going until we get through the whole string\n\t\tif ($current != $value[$i] || $i == strlen($value)) { \/\/ found a different character, or end of the input string\n\t\t\t$return .= \"$count$current\";\n\t\t\t$count = 1; \/\/ reset count\n\t\t\t$current = $value[$i]; \/\/ set new current character\n\t\t} else {\n\t\t\t$count++;\n\t\t}\n\t}\n\treturn $return;\n}\n\ncountAndSay(7);\n\necho \"\\n\\n\";\n\n?&gt;\n<\/pre>\n<p>I separated my code into two functions. I think this is the best approach. As an exercise, see if you can figure out how to refactor it into one. This could help you to internalize the logic as you write it out for yourself.<\/p>\n<p>The initial seed value is &#8220;1&#8221;, and that is hard-coded at the top. The for-loop iterates based on the count input parameter. That means the code circles back and re-runs, with updated values, until its internal count (represented by the variable <em>$i <\/em>) matches the <em>$count<\/em> variable passed into <em>countAndSay($count)<\/em>.<\/p>\n<p>The code that we loop over outputs the current sequence value (starting with 1) as its own line (&#8220;<em>\\n<\/em>&#8221; will output a new line in most programming languages) , and then calculates the next. The function that determines the next line of output, <em>calcOutput($value)<\/em>, takes the current value as an argument.<\/p>\n<p>The first thing we do is cast the integer value passed along into a string. This lets us refer to each character by index &#8211; starting at zero &#8211; and save it to a variable <em>$current<\/em>. We start a new <em>$count<\/em>, to keep track of how many times we see the same digit.<\/p>\n<p>The next for-loop executes for the length of the <em>$value<\/em> string. On each loop, we check if the\u00a0<em>$current<\/em> character we saved matches the subsequent one in that <em>$value<\/em> string. It is again referenced by index, this time based on the for-loop&#8217;s iteration count represented by the variable <em>$i<\/em>.<\/p>\n<p>If it does match, one is added to the <em>$count <\/em>variable that is keeping track of how many times we see the same character is a row. If it doesn&#8217;t match (or we&#8217;ve reached the end of the input), the $<em>count<\/em> and\u00a0<em>$current<\/em> number are concatenated to the\u00a0<em>$return\u00a0<\/em>element. At that point, the\u00a0<em>$count\u00a0<\/em>is reset to 1, and the <em>$current<\/em> value is updated.<\/p>\n<p>Writing an algorithm to generate the look-and-say (also known as, count-and-say) sequence is a common coding puzzle. You might run into it during a job interview as a software engineer. As practice, see if you can simplify my example code, or even write it in a different programming language than PHP.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The look-and-say sequence is a series of integers. It can grow indefinitely. It is generated by reciting a number phonetically, and writing what you spoke numerically. Its popularity is attributed to famed cryptographer Robert Morris. It was introduced by mathematician John Conway. It looks like this: 1 11 21 1211 111221 312211 13112221 The first &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.antpace.com\/blog\/look-and-say-in-php\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Look-and-Say in PHP&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":3233,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[7,32,92,119,120],"class_list":["post-1514","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology","tag-algorithms","tag-crypto","tag-php","tag-software","tag-software-engineer"],"_links":{"self":[{"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/posts\/1514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/comments?post=1514"}],"version-history":[{"count":1,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/posts\/1514\/revisions"}],"predecessor-version":[{"id":3234,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/posts\/1514\/revisions\/3234"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/media\/3233"}],"wp:attachment":[{"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/media?parent=1514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/categories?post=1514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/tags?post=1514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}