Answer to Question #308489 in Python for Michael

Question #308489

Kalinjar Fort is a popular tourist place in Bundelkhand, Utter Pradesh. Everyday peoples registered to visit Fort as token provided based on first come basis. Everyday limited numbers of peoples are allowed to visit. The management selects a number N1 randomly every day and generates another number N2 as count of the total number of set bits (i.e., total no. of 1s) in binary representation of all numbers from 1 to N1. For example, N1=3 then N2=4 [1(01) +2(10) +3(11)]. So, write a python module CountBit.py to find N2 using N1. You need to import this module , define a function Get_Token(n) which takes a number and return value as shown in example by calling the appropriate function implemented in CountBit.py module.

 

 

Example-1

Example-2

Example-3

Input:

3

 

Output:

4

 

Input:

7

 

Output:

12

Input:

8

 

Output:

13

 



1
Expert's answer
2022-03-09T12:32:39-0500
import random


N1 = random.randint(0,10)
N2=0
for r in range(0,N1+1):
    binary = bin(r)
    bits_1 = [ones for ones in binary[2:] if ones=='1'] 
    N2 = N2 + len(bits_1)
print("N1 = ",N1)
print("N2 = ",N2)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS