What does the ^ operator in python do to integers? [closed] - python

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.

Related

How can I iterate through every digit in a number? [closed]

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 11 months ago.
Improve this question
Say I had the number 123, how could I return 3, 2, 1, 23, 12, and 123. And if I had the number 100, return 100 and 0 only and not 100, 00, 0.
str can change the format of integers and floats to a string. Then, using the set function, you can change this string to the characters of the string. Note that, sets do not contain the duplicated values, there fore 100 would change to something like {"1", "0"}. You can use something like:
myDigit = 100
myDigitList = set(str(myDigit))
for digit in myDigitList:
print(digit)
Output
1
0
You could take advantage of the string properties in python. Then converting it back to numbers:
number = 1234
number_string = str(number)
for i in range(len(number_string)):
print(int(number_string[i]))

How to get a list using the range function? [closed]

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 3 years ago.
Improve this question
How to get expected answer [0, 1, 2, 3] using the range function?
I tried this Python code:
print(range(4)),
result:
range(0,4)
The result is different from online playgrounds:
result 01: range(0,4)
result 02: [0, 1, 2, 3]
The general syntax of range function:
range(start, stop, step)
start : Numerical index, at which position to start(optional)
stop : Index you go upto but not include the number(position to end)(required)
step: Incremental step size(optional)
Examples
x = range(0, 6)
for n in x:
print(n) //output: 0 1 2 3 4 5
In your case you need a list containing the integers so as #Green Cloak Guy said you should use list(range(4))
do list(range(4)).
The range() function returns a generator (only produces one value at a time). You have to convert that generator to a list.

What is the most effiecient way to get Factorial Sum? [closed]

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
The code is to find the factorial of each individual value in an array and then find the sum of them together .An example would be [1,2,3,4], which will then be 1!+2!++3!+4!=33.A single integer equal to the desired sum, reduced modulo . The issue is that when it comes on to large numbers(just my assumption) it results in "Terminated due to timeout" status
At first I used a for loop to go through each value in the array. Thinking that it may be a search issue I used for in range to ensure it has a set range. Sadly that still hasn't solved the problem. I now assume that it has to be a problem with factorial since multiplication is
def factModSum(arr):
sum=0
for i in range (0,len(arr)):
sum=sum+factorial(arr[i])
return sum%(10**9)
Example 1:
Input: 1 2 3 4
output: 33
Expected output: 33
Example 2:
Input:2 3 5 7
Output:5168
Expected output: 33
Example 3:
Input:12 13 14
Output:884313600
Expected output: 33
At the core of it at the function works. But Im getting timeout error for some of my Test case , therefore assuming that the code is not able to process large numbers in a given time
If you are modding by 10 ** 9 you can try this:
def factModSum(arr):
return sum(factorial(i) for i in arr if i < 40) % 10**9
This is because n! with n >= 40 is congruent to 0 mod 10**9.

python math wrong answer (not decimals) [closed]

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.

How to do & operation on binary no.(0b1100) and bin(4)? [closed]

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

Categories