Answer to Question #308944 in Python for Michael

Question #308944

 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 in RollNo_W12B_2.py. In RollNo_W12B_2.py, 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:

12Input:

8

 

Output:

13


1
Expert's answer
2022-03-12T04:55:39-0500
# CountBit.py


def Num_Bits(x):
    cnt = 0
    while x > 0:
        if x%2 == 1:
            cnt += 1
        x //= 2
    return cnt


def Num_Visits(N1):
    N2 = 0
    for i in range(N1):
        N2 += Num_Bits(i+1)
    return N2


# RollNo_W12B_2.py


from CountBit import Num_Visits


def Get_Token(n):
    return Num_Visits(n)


def main():
    N1 = int(input())
    N2 = Get_Token(N1)
    print(N2)


if __name__ == '__main__':
    main()

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