Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I need help doing this problem without any conditionals or loops:

Write the function getInRange which takes 3 values (which you may assume are all numeric) -- x, bound1, and bound2, where bound1 is not necessarily less than bound2. If x is between the two bounds, just return it unmodified. Otherwise, if x is less than the lower bound, return the lower bound, or if x is greater than the upper bound, return the upper bound.

share|improve this question
4  
Please provide your code , site is meant for code review not the solution of problem .please migrate it to stackoverflow. – paritosh Dec 25 '12 at 4:11
Other than perhaps some ridiculous bitwise-hackery, it's not possible to do conditional logical without conditionals.... – Corbin Dec 25 '12 at 6:28
Unless functions are allowed, even if those contain conditionals – Glenn Rogers Dec 25 '12 at 7:38

closed as off topic by Corbin, Glenn Rogers, Brian Reichle, svick, Winston Ewert Dec 26 '12 at 4:37

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

how about:

def getInRange(x, bound1, bound2):
    return max(min(x, bound2), bound1)

?

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.