Big Coffee 20 Line Python code to play the algorithm! Is it difficult to relate to algorithms? Subvert cognition!

Source: Internet
Author: User

The algorithm refers to the solution problem accurate and complete description, is the clear instruction to solve the problem, uses the systematic method to describe solves the problem the strategy mechanism.

LRU is one of the algorithms, so how to use Python to implement an LRU-based algorithm?

This blog post is about using Python to implement a cache based on the LRU algorithm.

Cache

As you can tell, when you enter the information you want to search in the input box of your browser, the browser sends a request to the server, and the server passes a series of operations before returning the data to the browser.

But if there are many browsers to access at the same time, then there will be a lot of repeated operations. This will cause waste of resources, waste of time, causing the lag, which is obviously not what we want.

LRU algorithm

Commonly used in page replacement algorithms, is for virtual page Storage Management Services.

Elimination Logic:

The cache is a list structure, and each space represents a cache capacity, which is obviously 8. The top and the end are a node, respectively, the head and tail.

When the number of data accesses increases, it is constantly being moved to the head of the list, with very little data being accessed, and the cache is squeezed.

20 Line Python code implementation LRU algorithm

Is the Python algorithm difficult? Difficult, but not as close to your fingertips. We can start from the easy, then the road will be more and more wide.

Big Coffee 20 Line Python code to play the algorithm! Is it difficult to relate to algorithms? Subvert cognition!

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.