Author avatar

Recnac

Algorithm Templates: Introduction

Recnac

  • Feb 22, 2020
  • 3 Min read
  • 997 Views
  • Feb 22, 2020
  • 3 Min read
  • 997 Views

Introduction

Stop trying to reinvent the wheel.

When you encounter a new algorithm problem, it's better to apply an existing algorithm template than to design and implement one from scratch. In this series, I have collected and summarized the general code templates for particular algorithms and added typical examples to help you make better use of them.

Why Python?

As the saying goes, "Life is short. You need Python." With the help of concise syntax and advanced features, Python lets us ignore redundant grammar and totally focus on algorithm and logic. As a high-level language, Python is well suited for expressing algorithms. It's close to natural languages. Besides, Python is one of the most popular languages nowadays.

It's worth mentioning that the algorithm templates in this guide are written in Python, but it is easy to extend them to other languages, such as Java or C++. Just replace Python's syntactic sugar and advanced tricks, though this may result in less concise syntax.

Templates

As I mentioned before, algorithm templates are very useful. They show the core idea of an algorithm, and by reusing and reforming these templates, you can easily apply them to your scenario. To use a more vivid example, it's hard to build a house from nothing, but easier if we have the framework and components. We just need to choose, fill, reform, and decorate.

I have tried my best to collect and summarize the most general and best-practice templates in a particular domain, but this version may not be the best solution. Or, it is indeed the best performing and most concise solution, but it's not suitable for you. Within acceptable performance measures, a coding style is best for you when it's most convenient.

These code templates are written in Python with some pseudo code, so I use self and add stub functions to pass compiler's check. You should replace them with your particular logic according to the comments. In order to keep the code concise, some code snippets may not be complete and runnable.

Examples

Many code examples in these guides come from LeetCode. I have tried my best to include brief problem descriptions for most cases, but in reality, you can focus only on the algorithm. If you are also interested in the problem and want to go deeper, you can visit LeetCode for more information (I have included the problem ID for your information).

Thanks to LeetCode for the algorithm problems. Moreover, LeetCode has a great OJ platform. It's great for functional and performance tests. Many templates and tricks are brought to light by discussions in LeetCode. Thanks to all the code masters who have worked on this platform.

Conclusion

Now we're ready to start our algorithm template learning journey. You can start with Algorithm Templates: Two Pointers - Part 1.

To access the complete code, you can download Algorithm Templates from Github. I hope some of them will be useful for you.

Hope you enjoy this series. If you have any questions, you're welcome to contact me at [email protected]

8