Solution: Task V to Task VIII
Explore how to implement matrix addition, multiplication, and scalar intensification using coroutines in Python. Understand managing matrix operations in series, switching between operations with nested generators, and handling exceptions. This lesson reinforces Python coroutine patterns for advanced matrix manipulations.
Explanation
Task 5: Add matrices in series
You were to add matrices in the order the coroutine add_series receives them.
Look at the following code.
In main, look at the header of add_series at line 39. We create a matrix result. It’s empty, initially. Look at line 43. We apply this condition because we want the process to continue until a user is sending some values. We are yielding result as this value, which will possibly change in every step. When a user sends a value, it will be stored in curr_matrix.
Now, at the time when add_series gets a value for the first time, the result would be None. So, its value needs to be updated as curr_matrix (see line 47). There has to be no addition at the first step, as per the requirement.
After the first step, whenever the user sends a value again, ...