In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of January 3rd
(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 (note that you don't need to round: just return the result of 1/n as a string in the respective position).
The function should catch the exceptions that arise and continue processing transformations as possible. For each exception caught, your function should output a line with the message "Error: <type_of_error> for value <value>: <error_message>.". To produce the error message, you might want to explore the use of type(exception).__name__. In case there are empty sublists, they should be removed from 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], ["abc","def"], [1]]
transformed_lst = transform(lst)
print("Transformed List:", transformed_lst)
|
Error: ZeroDivisionError for value "0": division by zero. Error: ValueError for value "abc": invalid literal for int() with base 10: 'abc'. Error: ValueError for value "def": invalid literal for int() with base 10: 'def'. Transformed List: [[0.5, 0.25], [0.5, 0.25], [1.0, 0.2, 0.1], [1.0]] |