CodeFights - Count Bits - Solution

Problem
Implement the missing code, denoted by ellipses. You may not modify the pre-existing code.
Implement a function that, given an integer n, uses a specific method on it and returns the number of bits in its binary representation.
Note: in this task and most of the following tasks you will be given a code snippet with some part of it replaced by the ellipsis (...). Only this part is allowed to be changed.
Example
For n = 50, the output should be
countBits(n) = 6.
5010 = 1100102, a number that consists of 6 digits. Thus, the output should be 6.
Input/Output
  • [execution time limit] 4 seconds (py)
  • [input] integer n
    A positive integer.
    Guaranteed constraints:
    1 ≤ n ≤ 109.
  • [output] integer
    The number of bits in binary representation of n.
Solution
It is expected to only replace the ellipsis with the code that would give you the bit length of the given integer. 

def countBits(n):
     return n.bit_length()

9 comments:

  1. Softofree
    Get your favorite mac & windows applications on your computer or macbook. Free games & applications are available here to download softofree

    ReplyDelete
    Replies
    1. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai

      Delete