The following is some summary and personal practices after I read "Programming Pearls Second Edition". 1. The main problem with programmers is not necessarily technically, it is more likely to be psychological: because he is trying to solve a wrong problem, he can't make progress. By breaking the conceptual obstacles, we will solve a simpler problem so that we finally solved the problem. 2, "The more the problem is, the easier it may be," it is possible to solve ", this means directly solving a 23 case problem, to write a general program that handles N, and then it The situation is more difficult when the program is applied to n = 23. 3, the development of the code is the top down (first starting from the general concept, then refining into a row of code), but the correctness analysis is from bottom to bottom : We will start from individual code lines to study how they work together to solve this problem. When you debug, modify code or errors, it is necessary to completely understand the code, resist the "change code, as long as she runs up" urge. 4. The simplicity of maintaining the code is usually the key to the correctness.
As an assembled {} Pseudo-Code Example (Author Analysis): (Ask: Enter, program variable, and output between the relationships describe the status of the program; assertion allows programmers to accurately describe these relationships.)
David Gries' coffee tank problem. At first gave you a coffee pot with some black beans and white beans and a lot of extra black beans. Then you repeat the following procedure until there are only one beans in the tank: randomly select two beans from the tank. If they have the same color, they throw them away and put a black beans in the tank. If they are different, put white beans back and throw away black beans. Please prove that the process will terminate. When the first jar has both black beans and white beans, can you say what color is the last place in the jar? M: Indicates the number of black beans in the jar N: indicates that the number of white beans in the can Samecolor () means that the two beans are removed from the tank
M = black beans N = white beans {(m n)> 0} loop if (m n) == 1 {m == 1 || n == 1} if m == 1 {m == 1} Color = block; break else {n == 1} color = whrite; break case: {SameColor () == Black && m> = 2} m = m = m-1; {(m n) > = 1} SameColor () == whrite: {SameColor () == whrite && n> = 2} n = n-2; {n> = 0} m = m 1; {(m n)> = 1} SameColor () == false: {SameColor () == false && n> = 1 && m> = 1} m = m-1 {(m n)> = 1} {(m n)> = 1} Analysis: When the cycle is terminated, the Color will be assigned, and the total number of beans (M N) in the jar will reduce the total number of beans in the jar. In the initial (m n)> 0, the last jar is affirmed. A bean, m == 1 or n == 1, you can terminate the program; 5, simple and powerful programs can make users happy and will not let the program builders worry, this is the ultimate goal of the programmer.
6. When performing performance calculations for real-time software systems, we must reduce performance in accordance with 2, 4 or 6 coefficients to compensate for our ignorance. When performing reliability / availability commitments, we should reserve a 10 coefficient for the goal we believe to meet to compensate for our ignorance. When estimating ghost fans, costs, and progress. We should retain 2 or 4 coefficients to compensate for our lack of aspects. Einstein Ming "" Anything should be as simple as possible, unless it is not simpler "
7. Ministry of treatment: To solve the problem of N, you can recursively solve the problem of two sizes approximate N / 2 and then combine their answers to get the answer from the entire issue.
8, an example of an optimization of two-point lookup algorithm: algorithm 1: l = 0; u = n-1loop / * lookup T in x [l "] location * / if L> u p = -1; Break file : // Find the end, T does not exist in x [l "] M = (L U) / 2 file: // binary x [l" case x [m]
Description: The algorithm first completed the two-point lookup function; if t ex existed in X [L "], the position returned by the algorithm may be one of them, but the algorithm 2 will return the first one. position. Although the algorithm two seems to be more difficult to algorithm, the algorithm is more efficient, because the algorithm may have more than two times more than two, Case; budget two only need to be compared. 9, code optimization principle: Try to optimize less code; many other properties and efficiency of software, even more important; before optimizing the code, we should ensure that other methods do not provide more effective solutions.
10. An example of myself: When you make a storage operation for an embedded device, its setup vendor provides a dynamic library included full rewriting operations that set up a storage area, and single records, delete operations. Through the test, it is basically the same as a single operation and the time spent on the full rewriting, even more. It turns out that its storage medium is falsed, and all operations are all erased and rewriting. So do a single operation is slower than the complete reverberation, and the operation should be stable. Therefore, I have never used a single operation call, but all use full rewriting, thereby improving efficiency. That is, sometimes it is simple to operate, and it is not necessarily high, it may be the result of organizing some complex operations. As is, you can use%, it is more slow to use if IF and slower.
10. Reduce space usually brings reasonable side effects of running time: The smaller the program, the faster the load, the easier it to fill it into the cache; the less data that needs to operate, the time it takes, the time spent in operation is usually The less. The time required for transmitting data across networks is usually proportional to the size of the data. The key to the compression space is simple, simple, can generate functionality, robustness, and speed and space;