{"id":33586,"date":"2024-11-01T09:18:11","date_gmt":"2024-11-01T09:18:11","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33586"},"modified":"2024-11-01T11:47:31","modified_gmt":"2024-11-01T11:47:31","slug":"python-coding-test-course-finding-the-number-of-stairs","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33586\/","title":{"rendered":"Python Coding Test Course, Finding the Number of Stairs"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! Today we will take a closer look at one of the algorithm problems that often appears in Python coding tests, &#8220;Counting Stair Numbers&#8221;. Through this problem, we will learn the concept of dynamic programming and the process of solving problems.<\/p>\n<h2>Problem Description<\/h2>\n<p>A stair number is defined by the following rules:<\/p>\n<ul>\n<li>A stair number is composed of digits from 0 to 9.<\/li>\n<li>The digits of two adjacent positions must differ by exactly 1. For example, 234 is valid, but 235 or 123 are not valid.<\/li>\n<li>The first digit of a stair number cannot be 0.<\/li>\n<\/ul>\n<p>Given a natural number <code>N<\/code>, how many stair numbers of length N exist? For example, if N is 2, there are 9 possible stair numbers: 10, 12, 21, 23, 32, 34, 43, 45, 54, 65, 76, 78, 87, 89, 98, totaling 15.<\/p>\n<h2>Input Format<\/h2>\n<p>The first line contains the number of digits in the stair number <code>N<\/code> (1 \u2264 N \u2264 100).<\/p>\n<h2>Output Format<\/h2>\n<p>The first line should output the number of N-digit stair numbers modulo 1,000,000,000.<\/p>\n<h3>Example Input<\/h3>\n<pre>\n2\n<\/pre>\n<h3>Example Output<\/h3>\n<pre>\n15\n<\/pre>\n<h2>Problem Solving Strategy<\/h2>\n<p>This problem can be solved using dynamic programming. Stair numbers can be categorized according to the rules of the problem, and for each digit, we consider the possible number combinations to derive the results. Now, let&#8217;s look at the specific solution process.<\/p>\n<h3>Step 1: State Definition<\/h3>\n<p>To find N-digit stair numbers, we define a DP array. We use <code>dp[n][k]<\/code> to represent the number of stair numbers of length <code>n<\/code> whose last digit is <code>k<\/code>. Here, <code>n<\/code> is the number of digits, and <code>k<\/code> is the last digit (0 to 9).<\/p>\n<h3>Step 2: Initial Condition Setting<\/h3>\n<p>The stair numbers of length 1 are from 1 to 9. Therefore, we set <code>dp[1][0] = 0<\/code> (0 cannot be the first digit), and <code>dp[1][1] = dp[1][2] = ... = dp[1][9] = 1<\/code>.<\/p>\n<h3>Step 3: Deriving the Recursion Formula<\/h3>\n<p>To construct stair numbers of length <code>n<\/code>, we add one digit to stair numbers of length <code>n-1<\/code>. If the last digit is <code>0<\/code>, it can lead to <code>1<\/code>, and if the last digit is <code>9<\/code>, it can lead to <code>8<\/code>. Therefore, we get the following recursion formulas:<\/p>\n<pre>\ndp[n][0] = dp[n-1][1]\ndp[n][k] = dp[n-1][k-1] + dp[n-1][k+1] (1 &lt;= k &lt;= 8)\ndp[n][9] = dp[n-1][8]\n<\/pre>\n<h3>Step 4: Calculating the Final Result<\/h3>\n<p>The N-digit stair numbers can be found by summing up the cases for all digits from 0 to 9. The final result is calculated as <code>sum(dp[N])<\/code>.<\/p>\n<h2>Implementation<\/h2>\n<p>Now, let&#8217;s implement all of this logic in Python code:<\/p>\n<pre><code>def count_stair_numbers(n):\n    # Constant for modular arithmetic\n    MOD = 1000000000\n\n    # Initialize DP table\n    dp = [[0] * 10 for _ in range(n + 1)]\n\n    # When the length is 1\n    for i in range(1, 10):\n        dp[1][i] = 1\n\n    # Fill the DP table\n    for i in range(2, n + 1):\n        dp[i][0] = dp[i - 1][1] % MOD\n        for j in range(1, 9):\n            dp[i][j] = (dp[i - 1][j - 1] + dp[i - 1][j + 1]) % MOD\n        dp[i][9] = dp[i - 1][8] % MOD\n\n    # Calculate the result\n    result = sum(dp[n]) % MOD\n    return result\n\n# User Input\nn = int(input(\"Enter the value of N: \"))\nprint(count_stair_numbers(n))\n<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>Today, through the &#8220;Counting Stair Numbers&#8221; problem, we have understood the basic concepts of dynamic programming and explored the process of solving problems using Python code. I hope this will enhance your algorithm problem-solving skills. Stay tuned for the next tutorial where we will cover another interesting algorithm problem!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! Today we will take a closer look at one of the algorithm problems that often appears in Python coding tests, &#8220;Counting Stair Numbers&#8221;. Through this problem, we will learn the concept of dynamic programming and the process of solving problems. Problem Description A stair number is defined by the following rules: A stair number &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33586\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python Coding Test Course, Finding the Number of Stairs&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[145],"tags":[],"class_list":["post-33586","post","type-post","status-publish","format-standard","hentry","category-python-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Coding Test Course, Finding the Number of Stairs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/33586\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Coding Test Course, Finding the Number of Stairs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! Today we will take a closer look at one of the algorithm problems that often appears in Python coding tests, &#8220;Counting Stair Numbers&#8221;. Through this problem, we will learn the concept of dynamic programming and the process of solving problems. Problem Description A stair number is defined by the following rules: A stair number &hellip; \ub354 \ubcf4\uae30 &quot;Python Coding Test Course, Finding the Number of Stairs&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33586\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:18:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:47:31+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33586\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33586\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Python Coding Test Course, Finding the Number of Stairs\",\"datePublished\":\"2024-11-01T09:18:11+00:00\",\"dateModified\":\"2024-11-01T11:47:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33586\/\"},\"wordCount\":397,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33586\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33586\/\",\"name\":\"Python Coding Test Course, Finding the Number of Stairs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:18:11+00:00\",\"dateModified\":\"2024-11-01T11:47:31+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33586\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33586\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33586\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Coding Test Course, Finding the Number of Stairs\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Coding Test Course, Finding the Number of Stairs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/33586\/","og_locale":"ko_KR","og_type":"article","og_title":"Python Coding Test Course, Finding the Number of Stairs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! Today we will take a closer look at one of the algorithm problems that often appears in Python coding tests, &#8220;Counting Stair Numbers&#8221;. Through this problem, we will learn the concept of dynamic programming and the process of solving problems. Problem Description A stair number is defined by the following rules: A stair number &hellip; \ub354 \ubcf4\uae30 \"Python Coding Test Course, Finding the Number of Stairs\"","og_url":"https:\/\/atmokpo.com\/w\/33586\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:18:11+00:00","article_modified_time":"2024-11-01T11:47:31+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33586\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33586\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Python Coding Test Course, Finding the Number of Stairs","datePublished":"2024-11-01T09:18:11+00:00","dateModified":"2024-11-01T11:47:31+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33586\/"},"wordCount":397,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33586\/","url":"https:\/\/atmokpo.com\/w\/33586\/","name":"Python Coding Test Course, Finding the Number of Stairs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:18:11+00:00","dateModified":"2024-11-01T11:47:31+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33586\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33586\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33586\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Python Coding Test Course, Finding the Number of Stairs"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33586","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=33586"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33586\/revisions"}],"predecessor-version":[{"id":33587,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33586\/revisions\/33587"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}