Computers and Technology

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]
"""
def countBits(num: int) -> List[int]:
res = [0]
while len(res) <= num:
for i in res[:num+1 - len(res)]: # :8 - 7- 6 -5 1
res += [i + 1]
return res

answer
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 05:00, 420420blazee
Are special characters that allow you to search for multiple words at the same time.
Answers: 2
image
Computers and Technology, 22.06.2019 16:30, mesposito
Technician a says that a dry sump system uses no oil storage sump under the engine. technician b says that a wet sump system uses no oil storage sump under the engine. who is correct?
Answers: 3
image
Computers and Technology, 23.06.2019 00:30, Thisisdifinite
Which of the following would you find on a network
Answers: 3
image
Computers and Technology, 23.06.2019 11:30, talyku7131
Me dangers of social media and the internetexplain what each means: 1) social media and phones have become an addiction.2) outside people have access to you all the time.3) cyberstalking4) cyberbullying5) catphishing6) viruses7) identity theft8) credit card fraud9) hacking10) money schemes
Answers: 1
Do you know the correct answer?
Programming question: Given a number, eg. 8, try to calculate and output a list/array of
that...

Questions in other subjects: