user14859499
user14859499

Reputation:

Is there a way to write this if-else using min and max?

This is the condition.

 if (car.y < 100): car.y = 100
 if (car.y + car.height) > 500: car.y = 500 - car.height  

I want to know how to write this using min and max.

This is what I tried.

car.y = max(100, min(500, car.y - car.height))

According to my knowledge, min(500, car.y - car.height) returns 500 if 500 < car.y - car.height, which means it cant ever be more than 500, capping the upper limit and since its in max(100, ...), the lower value cant be less than 100.

I used this test and everything works as expected. The values are capped between 100 and 500.

for i in range(100):
    print(max(100, min(500, random.randint(10,  1000) - random.randint(10, 1000))))

Now here is where I am stuck.

In the following case.

y = 100
height = 10  
while True:
    if (y < 100): y = 100
    if (y + height) > 500: y = 500 - height
    y += 1
    print(y)

It behaves as expected but in the following case:

y = 100
height = 10

while True:
    y = max(100, min(500, y - height))
    y += 1
    print(y)

It just prints 101 continuously and doesn't increment at all. It's stuck at 101. Thank you for helping me.

Upvotes: 5

Views: 517

Answers (4)

BiOS
BiOS

Reputation: 2304

If you try to run this code (I have used a for loop rather than a while to make it more testable):

y = 100
height = 10

for i in range(5):
    h = min(500, y - height)
    y = max(100, h)
    print(h , ' ' , y , ' ', y+1, '\n')
    y += 1

You will see that it gets stuck at 101. Why? Let's do the iteration together:

Loop 1

h is min between 500 and (100-10), thus it will be 90

y is max between 100 and 90, so it will be 100

y is plus 1, which now becomes 101

Loop 2

h is min between 500 and (101-10), so 91

y is max between 100 and 91, so? 100!

y is plus 1, so 100 + 1 now becomes 101... and this will continue forever, let's continue just to prove this:

Loop 3

h is min between 500 and (101-10), so 91

y is max between 100 and 91... 100!

y is plus 1, 101...

So as you can see, as the product of y will remain 101, no matter how many times we try, it will remain 101 in the end.

Now you know why it got stuck. Depending on what you need to do or what you are experimenting on, you can add another variable, allowing y to freely grow and make your code work as expected:

y = 100
height = 10

for i in range(25):
    h = min(500, y - height)
    z = max(100, h)
    y += 1
    print(h , ' ' , z , ' ', y, '\n')

And convert the for loop into a while as needed.

Upvotes: 1

Vishal Asrani
Vishal Asrani

Reputation: 186

y = 100
height = 10  
while True:
    if (y < 100): y = 100
    if (y + height) > 500: y = 500 - height
    y += 1
    print(y)

Snippet above makes it stuck at 491. Similar code in max min style is below:

y = 100 
height = 10 
while True: 
    y = min(500-height, max(100, y)) + 1
    print(y)

Upvotes: 1

Jacob Thompson
Jacob Thompson

Reputation: 23

Same as Blue_notes answer just the other way round.

car.y = max(car.y, 100);
car.y = min(car.y, 500-car.height)

or if you want it as one line

car.y = min(max(car.y, 100),500 - car.height)

Upvotes: 2

pho
pho

Reputation: 25489

The maximum that car.y can be is 500 - car.height (so you need to select the smaller of the two). The minimum that it can be is 100 (so you need to select the larger of this pair)

car.y = max(min(car.y, 500 - car.height), 100)

Upvotes: 0

Related Questions