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: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 10:30
Aconstruction company is creating a powerpoint presentation describing how they calculate costs during each construction step. they plan to email this presentation to clients. the individual clients will be watching the presentation slide show on their own personal computers. what is the most important formatting step the company should take to make the text readable and pleasing to the eye?
Answers: 2
question
Computers and Technology, 23.06.2019 17:00
What are the 12 colors of the spectrum called?
Answers: 1
question
Computers and Technology, 26.06.2019 02:20
Technician a says that oil for the rod bearings comes from splash off the crankshaft. technician b says that lubrication to the main bearings is fed through the main oil gallery in the block. who is correct?
Answers: 1
question
Computers and Technology, 26.06.2019 03:00
According to the rules of economics, all resources arein their supply. the wants and needs of people are. the more scarce a resource is, the people are willing to pay for it. the principle of scarcity forces people to.
Answers: 1
You know the right answer?
Programming question: Given a number, eg. 8, try to calculate and output a list/array of
that...
Questions
question
Mathematics, 18.07.2019 04:30
Questions on the website: 13722363