Restrict the Domain X25677


Statement
 

pdf   zip   main.py

Write a function restrict_domainrestrict\_domain that receives a function operating on floats ff, a lower bound lblb and an upper bound ubub. Then, restrict_domainrestrict\_domain returns a new function that operates exactly as ff within the closed interval [lb,ub][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