- 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 count number of ways to win at most k consecutive games in Python

Suppose we have two numbers n and k. Here n represents the number of games we are going to play. We have to find in how many ways we can win k or fewer games consecutively. If the answer is too large then mod the result by 10^9 + 7.

So, if the input is like n = 3 k = 2, then the output will be 7, as the possible ways in which we can win 2 or fewer times consecutively, are ["LLL", "WLL", "LWL", "LLW", "WWL", "LWW", "WLW"]

To solve this, we will follow these steps −

- m := 1^9 + 7
- Define a function dp() . This will take i, K
- if i >= n or K > k, then
- return true when K <= k, otherwise false

- return dp(i + 1, 0) mod m + dp(i + 1, K + 1) mod m
- From the main method, do the following −
- return dp(0, 0) mod m

## Example

Let us see the following implementation to get better understanding −

def solve(n, k): m = 1**9 + 7 def dp(i, K): if i >= n or K > k: return K <= k return dp(i + 1, 0) % m + dp(i + 1, K + 1) % m return dp(0, 0) % m n = 4 k = 2 print(solve(n, k))

## Input

4, 2

## Output

5

- Related Articles
- Program to count the number of ways to distribute n number of candies in k number of bags in Python
- Program to find number of sequences after adjacent k swaps and at most k swaps in Python
- Program to find how many ways we can climb stairs (maximum steps at most k times) in Python
- Python Program to Count number of binary strings without consecutive 1’
- Program to find sum of rectangle whose sum at most k in Python
- Count ways to express a number as sum of consecutive numbers in C++
- Program to find most occurring number after k increments in python
- Program to count number of ways we can distribute coins to workers in Python
- Program to count number of paths whose sum is k in python
- Program to count number of ways we can throw n dices in Python
- Count number of ways to partition a set into k subsets in C++
- Program to find number of consecutive subsequences whose sum is divisible by k in Python
- Program to count number of sublists with exactly k unique elements in Python
- Program to find maximum sum by performing at most k negate operations in Python
- Python program to count pairs for consecutive elements

Advertisements