{"id":25665,"date":"2021-12-04T09:19:05","date_gmt":"2021-12-04T03:49:05","guid":{"rendered":"https:\/\/python-programs.com\/?p=25665"},"modified":"2021-12-04T09:19:05","modified_gmt":"2021-12-04T03:49:05","slug":"python-solution-to-the-ladders-problem","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-solution-to-the-ladders-problem\/","title":{"rendered":"Python Solution to the Ladders Problem"},"content":{"rendered":"
In this session, we will learn about The Ladders Problem, which is a highly interesting problem. Let us first define what we hope to accomplish with this problem.<\/p>\n
In the problem, we are given two inputs: the number of steps and the maximum number of steps that a man can take at one time.<\/p>\n
For example, if the maximum number of steps is 3, At any given time, a person can take one, two, or three steps.<\/p>\n
We must count all of the ways the man can reach the top of the ladder by taking one, two, or three steps at a time.<\/p>\n
The problem may now be solved with standard looping and if-else statements. However, following the Recursion technique is a preferable option. If you are unfamiliar with how Recursion works, I recommend just having a glance over it.<\/p>\n
To answer this problem, we shall seek the smallest problems, i.e. for n = 1 to n = n.<\/p>\n
Consider that the maximum number of steps is three.<\/p>\n
No of steps in a ladder (n)\u00a0<\/strong> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 Total possible Ways\u00a0 \u00a0<\/strong><\/p>\n 1\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Only one possible way<\/p>\n 2\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a02\u00a0 Possible ways – Take 1 step two times or 2 steps 1 time<\/p>\n 3\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 4\u00a0 possible ways – Take 1 step 3 times, Take 2 steps 1 time and 1 step 1\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 time, Take 1 step 1 time and 2 steps 1 time, Take 3 steps 1 time Take\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a01\u00a0 step<\/p>\n n>3\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0(Take 1 step+ Take 2 steps + Take 3 steps ) and find the remaining<\/p>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0F(n) = F(n-1)+F(n-2)+F(n-3)<\/strong><\/p>\n In this case, we are calculating the number of methods for k maximum steps. As a result, we shall compute the values in the order (n-1), (n-2), (n-3) and so on till (n-k).<\/p>\n Finally, we will add together all of the values obtained and return the final result. The code for implementing the same is provided below.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In this session, we will learn about The Ladders Problem, which is a highly interesting problem. Let us first define what we hope to accomplish with this problem. Let us First See, What the Ladders Problem is? In the problem, we are given two inputs: the number of steps and the maximum number of steps …<\/p>\nPython code<\/h4>\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Create a function say total_possibleways() which accepts the given two n, k values\r\n# as the arguments and returns the total possible ways that a man can reach the top\r\n# of the ladder\r\n\r\n\r\ndef total_possibleways(gvn_n, gvn_k):\r\n # Inside the function, check if the given n value is less than 0 using the\r\n # if conditional statement.\r\n\r\n if(gvn_n < 0):\r\n # If it is true, then return 0.\r\n return 0\r\n\r\n # Check if the given n value is equal to 0 (already at the top) using\r\n # the if conditional statement.\r\n if(gvn_n == 0):\r\n # If it is true, then return 1.\r\n return 1\r\n # Similarly, Check if the given n value is less than 3.\r\n if(gvn_n < 3):\r\n # If it is true, then return the given n value.\r\n return gvn_n\r\n\r\n # Take a variable and initialize its value with 0.\r\n rslt = 0\r\n # Loop from 1 to the given k value using the for loop.\r\n for itr in range(1, gvn_k+1):\r\n # Inside the loop, pass gvn_n-iterator value, gvn_k (recursive function)\r\n # as arguments to the total_possibleways() function and it to the above\r\n # declared variable.\r\n # Store it in another variable.\r\n rslt += total_possibleways(gvn_n-itr, gvn_k)\r\n # Return the above result.\r\n return rslt\r\n\r\n\r\n# Give the n value as static input and store it in a variable.\r\ngvn_n = 10\r\n# Give the k value as static input and store it in another variable.\r\ngvn_k = 12\r\n# Pass the given n, k values to the above total_possibleways() function and\r\n# store it in another variable.\r\nfnl_rslt = total_possibleways(gvn_n, gvn_k)\r\n# Print the above result i.e, total possible ways\r\nprint(\"The total possible ways = \", fnl_rslt)\r\n<\/pre>\n
The total possible ways = 512<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Create a function say total_possibleways() which accepts the given two n, k values\r\n# as the arguments and returns the total possible ways that a man can reach the top\r\n# of the ladder\r\n\r\n\r\ndef total_possibleways(gvn_n, gvn_k):\r\n # Inside the function, check if the given n value is less than 0 using the\r\n # if conditional statement.\r\n\r\n if(gvn_n < 0):\r\n # If it is true, then return 0.\r\n return 0\r\n\r\n # Check if the given n value is equal to 0 (already at the top) using\r\n # the if conditional statement.\r\n if(gvn_n == 0):\r\n # If it is true, then return 1.\r\n return 1\r\n # Similarly, Check if the given n value is less than 3.\r\n if(gvn_n < 3):\r\n # If it is true, then return the given n value.\r\n return gvn_n\r\n\r\n # Take a variable and initialize its value with 0.\r\n rslt = 0\r\n # Loop from 1 to the given k value using the for loop.\r\n for itr in range(1, gvn_k+1):\r\n # Inside the loop, pass gvn_n-iterator value, gvn_k (recursive function)\r\n # as arguments to the total_possibleways() function and it to the above\r\n # declared variable.\r\n # Store it in another variable.\r\n rslt += total_possibleways(gvn_n-itr, gvn_k)\r\n # Return the above result.\r\n return rslt\r\n\r\n\r\n# Give the n value as user input using the int(input()) function and store it in a variable.\r\ngvn_n = int(input(\"Enter No of steps(n) = \"))\r\n# Give the k value as user input using the int(input()) function and store it in another variable.\r\ngvn_k = int(input(\"Enter maximum no of steps(k) = \"))\r\n# Pass the given n, k values to the above total_possibleways() function and\r\n# store it in another variable.\r\nfnl_rslt = total_possibleways(gvn_n, gvn_k)\r\n# Print the above result i.e, total possible ways\r\nprint(\"The total possible ways = \", fnl_rslt)\r\n<\/pre>\n
Enter No of steps(n) = 3\r\nEnter maximum no of steps(k) = 4\r\nThe total possible ways = 4<\/pre>\n