
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to check number of ways we can move k times and return back to first place in python
Suppose we are at position 0 of n length list, and on each step, we can move right one place or left one place (not exceeding bounds), or stay at the same position. Now if we can take exactly k steps, then we have to find how many unique walks we can take and reach back to index 0. If the answer is very large return it mod 10^9 + 7.
So, if the input is like n = 7 k = 4, then the output will be 9, as the actions are-
- [Right, Right, Left, Left],
- [Right, Left, Right, Left],
- [Stay, Stay, Stay, Stay],
- [Right, Left, Stay, Stay],
- [Stay, Stay, Right, Left],
- [Right, Stay, Stay, Left],
- [Right, Stay, Left, Stay],
- [Stay, Right, Left, Stay],
- [Stay, Right, Stay, Left],
To solve this, we will follow these steps −
- m := 10^9 + 7
- N := length
- Define a function dp() . This will take i, jumps
- if jumps is same as 0, then
- return (true when i is same as 0, otherwise false)
- count := dp(i, jumps - 1)
- if i >= 0, then
- count := count + dp(i + 1, jumps - 1)
- if i <= N - 1, then
- count := count + dp(i - 1, jumps - 1)
- return count
- From the main method do the following:
- return dp(0, n) mod m
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, length, n): MOD = 10 ** 9 + 7 N = length def dp(i, jumps): if jumps == 0: return +(i == 0) count = dp(i, jumps - 1) if i >= 0: count += dp(i + 1, jumps - 1) if i <= N - 1: count += dp(i - 1, jumps - 1) return count return dp(0, n) % MOD ob = Solution() n = 7 k = 4 print(ob.solve(n, k))
Input
7, 4
Output
9
- Related Articles
- Program to find how many ways we can climb stairs (maximum steps at most k times) in Python
- Program to count number of ways we can place nonoverlapping edges to connect all nodes in C++
- Program to count number of ways we can distribute coins to workers in Python
- Program to find number of ways we can decode a message in Python
- Program to count number of ways we can throw n dices in Python
- Program to find number of ways we can split a palindrome in python
- Program to find number of ways we can concatenate words to make palindromes in Python
- Program to find number of ways we can arrange symbols to get target in Python?
- Program to check we can reach end of list by starting from k in Python
- Program to check how many ways we can choose empty cells of a matrix in python
- Program to find number of ways we can select sequence from Ajob Sequence in Python
- Program to count how many ways we can cut the matrix into k pieces in python
- Program to find number of ways we can reach to the next floor using stairs in Python
- Program to check whether we can convert string in K moves or not using Python
- Program to find number of ways we can get n R.s using Indian denominations in Python

Advertisements