Write a function: that, given two non-negative integers A and B, returns the number of bits set to 1 in the binary representation of the number A * B. For example, given A = 3 and B = 7 the function should return 3, because the binary representation of A * B = 3 * 7 = 21 is 10101 and it contains three bits set to 1. Assume that: In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment
def solution(A,B):
C =A*B
bin_c = bin(C).replace("0b","") #converting to binary form
res = [int(x) for x in str(bin_c)] #converting binary number to list of 0 and 1
c_1 = 0 #counter for 1
for i in range(len(res)): #counting the number of 1's in list
if res[i]== 1:
c_1 = c_1 + 1
return c_1
int countbits(unsigned int a, unsigned int b) {
unsigned int n = a * b;
unsigned
int
count =
0;
while
(n) {
count
+= n & 1;
n
>>= 1;
}
return
count;
}
def checkSorted(a, b):
output = a*b
if output == 0:
return 0
else:
binResult = bin(output)[2:]
return binResult.count("1")
result = checkSorted(3,7)
print(result)
Write a function: that, given two non-negative integers A and B, returns the number of bits...
Q3 [Number Searching] (25%) Given two non-negative integers A (data type: int) and B (data type: int), write a C++ program to determine whether A contains B, wherein "contains” means an exact match. For example, • If A is 12345 and B is 234, A contains B, where the matching part is underlined in A. • If A is 12345 and B is 235, A does not contain B, even A has the digits 2, 3 and 5. It is...
Parvati is given an array of integers. She is asked to return the sum of all the two-digit numbers in the array. Please help her to perform this task. Write a function: class Solution { public int solution (int[] A); } that, given an array A consisting of N integers, returns the sum of all two-digit numbers. For example, given A = [1, 1000, 80, -91], the function should return -11 (as the two-digit numbers are 80 and -91). Given...
Give an informal description (in plain English) of a Turing machine with three tapes that receives as input two non-negative integers x and y, and returns as output the integer xy. Integers are represented as binary strings.Start of the computation: The first tape contains the binary representation of x and its head is on the rightmost symbol of x. The second tape contains the binary representation of y and its head is on the rightmost symbol of y. The third...
Hi, I have Python programming problem as follow: Thank you. Best Regards. Write a function solution that, given two integers A and B, returns a string containing exactly A letters 'a' and exactly B letters 'b' with no three consecutive letters being the same in other words, neither "aaa" nor "bbb" may occur in the returned string). Examples: 1. Given A = 5 and B = 3, your function may return "aabaabab". Note that "abaabbaa" would also be a correct...
please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....
In PYTHON: Write a function that receives a list of integers and returns the number of integers that are larger than the element immediately before them. For example, given [1,2,3,-5,0,-5,-10] the function should return 3 (since 1<2, 2<3, and -5<0).
PYTHON 3!!!!! Write a function: class Solution { public String solution(String T); } that, given a string T, returns the latest valid time that can be obtained from T, as a string in the format "HH:MM", where HH denotes a two-digit value for hours and MM denotes a two-digit value for minutes. Examples: 1. Given T = "2?:?8", the function should return "23:58". 2. Given T = "?8:4?", the function should return "18:49". 3. Given T = "??:??", the function...
1.3 Write a function to output an arbitrary double number (which might be negative using only printDigit for VO Write a recursive function that returns the number of 1 in the binary representation 1 in the representation of N/2 plus 1, if N is odd
Using Python3 Write a function that takes an array of integers and returns it in increasing order. There can be negative numbers, but they are treated as positive numbers. Example list= list=[-6,7,6,7,-9,1,0,-3,-2,1,4] Answer list= [0, 1, 1, -2, -3, 4, 6, -6, 7, 7, 3]
Expectations of Functions: Let X be distributed over the set N of non-negative integers, with probability mass function: Let X be distributed over the set N of non-negative integers, with pmf a P(X = i) = 21 for some fixed α E R. Find EX For Y X mod 3, find ·P(Y= 1) .ELY