In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of January 4th
(this exercise will still be available for submission after that deadline, but without counting towards your grade)
[to understand the context of this problem, you should read the class #11 exercise sheet]


In this problem you should submit a function as described. Inside the function do not print anything that was not asked!

[IP099] The Wisdom of the Oracle

The scent of cookies fills the air as the Oracle greets them with a knowing smile.

"Ah, Neo," she says, handing him a cookie. "You've learned to handle errors, but can you predict them? The Matrix is full of unpredictability, but with the right skills, you can foresee and account for future exceptions."

Neo looks puzzled. "How?"

The Oracle takes a seat and points to a scroll of code. "Imagine a function f that accepts numbers. For some, it works perfectly. For others, it fails. Your task is to count the failures within a given range. This will teach you how to anticipate errors without letting them disrupt your process. It's like seeing into the future, Neo."

The Problem

Write a function future_exceptions(f, a, b) that receives a function f and two integers a and b. Consider that function f accepts an integer between a and b and may raise an exception when applied to an invalid integer. Your function future_exceptions(f, a, b) should try to apply f to all integers in the range [a,b] and return the amount of exceptions caught. For instance:

Constraints

The following limits are guaranteed in all the test cases that will be given to your program:

f       A function that takes one integer
-1000 ≤ a, b ≤ 1000       Integers a and b that make up the interval [a,b]

Example Function Calls Example Output
print(future_exceptions(lambda x: 1/x, -5, 5))
print(future_exceptions(lambda x: 1/(abs(x)-2), -5, 5))
1
2

Introduction to Programming (CC1024)
DCC/FCUP - University of Porto