Think simple – Everything will be simple

Tram Ho

Hello everyone, today we will come to a perspective article in thinking using algorithms in programming! Suppose I have a problem like this:

Write a program that prints even numbers between 1 and 100.

This is a very basic exercise for those who are new to programming, it is not difficult, even as easy as eating candy. But I have found a new solution here:

Maybe the above program is only humorous, heavy of opposition, but in the spirit of today’s article we will analyze in a different way, from a practical perspective.

If this is a real project

Imagine a rich gentleman came and told you, “Write me a program to print even numbers between 1 and 100, complete in a day, I’ll pay you $ 1,000.” And of course, you can not miss such a fragrant rafters. Wait !!! Let’s analyze a bit!

Choose a good solution: Why a good solution, but not a right one? Because in school, if you give the right solution, you get good grades. But in reality, the same problem has many ways to solve, and no one is absolutely right, only one solution is better than the other.

Code is just fleeting, new products are important

There are billions of ways to print this number, but who cares? What does one want? The program prints the above sequence of numbers, that’s all. The other customer pays to get the product, not the code or the algorithm at all.

Performance and value: Maybe the solution does not seem very smart, but if this is a real product, I will give it a score of 10, it is clear that the program did a good job, optimized for speed, little (or no) errors arise, products work well with customers, bringing real value.

This is the right solution: After looking at the image above, some of you commented, “So if 10,000 is written till morning” No, if the problem is 10,000, we will choose another option, the alternatives Given must be in accordance with actual conditions and capabilities. Using complex technologies and algorithms for a simple problem is not very smart.

Although the area of ​​the test paper is limited, it is sufficient to meet this situation. Regarding the practice, there is no need to use the NoSql database for a table with only 100 records.

Time to complete the project: About the time to complete the project, enough time to write all the above numbers. Therefore, this is a good plan to ensure progress before the deadline.

Conclude

From a math problem, suppose you will give yourself a small lesson. In life or programming, too. Look at things, think them simpler – You will find everything is easy.

Look at, value, accurately measure the problem and then come up with a method, using a reasonable algorithm or technology. Because there are simply thousands of ways to code, a good product is what resistance is needed. Please optimize it in the best way possible.

Share the news now

Source : Viblo