From visual point of view the difference in (0 - 32) * 5 / 9 and (0 - 32) * (5 / 9) are just brackets but I get -18 for one and 0 for other.
Its a combination of the fact that in python without the division import (see below), a division of two integer numbers is always truncated to an integer and the order of operation.
(0 - 32) * 5 / 9
0 - 32 = -32
-32 * 5 = -160
-160 / 9 = - 18
(0 - 32) * (5 / 9)
0 - 32 = - 32
5 / 9 = 0
- 32 * 0 = 0
you should put from __future__ import division, on top of your python file
then divisions are not integer divisions.
Related
x = 1 / 2 + 3 // 3 + 4 ** 2 # Why is this equivalent to 17?
y = 1 / 2 + 3 # This is equivalent to 3.5
z = 1 / 2 + 3 // 3 # This should be equivalent to 3.5 // 3
t = 3.5 // 3 + 4 ** 2 # Why is this 17 but the original statement is 17.5?
Why are the expressions for t and x providing different results? Are they not equivalent?
(Original image)
It's 17.5 because your statement is as follows:
1/2 - this is evaluated to 0.5
3 // 3 - this is evaluated to 1
4 ** 2 - this is evaluated to 16
16 + 1 + 0.5 = 17.5
You need to understand the order of operations in your initial statement:
1 / 2 + 3 // 3 + 4 ** 2
This can be bracketed according to standard order of operations (BODMAS or some variant):
(1 / 2) + (3 // 3) + (4 ** 2)
Which then evaluates as above. Your confusion stems from the fact that 1 / 2 + 3 // 3 is not equivalent to (1/2 + 3) // 3, but instead equivalent to (1/2) + (3 // 3) - they're both division, so they'll both take precedence over the addition operator.
How do you filter integers 1-52 into 13 buckets? [1 to 4], [5 to 8], [9 to 12], etc. I want to know if given 2 random integers are they in the same bucket? I have tried
(X mod 4) - X]
but get the wrong results below.
#first bucket
(1 mod 4) - 1 = 0
(2 mod 4) - 2 = 0
(3 mod 4) - 3 = 0
(4 mod 4) - 4 = -4 #wrong result
#2nd bucket
(5 mod 4) - 5 = -4
(6 mod 4) - 6 = -4
(7 mod 4) - 7 = -4
(8 mod 4) - 8 = -8 #wrong result
Thank you for any guidance.
As the numbers start with 1, subtract 1 first. And then you don't need to use the modulo operator. Instead use integer division, and you'll get a sequence number for a "bucket" (0..12):
def bucketof(a):
return (a - 1) // 4
print(bucketof(5) == bucketof(8)) # True
I am able to understand the logic here for the below pattern but not getting the way of implementation, please can someone help me here.
The pattern I want is like below, (in first column everything till 5, then second column after 5 leaving first and last row and so on. . )
1
2 6
3 7 9
4 8
5
Try this:
n = int(input())
for i in range(1, (n//2)+2):
for j in range(i):
print(i + ((n - 1) * j) - (j * (j - 1)) , end=' ')
print()
for i in range((n//2)+2, n + 1):
for j in range(n - i + 1):
print(i + ((n - 1) * j) - (j * (j - 1)), end=' ')
print()
Output:
n = 9
1
2 10
3 11 17
4 12 18 22
5 13 19 23 25
6 14 20 24
7 15 21
8 16
9
Logic
n = 9
1 |
2 10 |(10 - 2) = 8
3 11 17 |(11 - 3) = 8, (17 - 11) = 6
4 12 18 22 |(12 - 4) = 8, (18 - 12) = 6, (22 - 18) = 4
5 13 19 23 25 |(13 - 5) = 8, (19 - 13) = 6, (23 - 19) = 4, (25 - 23) = 2
6 14 20 24 |(14 - 6) = 8, (20 - 14) = 6, (24 - 20) = 4
7 15 21 |(15 - 7) = 8, (21 - 15) = 6
8 16 |(16 - 8) = 8
9 |
For example in line5
first number is 5 + ((n - 1) * 0) - (0 * -1) = 5
next value 5 + ((n - 1) * 1) - (1 * 0) = 13
next value 5 + ((n - 1) * 2) - (1 * 2) = 19
next value 5 + ((n - 1) * 3) - (2 * 3) = 23
next value 5 + ((n - 1) * 4) - (3 * 4) = 25
above 5 is i value and 0,1,2,3,4 are j values
generalised formula for calculating the value i + ((n - 1) * j) - (j * (j - 1))
Teaching myself coding, what is the order of operations for this line of code?
print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
I attempted to do remainder and division first so I got 3 + 2 + 1 - 5 + 0 - 0.25 / 4 + 6. Then I completed AS from left to right and got 0.075. Totally wrong because LPTHW puts it at 7. Please offer detailed operation order.
I Googled Python order of operation, but results are not too instructively detailed.
print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
Expected result is 7, but obtained 0.075
It depends on which Python version you are using.
In Python 2, the / operator defaults to integer division, so 1 / 4 == 0.
On the other hand, in Python 3, the / operator defaults to true division, so 1 / 4 == 0.25. You must use // to achieve integer division in Python 3.
Regardless, Python still follows the classical PEMDAS order of operations, so the modulus and division still happen first, followed by addition and subtraction from left to right.
Here's how the problem reduces after you do the modulus and division in both versions:
Python 2
(3 + 2 + 1 - 5 + 0 - 0 + 6) == 7
Python 3
(3 + 2 + 1 - 5 + 0 - 0.25 + 6) == 6.75
*, /, //, % have higher precedence than + and -. So you should first calculate 4 % 2 = 0 and 1 / 4 = 0 (in Python 2.7), and then do the rest of the calculation from left to right.
In Python 2, / uses integer division if both its arguments are integers. That means that 1/4 == 0, since integer division will round down. Then:
= 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
= 3 + 2 + 1 - 5 + 0 - 0 + 6
= 7
To get 6.75 in Python 2 (the expected answer when done on paper), make one of the operands a float:
>> 3 + 2 + 1 - 5 + 4 % 2 - 1.0 / 4 + 6
# ^
>> 6.75
This isnt necessary in Python 3 because / defaults to returning a float.
I'm a complete noob when it comes to programming and having an issue with python maths. If I run the following line of code, python returns 97 as the answer.
print 100 - 25 * 3 % 4
97
Why isn't it (100-25*3) = (100-75) = 25 or (75*3) = 225?
The modulo operator ( % ) has the same precedence as multiplication ( * ), so they are executed from left to right:
100 - ((25 * 3) % 4)
= 100 - (75 % 4)
= 100 - 3
= 97
It's called precedence.
100 - 25 * 3 % 4 is
100 - ((25 * 3) % 4)
100 - (75%4)
100 - 3
97
For the same reason 1 + 2 * 3 is
1 + (2 * 3)
1 + 6
7
and not 9.
The modulo (%) operator has the same precedence as multiplication (*) and division (/). Let's interpret this expression on operator at a time:
print 100 - 25 * 3 % 4
print 100 - 75 % 4
print 100 - 3
print 97
You need to add brackets to override Python's default precedence. Therefore, the code should look more like this:
100 - ((25 * 3) % 4)
Hope I could help!