Python Solution to the Ladders Problem

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 that a man can take at one time.

For example, if the maximum number of steps is 3, At any given time, a person can take one, two, or three steps.

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.

Here Comes the Solution

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.

To answer this problem, we shall seek the smallest problems, i.e. for n = 1 to n = n.

Consider that the maximum number of steps is three.

No of steps in a ladder (n)                     Total possible Ways   

1                                                             Only one possible way

2                                                             2  Possible ways – Take 1 step two times or 2 steps 1 time

3                                                            4  possible ways – Take 1 step 3 times, Take 2 steps 1 time and 1 step 1                                                                                          time, Take 1 step 1 time and 2 steps 1 time, Take 3 steps 1 time Take                                                                                     1  step

n>3                                                       (Take 1 step+ Take 2 steps + Take 3 steps ) and find the remaining

                                                             F(n) = F(n-1)+F(n-2)+F(n-3)

Python code

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).

Finally, we will add together all of the values obtained and return the final result. The code for implementing the same is provided below.

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Create a function say total_possibleways() which accepts the given two n, k values as the arguments and returns the total possible ways that a man can reach the top of the ladder.
  • Inside the function, check if the given n value is less than 0 using the if conditional statement.
  • If it is true, then return 0.
  • Check if the given n value is equal to 0 (already at the top) using the if conditional statement.
  • If it is true, then return 1.
  • Similarly, Check if the given n value is less than 3.
  • If it is true, then return the given n value.
  • Take a variable and initialize its value with 0.
  • Loop from 1 to the given k value using the for loop.
  • Inside the loop, pass gvn_n-iterator value, gvn_k (recursive function) as arguments to the total_possibleways() function and it to the above declared variable.
  • Store it in another variable.
  • Return the above result.
  • Give the n value as static input and store it in a variable.
  • Give the k value as static input and store it in another variable.
  • Pass the given n, k values to the above total_possibleways() function and store it in another variable.
  • Print the above result i.e, total possible ways.
  • The Exit of the Program.

Below is the implementation:

# Create a function say total_possibleways() which accepts the given two n, k values
# as the arguments and returns the total possible ways that a man can reach the top
# of the ladder


def total_possibleways(gvn_n, gvn_k):
    # Inside the function, check if the given n value is less than 0 using the
    # if conditional statement.

    if(gvn_n < 0):
        # If it is true, then return 0.
        return 0

    # Check if the given n value is equal to 0 (already at the top) using
        # the if conditional statement.
    if(gvn_n == 0):
        # If it is true, then return 1.
        return 1
        # Similarly, Check if the given n value is less than 3.
    if(gvn_n < 3):
        # If it is true, then return the given n value.
        return gvn_n

    # Take a variable and initialize its value with 0.
    rslt = 0
    # Loop from 1 to the given k value using the for loop.
    for itr in range(1, gvn_k+1):
        # Inside the loop, pass gvn_n-iterator value, gvn_k (recursive function)
        # as arguments to the total_possibleways() function and it to the above
        # declared variable.
        # Store it in another variable.
        rslt += total_possibleways(gvn_n-itr, gvn_k)
    # Return the above result.
    return rslt


# Give the n value as static input and store it in a variable.
gvn_n = 10
# Give the k value as static input and store it in another variable.
gvn_k = 12
# Pass the given n, k values to the above total_possibleways() function and
# store it in another variable.
fnl_rslt = total_possibleways(gvn_n, gvn_k)
# Print the above result i.e, total possible ways
print("The total possible ways = ", fnl_rslt)

Output:

The total possible ways =  512

Method #2: Using Built-in Functions (User Input)

Approach:

  • Create a function say total_possibleways() which accepts the given two n, k values as the arguments and returns the total possible ways that a man can reach the top of the ladder.
  • Inside the function, check if the given n value is less than 0 using the if conditional statement.
  • If it is true, then return 0.
  • Check if the given n value is equal to 0 (already at the top) using the if conditional statement.
  • If it is true, then return 1.
  • Similarly, Check if the given n value is less than 3.
  • If it is true, then return the given n value.
  • Take a variable and initialize its value with 0.
  • Loop from 1 to the given k value using the for loop.
  • Inside the loop, pass gvn_n-iterator value, gvn_k (recursive function) as arguments to the total_possibleways() function and it to the above declared variable.
  • Store it in another variable.
  • Return the above result.
  • Give the n value as user input using the int(input()) function and store it in a variable.
  • Give the k value as user input using the int(input()) function and store it in another variable.
  • Pass the given n, k values to the above total_possibleways() function and store it in another variable.
  • Print the above result i.e, total possible ways.
  • The Exit of the Program.

Below is the implementation:

# Create a function say total_possibleways() which accepts the given two n, k values
# as the arguments and returns the total possible ways that a man can reach the top
# of the ladder


def total_possibleways(gvn_n, gvn_k):
    # Inside the function, check if the given n value is less than 0 using the
    # if conditional statement.

    if(gvn_n < 0):
        # If it is true, then return 0.
        return 0

    # Check if the given n value is equal to 0 (already at the top) using
        # the if conditional statement.
    if(gvn_n == 0):
        # If it is true, then return 1.
        return 1
        # Similarly, Check if the given n value is less than 3.
    if(gvn_n < 3):
        # If it is true, then return the given n value.
        return gvn_n

    # Take a variable and initialize its value with 0.
    rslt = 0
    # Loop from 1 to the given k value using the for loop.
    for itr in range(1, gvn_k+1):
        # Inside the loop, pass gvn_n-iterator value, gvn_k (recursive function)
        # as arguments to the total_possibleways() function and it to the above
        # declared variable.
        # Store it in another variable.
        rslt += total_possibleways(gvn_n-itr, gvn_k)
    # Return the above result.
    return rslt


# Give the n value as user input using the int(input()) function and store it in a variable.
gvn_n = int(input("Enter No of steps(n) =  "))
# Give the k value as user input using the int(input()) function and store it in another variable.
gvn_k = int(input("Enter maximum no of steps(k) = "))
# Pass the given n, k values to the above total_possibleways() function and
# store it in another variable.
fnl_rslt = total_possibleways(gvn_n, gvn_k)
# Print the above result i.e, total possible ways
print("The total possible ways = ", fnl_rslt)

Output:

Enter No of steps(n) = 3
Enter maximum no of steps(k) = 4
The total possible ways = 4