Restrict the Range X85359


Statement
 

pdf   zip   main.py

html

Write a function restrict_range that receives a function operating on floats f, a lower bound lb and an upper bound ub. Then, restrict_range returns a new function that operates exactly as f when f(x) falls within the closed interval [lb, ub]. Instead of outputs outside that interval, the function produced returns float("−inf").

Sample session
>>> f = restrict_range(lambda x: x * x * x, 1, 1000)
>>> f(1)
1.0
>>> f(-5)
-inf
>>> f(5)
125.0
>>> f(10)
1000.0
>>> f(11)
-inf
Information
Author
Jordi Delgado and José Luis Balcázar
Language
English
Official solutions
Python
User solutions
Python