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 16:10
Drag each label to the correct location on the imagelist the do’s and don’ts of safeguarding your password.keep yourself loggedin when you leave your computer.don’t write your password down and leave it whereothers can find it.share your password with your friends.each time you visit a website,retain the cookies on your computer.use a long password with mixed characters.
Answers: 1
question
Computers and Technology, 22.06.2019 17:00
What allows you to create a wireless connection among your smart devices
Answers: 2
question
Computers and Technology, 23.06.2019 04:00
Laire writes a letter to her grandmother, in which she describes an amusement park she visited last week. she adds pictures of that place in her letter. which feature of a word processing program will claire to remove unwanted parts of the pictures?
Answers: 3
question
Computers and Technology, 23.06.2019 18:30
How often does colleges update the cost of attendance on their website? . a)every two years b) every four years c) every year d) every semester
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
History, 29.08.2019 18:00
question
Mathematics, 29.08.2019 18:00
question
Mathematics, 29.08.2019 18:00
question
History, 29.08.2019 18:00