Algorithm Series Tutorial 01

Source: Internet
Author: User

Hello everyone, long time no update article, sorry everyone. Starting today, I'm going to write another series of tutorials: Algorithms.

Opening words

Why write an algorithm series tutorial? Because recently just finished reading "Algorithms, 4th Edition" This classic algorithm book (e-book, the English version of the Internet has downloaded), with some new harvest, feel that those scattered knowledge points and experience is necessary and worth taking the time to tidy up a bit. In addition, my own algorithm knowledge should not reach to learn "machine learning" the degree of the subject, in writing tutorials at the same time I will continue to learn, I would like to work with you with this key to open the "machine learning" the door, to explore how the algorithm is to make the machine "learning".

Why do you want to learn the algorithm? I personally think that the ultimate goal is to improve their competitiveness in the workplace. The essence of the algorithm is to solve the problem, so the learning algorithm is essentially improving the ability to solve problems, which is one of the most important abilities in the workplace. In addition, if you want to perform better in the interview, you have to have a certain degree of mastery of the algorithm.

Writing tutorials is very exhausting, and the learning algorithm is very boring, but if everyone can participate in the exchange of discussion, then the whole process is constantly brainstorming, brainstorming process, this will be my motivation to continue writing, which will make the boring algorithm learning path becomes interesting, and let everyone harvest full.

In order to make it easier for everyone to use the fragmented time to learn, better use the free time on the phone to read, this series of every article I will be carefully typesetting, and every article reading time is not too long, will try to control within 6 minutes (not including the time of thinking).

This series will contain three parts of the content. The first part is the basis of the algorithm, because the algorithm and data structure is closely related, so will also talk about some data structure knowledge, but not too thin. The second part is based on the common algorithm case analysis, including parsing some algorithm surface questions. The third part is the application of the algorithm in machine learning, because I have not learned machine learning, so this part is the exploration and communication, I will learn to use my understanding to write, I hope you can participate in the exchange.

This series of article code selection in JavaScript language to write, the programming language is interlinked, I believe you read the JavaScript written algorithm can be quickly written in your familiar language. But the third part of the machine learning related content, may choose Python, is not sure.

I hope this algorithm series tutorial can help you in the future programming road handy.

Opening algorithm title: Fizzbuzz

Well, let's get to the beginning of an algorithmic problem fizzbuzz today. Fizzbuzz is a very typical algorithmic problem for interviewing. Here is the natural language description of Fizzbuzz:

Write a program, print numbers from 1 to 100, but can be divisible by 3 into "Fizz", can be divisible by 5 into "Buzz", can be divisible by 3 and divisible by 5 into "Fizzbuzz".

Is it easy to see? It's really simple. After reading the question, you can write the following code conveniently:

 for(vari =1; I <101; i++) {
ifI3==0)Console. log (' Fizz ')
Else ifI5==0)Console. log (' Buzz ')
Else ifI3==0&& I%5==0)Console. log (' Fizzbuzz ')
Else Console. log (i)
}

Match the demand, as if there were no problems. And so on, check carefully, this code problem is big, FizzBuzz never output the opportunity, such as encounter i=15 or output Fizz . This is a very easy for the novice programmer to make a mistake, to get the demand is a direct write, the understanding of the demand is more blunt, it is not easy to consider the context of the association relationship. The correct wording is to put the IF condition on line 4th to the front:

 for(vari =1; I <101; i++) {
ifI3==0&& I%5==0)Console. log (' Fizzbuzz ')
Else ifI3==0)Console. log (' Fizz ')
Else ifI5==0)Console. log (' Buzz ')
Else Console. log (i)
}

This is true, but if it is an interview, the answer can only be passed. How can you do good? I summarize the writing algorithm can be divided into three steps:

The first step is the implementation, that is, according to the requirements of the algorithm written to the correct operation of the program, the second step is to optimize, that is, reduce the complexity of the program to make the operation more efficient. The third step is to beautify, that is, to make the code look elegant and concise, this step is often flexible to the programming language itself, the syntax of sugar, but the brevity sometimes sacrifices the readability of the code, which requires a slight tradeoff in the actual scene.

To do these three steps, I think the answer to the algorithm in the interview can be excellent.

We have completed the first step in writing the Fizzbuzz algorithm, and we will do the second step: optimization. Both divisible by 3 and divisible by 5, we naturally think of the least common multiple of 3 and 5:15, the number divisible by 15 must be divisible by 3 and divisible by 5. This means that the second line of code above can be i judged only once (not two times), and the following is the optimized code:

 for(vari =1; I <101; i++) {
ifI the==0)Console. log (' Fizzbuzz ')
Else ifI3==0)Console. log (' Fizz ')
Else ifI5==0)Console. log (' Buzz ')
Else Console. log (i)
}

Let's take another look at the third step: landscaping. Above we write this algorithm with 6 lines of code (including curly braces), if you want to reduce the amount of code, how many lines can be reduced? Here are the implementation scenarios for the 2 line:

 for  (let  i = 0 ; I < 100 ; 
 console . Log ((++i% 3 ? ": ' Fizz ' ) + (i% 5 ? ": Buzz" ) | | i)

This implementation comes from Reddit. Of course you can write a line, but it's better to write a two-line reading experience. This code is very good understanding, I will not read, there are questions can be in the message area message to ask questions.

Finally, for the fizzbuzz algorithm problem, do you have other implementations? Welcome to discuss in the message area.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.