**Python code to find bugs (10) **
**Code design requirements of the previous period: **
Two matrices with 3 rows and 3 columns:
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
Please add the data at the corresponding positions and return a new matrix.
code show as below:
Please read the code carefully to find out the bugs!
**Correct answer: ** There are 2 questions in total. (For the answer time, see how many answers are correct?)
(1) One is a real bug. Please pay attention to the parameters of the range() method. Not only is the length of the X matrix list expected to be the maximum number of cycles, but also the value range of the loop variable i, which will be the index of the X list. Therefore, its starting value should not be 1, but 0, because the index counts from 0. Accordingly, there is no need to add 1 to len(X). The same is true for the following nested loops.
(2) The other is a problem that requires code optimization. This cannot be completely regarded as a bug, because the program can work. However, we say that the code should be as concise as possible, including not writing repetitive code. In many cases, the way we think about solving problems will directly affect the complexity of our code. In the above code, we define an empty list R to store the return value after matrix operation. Therefore, in the first round of the following cycle, we define an empty list Z to store the result of the addition of a corresponding row of the X matrix and the Y matrix in the second round of the cycle. The cycle ends. This result, Can be added to the R list. It seems that there is no problem with the idea, but can it be more concise? Why not define a two-dimensional R matrix from the beginning? In this way, can't we directly update the calculation results of X matrix and Y matrix directly into R? That's right, it can definitely be like this. However, the difference between the two is not formal, but the habitual complexity of thinking. We are always accustomed to using a one-dimensional linear way to consider problems in order, not used to a multi-dimensional parallel thinking mode. This case happens to help us think about such problems. Think about it, what is the difference between X[i] and X[i][j]? Which is more efficient? Why Python always defines some relatively complex data structures is to make the logic of dealing with problems simple, after all, dealing with problems is the goal.
So, the correct code should look like this:
**Comment: This code finds bugs, the main purpose: **
(1) Learn the matrix and recognize the expression of multi-dimensional data.
(2) Consolidate the basic grammar, and master the application of multiple parameters of the range() method.
(3) Train skilled application ability of complex data structure.
For students who have difficulty understanding the above code, please read and study the "Introduction to Python" sent by Gaodu, or the video course "Introduction to Python Easy" on the Gaodu website.
**Code design requirements of this issue: ** Exchange the values of two variables.
code show as below:
After several trials of relatively complex issues, this issue is simple. Of course, the above code is still buggy. Please find out, post in the message, and have the answer tomorrow.
**The correct answer will be announced tomorrow. **
Reminder: Conventionally, all codes are based on Pythpn3.
Recommended Posts