# HackerRank Solution: Python Diwali Lights [2 Methods]

Written By - Bashir Alam

## Question: Diwali Lights [Python Fundamentals]

On the eve of Diwali, Hari is decorating his house with a serial light bulb set. The serial light bulb set has N bulbs placed sequentially on a string which is programmed to change patterns every second. If at least one bulb in the set is on at any given instant of time, how many different patterns of light can the serial light bulb set produce?

Note: Lighting two bulbs *-* is different from **-

Input Format

The first line contains the number of test cases T, T lines follow.
Each line contains an integer N, the number of bulbs in the serial light bulb set.

Output Format

Print the total number of patterns modulo 105

Constraints

1 <= T <= 1000
0< N < 104

Sample Input

``````2
1
2``````

Sample Output

``````1
3``````

Explanation

Case 1: 1 bulb can be lit in only 1 way.
Case 2: 2 bulbs can be lit in -*, *-, ** i.e. 3 ways.

## Possible solutions

Now let us discuss the possible solutions to the problem. We already have been given the following code:

``````import math
import os
import random
import re
import sys

#
# Complete the 'lights' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts INTEGER n as parameter.
#

def lights(n):

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

t = int(input().strip())

for t_itr in range(t):
n = int(input().strip())

result = lights(n)

fptr.write(str(result) + '\n')

fptr.close()
``````

We just need to write our code in the `lights()` function to solve the problem.

### Solution-1: Using the `pow()` method

Let us use the `pow()` method to solve the problem. The `pow()` method in Python is used to find the power of the number.

``````import math
import os
import random
import re
import sys

def lights(n):
return pow(2, n, int(1E5)) - 1

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

t = int(input().strip())

for t_itr in range(t):
n = int(input().strip())

result = lights(n)

fptr.write(str(result) + '\n')

fptr.close()
``````

The `lights()` method takes in a single argument "`n`". The function uses the built-in `pow()` function to raise `2` to the power of "`n`", and then uses the modulo operator (`%`), with the value int(1E5) as the second argument, to take the remainder of the result when divided by int(1E5). Finally, the function subtracts 1 from this remainder and returns the final value. In short, it's a function that takes an input n, calculates `2^n` and then performs a modulus operation with `10^5`, and then subtracts 1 from the result

### Solution-2: Alternative solution

In Python, we can also use double asterisk symbols instead of the `pow()` method as shown below:

``````import math
import os
import random
import re
import sys

def lights(n):
return (2**n-1)%100000
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

t = int(input().strip())

for t_itr in range(t):
n = int(input().strip())

result = lights(n)

fptr.write(str(result) + '\n')

fptr.close()
``````

The solution provides a different way to solve the problem from the previous one in the way it calculates the final value. Instead of using the built-in pow() function, it uses the exponential operator (`**`) to raise 2 to the power of "n" and then subtracts 1 from the result. This result is then taken modulus of 100000. In the first code snippet, it uses pow(2, n, int(1E5)) which also performs the same operation, but in a different way. Both codes are performing the same operation, but the first one uses the pow() function and modulus operator, while the second one uses the exponential operator and modulus operator.

## Summary

In this short article, we discussed how we can solve the Diwali lights problem on Hacker Rank. The problem is of medium level and belongs to the mathematics category.  We solved the problem using two different methods and explained each of them.

Problem on Hacker Rank: Diwali Lights [Python Fundamentals]

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation. For any other feedbacks or questions you can either use the comments section or contact me form.