Carambola Python Advanced Lecture 2-The Pros and Cons of Algorithms

My CSDN blog column: https://blog.csdn.net/yty_7

 

Beginners generally ask a question: what is the procedure?

There is a very famous formula: program = data structure + algorithm

I provide a rigorous statement: program = algorithm + implementation language

Why omit the data structure? Because for beginners, algorithms and implementation languages ​​are easier to understand than data structures, then we will start with algorithms.

 

Algorithm refers to an accurate and complete description of the solution to the problem, and is a series of clear instructions to solve the problem. The algorithm represents a systematic method to describe the strategy mechanism for solving the problem.

We actually learn algorithms all the time. The addition, subtraction, multiplication and division we have learned in elementary school is the simplest algorithm. In middle school, did the algorithm become a little harder? There were negative numbers, exponents, logarithms, and even imaginary numbers. At university, it is a qualitative leap. Derivatives, differentials, integrals, partial differentials, and reintegrations appear ... The reason why it becomes more and more difficult is because the problems encountered are becoming more and more complex, and cannot be solved with the previous Proposed a new solution to the problem.

Back to the definition of the algorithm itself, an accurate and complete description of the solution to the problem. For a problem, there are often multiple problem-solving solutions, that is, there are multiple algorithms, then the algorithm will have advantages and disadvantages. To give a famous example in history:

German mathematician Gauss entered St. Catherine Elementary School at the age of seven. At about ten years old, the teacher had a problem in the math class: "Write down the integers from 1 to 100, and then add them up!" Gauss turned in the paper early. Seeing other students add up the numbers one by one, sweating on their foreheads, Gauss sat quietly. Finally, the teacher checked the answers of the classmates: most of them made mistakes, and finally checked the Gaussian test paper, there was only one number on it: 5050 (not to say, this is the correct answer.) The teacher was surprised and Gauss explained How did he find the answer: 1 + 100 = 101, 2 + 99 = 101, 3 + 98 = 101, ..., 49 + 52 = 101, 50 + 51 = 101, there are 50 pairs and the number is 101, so the answer is 50 × 101 = 5050.

The pros and cons of the algorithm are so intuitively reflected. Let's write these two algorithms in Python language, and attach the program running time for comparison:

Code 1: Adding from 1 to 100 using direct summation

import time

start_time = time.time()

sum = 0
for i in range(101):
    sum += i
print(sum)

end_time = time.time()

print("elapsed: %f" % (end_time - start_time))
print("complete!")

运行结果:
5050
elapsed: 0.000000
complete!

Code 2: Adding from 1 to 100 using the arithmetic formula of the arithmetic sequence

import time

start_time = time.time()

s=list(range(101))
sum = (s[0]+s[-1])*len(s)/2
print(sum)

end_time = time.time()

print("elapsed: %f" % (end_time - start_time))
print("complete!")

运行结果:
5050.0
elapsed: 0.000000
complete!

Because the computer's CPU and memory are very powerful, it is not a matter of adding directly from 1 to 100, and the program's running time is 0! But what if we increase from 1 to 100,000?

Code 3: Adding from 1 to 100,000 using direct summation

import time

start_time = time.time()

sum = 0
for i in range(100000+1):
    sum += i
print(sum)

end_time = time.time()

print("elapsed: %f" % (end_time - start_time))
print("complete!")

运行结果:
5000050000
elapsed: 0.050983
complete!

Code 4: Adding from 1 to 100,000 using the arithmetic formula of the arithmetic sequence

import time

start_time = time.time()

s=list(range(100000+1))
sum = (s[0]+s[-1])*len(s)/2
print(sum)

end_time = time.time()

print("elapsed: %f" % (end_time - start_time))
print("complete!")

运行结果:
5000050000.0
elapsed: 0.005998
complete!

Comparing the running time of the two, 0.050983 seconds of code 3 is 8.5 times of 0.005998 seconds of code 4!

Beginners can simply think that an algorithm that takes less time to achieve the same result is a better algorithm.

 

From this, we can get the following two simple conclusions:

1. Loop statements are quite time-consuming, avoid loop statements if possible

2. To make full use of the programming language's own functions and data structures (such as lists, dictionaries, etc.)

 

If you think, add the sum from 1 to 100 directly, is it wrong to write range (101)? Please review Chapter 6 of the basic tutorial: Python control structure (four) loop structure-range traversal.

 

In fact, there are simpler algorithms in this question, as follows:

import time

start_time = time.time()

m=1
n=100000

sum = (m+n)*(n-m+1)/2
print(sum)

end_time = time.time()

print("elapsed: %f" % (end_time - start_time))
print("complete!")

运行结果:
5000050000.0
elapsed: 0.000000
complete!

The characteristic of Code 4 is the use of the data structure of lists. I hope that everyone will consciously use the three structures of lists, dictionaries, and sets when programming, which will greatly simplify the program.

 

My CSDN blog column: https://blog.csdn.net/yty_7

Published 55 original articles · won praise 16 · views 6111

Guess you like

Origin blog.csdn.net/yty_7/article/details/104249198