Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am finding difficulty performing a bitwise & operation between the bin() and int in binary form. How can I achieve this goal?
If you are trying to perform the bitwise AND operation (using the & operator) on two integers, there is no need to convert them to strings of their binary representations.
Take this example:
x = 4 # 0b100
y = 5 # 0b101
print(x & y) # => 4, which is 0b100, because 0b100 & 0b101 = 0b100
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
The prog is to accept 4 digit binary number separated by ','.
Then I have to print those number which is divisible by 3.
Here is my code
binary_numbers = input("Enter Binary Numbers:").split(",")
newlist = [no for no in binary_numbers if int(no)%3 == 0]
print(newlist)
when I am printing newlist I am getting empty. I dont know where is the problem.
If you want to input binary numbers, you should set the base of the int cast to 2.
[no for no in binary_numbers if int(no, 2) % 3 == 0]
binary_numbers = input("Enter Binary Numbers:").split()
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Trying to learn a new approach.It's the classical problem of finding the sum of all multiples of 3 and 5 below 'n'.
What I want is:
print ("The sum is: ", sum(range(1, 100)))
But with an if-statement for multiples of 3 / 5.My question:How can I include that if-statement for a one-line solution?
Thanks for your help. =)// Chris S.
This is the approach you may look into:
def thesum(the_numbers):
the_numbers = [i for i in the_numbers if i % 3 == 0 or i % 5 == 0]
return print(sum(the_numbers))
thesum(range(100))
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Suppose we have an array/string of stock symbols: ['AMD','AMZN','BABA','FB'].
I need to be able to convert the supplied stock symbol to 1 and others to 0.
For example if we supplied 'AMZN' to the array above the resulting array should look: [0,1,0,0]. If 'FB' result should look like [0,0,0,1].
I need to feed it into an AI algorithm.
def get_binary_array(input_array, stock_ticker):
return [1 for thing in input_array if thing == stock_ticker else 0]
This is probably what you are looking for:
arr = ['AMD','AMZN','BABA','FB']
value = 'AMD'
one_hot = [int(value==i) for i in arr]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I know it´s the XOR for statements, but what does 2^2 compute in python?
If i give
list = [1,2,3,4,5,6,7,8]
[x^2 for x in list]
it computes to
[3, 0, 1, 6, 7, 4, 5, 10]
It's a bitwise XOR.
You are doing a bit-by-bit XOR with 2 to all of your values in the list.
For example the first element is 1^2 = 3.
Why? 1 is 01 in binary, 2 is 10 and so: (I'm omitting leading zeroes)
01
XOR 10
= 11
It gives 3 since 11 is 3 in binary.
^ is the Bitwise XOR operator in python. So in your example it return the bitwise XOR of each number in your list with 2. In that way I think it now makes more sense.
^ is used for an exclusive-or (bitwise) operation.
Make sure the power is integer only.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Why in gods name is it 57.5
compiler output
input return strings, thus 2 + 3 is 23 (the default behaviour for + with strings is to concatenate them), then the casting to int turns this into an actual 23, so finally you get 5 * 23 / 2 == 115 / 2 == 57.5.
To solve this, cast each parameter to int before doing the math operations:
int(h) * (int(a)+int(b)) / 2
You concatenate two string, then turn the result into an int. If you do int(a) + int(b) it will work properly.