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!
Neo has just passed his first trial. Morpheus nods approvingly.
"Well done, Neo. You're learning to stabilize the Matrix, but there's more to errors than single operations.
Imagine encountering not one error, but an entire sequence of them. Are you ready for your next lesson?"
Neo watches as the monitor now displays a matrix of numbers, some of which are zero, others invalid.
"This is what happens when the Matrix encounters unhandled exceptions", Morpheus explains.
"A single unhandled error disrupts the entire process. Your task is to fix this and restore the matrix integrity."
Neo leans in. "How do I fix it?"
"With precision and control", Morpheus says.
"Catch errors as they occur, and respond gracefully without crashing the program."
Write a Python function transform(lst) that, given a list of lists (sublists contain either integers or strings), returns another list of lists with the integers transformed by \(1/n\), where \(n\) is the respective integer in the list. The function catches the exceptions that arise to continue processing transformations as possible. When an exception is caught, your function should output a message "Error: < type_of_error > for value < value >: < error_message >."
. To produce the following message, you might want to explore the use of type(exception).__name__
. In case there are empty sublists, they should be removed in the transformed output.
The following limits are guaranteed in all the test cases that will be given to your program:
0 ≤ |sublst|, |lst| ≤ 100 | Size of the input list and sublists | |
-1000 ≤ sublsti ≤ 1000 | Elements of the sublist (integers) | |
sublsti can be a string | This scenario will also raise an exception, only if sublsti is not convertible to an integer |
Example Function Calls | Example Output |
lst = [[2, 8, 4], [4, -8, 'a'], [1, 5, 10]] transformed_lst = transform(lst) print("Transformed List:", transformed_lst) |
Error: ValueError for value "a": invalid literal for int() with base 10: 'a'. Transformed List: [[0.5, 0.125, 0.25], [0.25, -0.125], [1.0, 0.2, 0.1]] |
lst = [[2, 0, 4], ["2", "4"], [1, 5, 10]] transformed_lst = transform(lst) print("Transformed List:", transformed_lst) |
Error: ZeroDivisionError for value "0": division by zero. Transformed List: [[0.5, 0.25], [0.5, 0.25], [1.0, 0.2, 0.1]] |
lst = [[2, 8, 4], [4, "abc", "8"], [1, 5, 10]] transformed_lst = transform(lst) print("Transformed List:", transformed_lst) |
Error: ValueError for value "abc": invalid literal for int() with base 10: 'abc'. Transformed List: [[0.5, 0.125, 0.25], [0.25, 0.125], [1.0, 0.2, 0.1]] |