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

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 10:30, tommyaberman
Would a ps4 wired controller work on an xbox one
Answers: 1
image
Computers and Technology, 24.06.2019 02:30, cardsqueen
Which option completes the explanation for conflict of interest in an organization
Answers: 1
image
Computers and Technology, 25.06.2019 04:10, danny123421
8. create an abstract student class for parker university. the class contains fields for student id number, last name, and annual tuition. include a constructor that requires parameters for the id number and name. include get and set methods for each field; the settuition() method is abstract. create three student subclasses named undergraduatestudent, graduatestudent, and studentatlarge, each with a unique settuition() method. tuition for an undergraduatestudent is $4,000 per semester, tuition for a graduatestudent is $6,000 per semester, and tuition for a studentatlarge is $2,000 per semester. write an application that creates an array of at least six objects to demonstrate how the methods work for objects for each student type. save the files as student. java, undergraduatestudent. java, graduatestudent. java, studentatlarge. java, and studentdemo. java.
Answers: 1
image
Computers and Technology, 25.06.2019 07:00, KillerSteamcar
Asap 50 points ( paragraph pls) discuss the role of the concept behind the "internet of things (iot)" in today's digitally connected society.
Answers: 2
You know the right answer?
Programming question: Given a number, eg. 8, try to calculate and output a list/array of

Questions in other subjects:

Konu
Chemistry, 27.10.2020 01:50
Konu
English, 27.10.2020 01:50