# How can if/elif/else be replaced with min() and max()?

Stack Overflow Asked by nsmedira on December 18, 2020

Working on an exercise from MIT’s OpenCourseWare 6.01SC. Problem 3.1.5:

Define a function clip(lo, x, hi) that returns lo if x is less than lo, returns hi if x is greater than hi, and returns x otherwise. You can assume that lo < hi. …don’t use if, but use min and max.

Reformulated in English, if x is the least of the arguments, return lo; if x is the greatest of the arguments, return hi; otherwise, return x. Thus:

def clip(lo, x, hi):
if min(lo, x, hi) == x:
return lo
elif max(lo, x, hi) == x:
return hi
else:
return x


Maybe I am not understanding the problem correctly, but I can’t figure out how to return a result without using if at all. How can the function be modified to remove the if/elif/else statements?

Link to original problem 3.1.5

Link to previous problem 3.1.4 (for context)

EDIT:

Comments/answers to this question helped me realize that my original plain English reformulation wasn’t a great way to think about the problem. A better way to think about it would have been to determine which of the arguments is between the other two.

One line of code:

#! python3.8

def clip(lo, x, hi):
return max(min(x, hi), lo)

print(clip(1, 2, 3))
print(clip(2, 1, 3))
print(clip(1, 3, 2))

# Output
# 2
# 2
# 2


Correct answer by Paul Cornelius on December 18, 2020

Here you go, a value checking function without using if-else at all. While block will only run for once so there is no redundancy.

def clip(lo, x, hi):
low = (min(lo, x) == x)
high = (max(x, hi) == x)
while low:
return lo
while high:
return hi
return x



EDIT: I don't know why he downvoted my code

Answered by Ateeq ur Rehman on December 18, 2020

You can return this formula:

x + lo + hi - max(x, lo, hi) - min(x, lo, hi)


Arguing by cases:

Case 1:

If min(lo, x, hi) = lo and max(lo, x, hi) = hi
x + lo + hi - max(x, lo, hi) - min(x, lo, hi) ==> x + lo + hi - hi - lo ==> x


Case 2:

If min(lo, x, hi) = lo and max(lo, x, hi) = x
x + lo + hi - max(x, lo, hi) - min(x, lo, hi) ==> x + lo + hi - x - lo ==> hi


Case 3:

If min(lo, x, hi) = x and max(lo, x, hi) = hi
x + lo + hi - max(x, lo, hi) - min(x, lo, hi) ==> x + lo + hi - hi - x ==> lo


The formula returns the expected answer on all possible cases.

Answered by MrGeek on December 18, 2020

## Related Questions

### Substring instead of Left

2  Asked on January 5, 2021

### Can I use the parameter in the function and echo it out in the if statement below

0  Asked on January 5, 2021 by tu-ai-le

### R: Multi-type Point Pattern scan.test

1  Asked on January 5, 2021 by clemson

### Why is javac -source 1.5 allowing @Override on interface methods?

1  Asked on January 5, 2021 by don-hatch

### if else statements returning else value even when they should return if value?

1  Asked on January 5, 2021 by benstackoverflow

### Pandas: filter rows with loc on anonymous dataframe

2  Asked on January 5, 2021 by user3579222

### Randomizing list of lists without overlap

1  Asked on January 4, 2021

### React – Get the value of an input by clicking on a button

4  Asked on January 4, 2021 by mrfacundo

### Why are the spacings in the decipher incorrect?

2  Asked on January 4, 2021 by iliketocode123465

### PersistentObjectException: detached entity passed to persist [JpaRepository]

0  Asked on January 4, 2021 by adriano

### Laravel + Livewire: How to fix Pagination Elements Get method error

0  Asked on January 4, 2021 by no-one

### Lottie animation has large padding. How to resize the animation to the view, by removing the padding

2  Asked on January 4, 2021 by ali-has

### How to play more than one stream at once using soundio?

1  Asked on January 3, 2021 by 010110110101

### Units and Measurements in Swift

1  Asked on January 3, 2021 by tick_hoges

### Why is getter and setter not working for one class in my code, but works for the other ones

0  Asked on January 3, 2021 by katie-schmidt

### Scheme: Iterative process to reconstruct a list in original order?

3  Asked on January 3, 2021 by snusifer

### Count files in directory (python)

0  Asked on January 3, 2021 by user14606934

### Find common alphabets in multiple dictionaries – python

1  Asked on January 3, 2021 by ns68

### How to set the “Resource root URL” in Jenkins

1  Asked on January 3, 2021 by hephaestus

### Write Pandas DataFrame to chronologic excel sheet

0  Asked on January 3, 2021 by anna-bleha

### Ask a Question

Get help from others!