{"id":6566,"date":"2024-09-14T00:01:24","date_gmt":"2024-09-14T07:01:24","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6566"},"modified":"2024-09-21T08:03:48","modified_gmt":"2024-09-21T15:03:48","slug":"coding-a-look-and-say-sequence","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6566","title":{"rendered":"Coding a Look-and-Say Sequence"},"content":{"rendered":"<p>You may have seen this sequence on the Internet, in one of those &#8220;guess which number goes next?&#8221; type of posts:<\/p>\n<p><code>1 11 21 1211 ...<\/code><\/p>\n<p>So, which number comes next? I suppose it&#8217;s possible to divine a solution mathematically, but this sequence is known as a <em>Look-and-Say<\/em> sequence.<br \/>\n<!--more--><br \/>\nThe first value is one. Verbally, this value is described as &#8220;one one,&#8221; or 11. The value 11 is described as &#8220;two ones,&#8221; or 21. The value 21 is described as &#8220;one two, one one,&#8221; or 1211. And so it goes.<\/p>\n<p>Despite my math ignorance, mathematicians have examined the Look-and-Say sequence and extrapolated all sorts of interesting things. Well, interesting things for them. You can read more about the ugly details on <a href=\"https:\/\/en.wikipedia.org\/wiki\/Look-and-say_sequence\" rel=\"noopener\" target=\"_blank\">Wikipedia<\/a>. But I want to code this sequence.<\/p>\n<p>My goal is to plow through a value left-to-right, identifying and counting repeating digits. To start, I convert the value into a string. Then I process the string tallying repeating letters and outputting the results. Here is the code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_09_14-Lesson.c\" rel=\"noopener\" target=\"_blank\">2024_09_14-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    const int size = 32;\r\n    char number[size],*n,c;\r\n    int start,count;\r\n\r\n    <span class=\"comments\">\/* fetch the starting value *\/<\/span>\r\n    printf(\"Value: \");\r\n    scanf(\"%d\",&amp;start);\r\n\r\n    <span class=\"comments\">\/* convert `start` to a string *\/<\/span>\r\n    snprintf(number,size,\"%d\",start);\r\n\r\n    <span class=\"comments\">\/* process the string `number` *\/<\/span>\r\n    n = number;        <span class=\"comments\">\/* initialize the pointer *\/<\/span>\r\n    while(*n)        <span class=\"comments\">\/* loop through the string *\/<\/span>\r\n    {\r\n        count = 0;                <span class=\"comments\">\/* digit count *\/<\/span>\r\n        c = *n;                    <span class=\"comments\">\/* save the current digit *\/<\/span>\r\n        while( *n==c )            <span class=\"comments\">\/* loop while the digits match *\/<\/span>\r\n        {\r\n            count++;\r\n            n++;\r\n        }\r\n        <span class=\"comments\">\/* output the digit count and the digit *\/<\/span>\r\n        printf(\"%d%c\",count,c);\r\n    }\r\n    putchar('\\n');\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The outer <em>while<\/em> loop churns through the value stored in string <code>number<\/code>, working it left-to-right. The nested <em>while<\/em> loop repeats as long as the same digit occurs in the string. Variable <code>count<\/code> keeps track of how many times this digit repeats.<\/p>\n<p>When the inner loop stops, meaning that the digit has changed, the value is output, count and  digit. The outer <em>while<\/em> loop then continues plowing through the string.<\/p>\n<p>The result of this code is that a string is output, one that builds as each repeating digit is tallied. Here&#8217;s a sample run:<\/p>\n<p><code>Value: 123<br \/>\n111213<\/code><\/p>\n<p>You must run the program successively to calculate a Look-and-Say series, which is what I eventually want. But my first goal was to get an algorithm down for counting digits and outputting the result. My next step, to process a s sequence, took more effort than I anticipated. My journey continues with <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6573\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Code the solution to one of those Internet mysteries that drives everyone nuts. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6566\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-6566","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6566"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6566\/revisions"}],"predecessor-version":[{"id":6583,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6566\/revisions\/6583"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}