The path to python learning-simulate the LRU Algorithm in python Slicing,

Source: Internet
Author: User
Tags python list

The path to python learning-simulate the LRU Algorithm in python Slicing,
Problem description: A process just obtains the right to use three primary storage blocks. If the process accesses the page in the order of 1, 2, 3, 4, 2, 5, 5. When the LRU algorithm is used, what is the number of page missing times? Hint: LRU (Least Recently Used) indicates Least Recently Used.

This algorithm is often used in the page replacement algorithm. When the newly accessed page is not in the primary storage, we will remove the least recently used page from the primary storage and store the new page into the primary storage. You can use a queue to simulate this algorithm: The currently accessed webpage is at the end of the queue, and the most recently visited webpage is at the queue's header, if the newly accessed webpage is in the queue, the webpage will be moved to the end of the team, and the other pages will be moved forward in sequence. If the newly accessed webpage is not in the queue, the newly accessed webpage will be headed out of the queue and the other pages will be moved forward, new page to be accessed. A missing page is a page that is not accessed in the primary storage.

Use python to simulate the LRU algorithm:

List = [1, 2, 3, 4, 1, 5, 1, 2, 3, 4, 5] # This list stores the page to be accessed a_list = [] # This list is used to simulate the master memory in the LRU algorithm to store up to 3 count = 0 # record missing page tag = 1 # mark whether the page is missing for I in List: # loop of the list elements to be accessed if I not in a_list: # if the element to be accessed is not in a_list, It is a page missing count + = 1 tag = 1 if len (a_list) <3: # If a_list is not filled with a_list [len (a_list):] = [I] # equivalent to a_list.append (I), add element I to the end of a_list else: # If the list is filled with a_list [: 2:] = a_list [1:] # Use slice to replace the first two elements with the last two elements, function a_list [2:] = [I] # Move the I element to the last else of the list: # I element in the list tag = 0 a_list [a_list.index (I):] = a_list [a_list.index (I) + 1:] # Replace the elements starting with I and following the elements with the elements following the I element a_list [len (a_list):] = [I] # Insert the I element to the print (a_list, "missing page" if tag = 1 else "no page") behind the moved list ") print ("missing pages:", count)
Calculation Result:

 

Summary:

The slice in the python list returns a new list, which is a shallow copy of the original list. operations on the List directly returned by the slice can change the original list, however, if the list returned by the slicing operation is assigned to a new variable, the address of the original list is not copied, and the operation on the new list does not affect the original list; the corresponding "=" value assignment operation is a reference of a deep copy.

 

Related Article

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.