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, 21.06.2019 18:30
The method of presentation refers to the planning process for the presentation. the information chosen for the presentation. how the presentation topic will be introduced. how the presentation will be delivered.
Answers: 1
question
Computers and Technology, 22.06.2019 06:30
Requirement types discussed during software development include functional and color scheme nonfunctional and code style constraint and nonfunctional fashionable and functional.
Answers: 2
question
Computers and Technology, 22.06.2019 20:00
What is the worst-case complexity of the maxrepeats function? assume that the longest string in the names array is at most 25 characters wide (i.e., string comparison can be treated as o( class namecounter { private: int* counts; int nc; string* names; int nn; public: namecounter (int ncounts, int nnames); int maxrepeats() const; }; int namecounter: : maxrepeats () { int maxcount = 0; for (int i = 0; i < nc; ++i) { int count = 1; for (int j = i+1; j < nc; ++j) { if (names[i] == names[j]) ++count; } maxcount = max(count, maxcount); } return maxcount; }
Answers: 3
question
Computers and Technology, 23.06.2019 12:00
From excel to powerpoint, you can copy and paste a. cell ranges and charts, one at a time. b. cell ranges and charts, simultaneously. c. charts only. d. cell ranges only.
Answers: 3
You know the right answer?
Programming question: Given a number, eg. 8, try to calculate and output a list/array of

Questions