#๐Ÿ”’ Understanding chained operators.

17 messages ยท Page 1 of 1 (latest)

raven frigate
#

I would assume that these two conditions would be the same, but Python does not seem to think so. I'm confused about why and want to know what is happening internally when Python evaluates the condition.

Assuming nums[midpoint] is 1, target is 3 and nums[high] is 5 (or any values you want to use)

Condition 1
if nums[midpoint] < target <= nums[high]:

Condition 2
if nums[midpoint] < target and nums[high] >= target:

smoky atlasBOT
#

@raven frigate

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

tall lintel
#

https://peps.python.org/pep-0535/

nums[midpoint] < target <= nums[high]

# is equal to

nums[midpoint] < target and (target <= nums[high])
#
_expr = target
lhs_result = nums[midpoint] < _expr
_expr_result = lhs_result and (_expr <= nums[high])

or equivalent to this as described in the docs. (which represents how it is handled internally)

so all three are the same

raven frigate
#

I mean if all three are the same, then this is the same.

if nums[midpoint] < target and nums[high] >= target:

And from the clarification you gave, an and is used. So, I wonder why Python is short-circuiting the second condition

#

Let's take this example

Example 1

class Solution:
    def search(self, nums: List[int], target: int) -> int:

        low, high = 0, len(nums) - 1
        
        while low <= high:

            midpoint = (low + high) // 2

            if nums[midpoint] == target:
                return midpoint

            if nums[low] < nums[midpoint]:
                if nums[low] <= target < nums[midpoint]:
                    high = midpoint - 1
                else:
                    low = midpoint + 1
            else:
                if nums[midpoint] < target <= nums[high]:
                    low = midpoint + 1
                else:
                    high = midpoint - 1
        
        return -1

Example 2

class Solution:
    def search(self, nums: List[int], target: int) -> int:

        low, high = 0, len(nums) - 1
        
        while low <= high:

            midpoint = (low + high) // 2

            if nums[midpoint] == target:
                return midpoint

            if nums[low] < nums[midpoint]:
                if nums[low] <= target < nums[midpoint]:
                    high = midpoint - 1
                else:
                    low = midpoint + 1
            else:
                if nums[midpoint] < target and nums[high] >= target:
                    low = midpoint + 1
                else:
                    high = midpoint - 1
        
        return -1

With this input
nums =
[3,1]
target =
1

You would expect the same. Running them though, the results are different.

tall lintel
#

!e

a = 1
x = -1

a < x < b

python also does short circuit here, as you see no NameError occurs because of the short circuit

smoky atlasBOT
raven frigate
#

Let me check something on my IDE

tall lintel
#

!e

class Solution1:
    def search(self, nums, target):

        low, high = 0, len(nums) - 1

        while low <= high:

            midpoint = (low + high) // 2

            if nums[midpoint] == target:
                return midpoint

            if nums[low] < nums[midpoint]:
                if nums[low] <= target < nums[midpoint]:
                    high = midpoint - 1
                else:
                    low = midpoint + 1
            else:
                if nums[midpoint] < target <= nums[high]:
                    low = midpoint + 1
                else:
                    high = midpoint - 1

        return -1


class Solution2:
    def search(self, nums, target):

        low, high = 0, len(nums) - 1

        while low <= high:

            midpoint = (low + high) // 2

            if nums[midpoint] == target:
                return midpoint

            if nums[low] < nums[midpoint]:
                if nums[low] <= target < nums[midpoint]:
                    high = midpoint - 1
                else:
                    low = midpoint + 1
            else:
                if nums[midpoint] < target and nums[high] >= target:
                    low = midpoint + 1
                else:
                    high = midpoint - 1

        return -1

a = [*range(10)]
print(all(Solution1().search(a, x) == Solution2().search(a, x) for x in range(10)))

all the same

smoky atlasBOT
raven frigate
#

Okay. I think this proves the bug might be elsewhere. So yes, both conditions are evaluated as an and and both would short circut

#

Thank you @tall lintel

tall lintel
#

you are welcome

raven frigate
#

!close

smoky atlasBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.