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 05:00
Which two editions of windows 7 support 64 bit cpus? choose two out of professional, business, starter, or home premium.
Answers: 1
question
Computers and Technology, 22.06.2019 06:50
Type the correct answer in the box. spell all words correctly. which view of report creation allows you to customize the report before the database program creates it? creating a report in allows you to customize the report before the database program creates it. pl asap
Answers: 1
question
Computers and Technology, 22.06.2019 21:30
Elements such as fonts colors visual structure graphics and the interface of a web page should complement each other to ensure blank
Answers: 3
question
Computers and Technology, 23.06.2019 00:10
Write a function so that the main0 code below can be replaced by the simpler code that calls function mphandminutes tomiles0. original main0 int main) l double milesperhour-70.0; double minutestraveled = 100.0; double hourstraveled; double milestraveled; hourstraveled = minutestraveled / 60.0; milestraveled = hourstraveled * milesperhour; cout < "miles" 2 using namespace std; 4 /* your solution goes here/ 6 int maino 1 test passed 7 double milesperhour 70.0 all tests passed 8 double minutestraveled 100.0; 10 cout < < "miles: " < < mphandminutestomiles(milesper-hour, minutestraveled) < < endl; 12 return 0; 13
Answers: 1
You know the right answer?
Programming question: Given a number, eg. 8, try to calculate and output a list/array of

Questions
question
Mathematics, 24.06.2019 18:00
question
Social Studies, 24.06.2019 18:00
question
Mathematics, 24.06.2019 18:00
question
Biology, 24.06.2019 18:00
question
Mathematics, 24.06.2019 18:00
Questions on the website: 13722363