Restrict the Domain X25677


Statement
 

pdf   zip   main.py

html

Write a function restrict_domain that receives a function operating on floats f, a lower bound lb and an upper bound ub. Then, restrict_domain returns a new function that operates exactly as f within the closed interval [lb, ub]. Outside that interval, the function produced returns float("−inf").

Sample session
>>> from math import sqrt
>>> f = restrict_domain(sqrt, 1, 100)
>>> f(25)
5.0
>>> f(-25)
-inf
>>> f(125)
-inf
>>> f(1)
1.0
>>> f(100)
10.0
>>> f = restrict_domain(lambda x: x * (x+1), 10, 12)
>>> f(9)
-inf
>>> f(10)
110.0
>>> f(11)
132.0
>>> f(12)
156.0
>>> f(13)
-inf
Information
Author
Jordi Delgado and José Luis Balcázar
Language
English
Official solutions
Python
User solutions
Python