subject

Programming question: Given a number, eg. 8, try to calculate and output a list/array of

that in each number's binary form - how many '1' in each number? (should not use string. count('1') in the Python. Efficiency is most important!)

Example: number is 8.

Expected output is - [0, 1, 1, 2, 1, 2, 2, 3, 1]

See some good code snippet (listed below), but not quite understand it? Can you help explain?

```code:

from typing import List

def countBits(num: int) -> List[int]:

""" count all numbers: from 0 to num (eg. 8)

-each number's binary bit in '1':

>>> countBits(8)

[0, 1, 1, 2, 1, 2, 2, 3, 1]

"""

res = [0]

while len(res) <= num:

for i in res[:num+1 - len(res)]: # :8 - 7- 6 -5 1

res += [i + 1]

return res

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 09:30
My mom and i are moving and we don’t have wifi for the next week, i want to know if using a using a hotspot with unlimited data is better than using regular wifi. i’m considering cost, speed, and data sacrifices.
Answers: 1
question
Computers and Technology, 22.06.2019 12:40
How do i get the most points, without any effort?
Answers: 2
question
Computers and Technology, 24.06.2019 03:30
Auniform resource locator (url) is a formatted string of text that web browsers, email applications, and other software programs use to identify a particular resource on the internet. true false
Answers: 2
question
Computers and Technology, 24.06.2019 16:00
Your is an example of personal information that you should keep private.
Answers: 1
You know the right answer?
Programming question: Given a number, eg. 8, try to calculate and output a list/array of

Questions
Questions on the website: 13722367