Programming question: Given a number, eg. 8, try to calculate and output a list/array of
that...
![subject](/tpl/images/cats/informatica.png)
Computers and Technology, 30.05.2020 02:01 apreston2882
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](/tpl/images/cats/User.png)
Answers: 2
Another question on Computers and Technology
![question](/tpl/images/cats/informatica.png)
Computers and Technology, 22.06.2019 01:10
When you reach a yield sign, yield to cross traffic and before you enter the intersection. a. flash your headlights b. wait for a signal c. wait five seconds d. wait for a safe gap
Answers: 1
![question](/tpl/images/cats/informatica.png)
Computers and Technology, 23.06.2019 08:00
What is a scenario where records stored in a computer frequently need to be checked
Answers: 2
![question](/tpl/images/cats/informatica.png)
Computers and Technology, 23.06.2019 21:40
Draw the resistor’s voltage and current phasors at t=15ms. draw the vectors with their tails at the origin. the orientation of your vectors will be graded. the exact length of your vectors will not be graded.
Answers: 2
![question](/tpl/images/cats/informatica.png)
Computers and Technology, 24.06.2019 02:10
Aspeed limit sign that says "night" indicates the legal speed between sunset and sunrise.
Answers: 2
You know the right answer?
Questions
![question](/tpl/images/cats/mat.png)
Mathematics, 06.05.2020 07:19
![question](/tpl/images/cats/istoriya.png)
![question](/tpl/images/cats/istoriya.png)
![question](/tpl/images/cats/biologiya.png)
![question](/tpl/images/cats/ekonomika.png)
![question](/tpl/images/cats/istoriya.png)
![question](/tpl/images/cats/obshestvoznanie.png)
![question](/tpl/images/cats/mat.png)
![question](/tpl/images/cats/mat.png)
![question](/tpl/images/cats/User.png)
![question](/tpl/images/cats/mat.png)
![question](/tpl/images/cats/fizika.png)
![question](/tpl/images/cats/mat.png)
Mathematics, 06.05.2020 07:19
![question](/tpl/images/cats/mat.png)
Mathematics, 06.05.2020 07:19
![question](/tpl/images/cats/himiya.png)
Chemistry, 06.05.2020 07:19
![question](/tpl/images/cats/biologiya.png)
Biology, 06.05.2020 07:19
![question](/tpl/images/cats/ekonomika.png)
![question](/tpl/images/cats/mat.png)
Mathematics, 06.05.2020 07:19
![question](/tpl/images/cats/mat.png)