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]
"""
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

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 02:00
Consider how gaming consoles initially relied on joysticks and joypads and then made the switch to modern gaming controls, which include analog sticks, buttons and switches, touch controls, accelerometers, motion controls, etc. name at least two kinds of gaming experiences that are possible with these new control devices but were not possible on original joysticks. explain how new technologies made this newer game style possible.
Answers: 1
question
Computers and Technology, 22.06.2019 06:00
In outlook how can cherie look at the details of an event that appears on the month view of her calendar? check all that apply. by switching to the detail view by switching to the week view by switching to the day view by right-clicking on the event by double-clicking on the event by highlighting the event
Answers: 2
question
Computers and Technology, 22.06.2019 16:20
Consider the following statements, then select one of the answers below: the signal() function shown below registers "sig_handler()" as the signal handler function for the sigkill signal, without the complexity of using when the sigkill signal is sent to a process running this code, by a user typing "kill -kill ", where the correct process id is used for to target the process, sig_handler() will be executed.
Answers: 1
question
Computers and Technology, 24.06.2019 18:30
How does the use of e-mail benefit business communications? it can be sent at any time. it is faster than regular mail. it improves writing skills. it is less expensive than using a courier. it reduces the need for proofreading.
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
Questions on the website: 13722361