Solutions for LRU Algorithm

    • @TerryDic
    • @Robert, submitted in Python 2, achived 100% score
    def solve(processList,capacity):
        # List of current pages in Main Memory 
        s = []  
    
        pageFaults = 0
        # pageHits = 0 
    
        for i in processList: 
    
            # If i is not present in currentPages list 
            if i not in s: 
    
                # Check if the list can hold equal pages 
                if(len(s) == capacity): 
                    s.remove(s[0]) 
                    s.append(i) 
    
                else: 
                    s.append(i) 
    
                # Increment Page faults 
                pageFaults +=1
    
            # If page is already there in  
            # currentPages i.e in Main 
            else: 
    
                # Remove previous index of current page 
                s.remove(i) 
    
                # Now append it, at last index 
                s.append(i) 
    
        return "{}".format(pageFaults)
    
    • @TerryDic
    • @Robert, submitted in Python 3, achived 100% score
    def solve(processList,capacity):
        # List of current pages in Main Memory 
        s = []  
    
        pageFaults = 0
        # pageHits = 0 
    
        for i in processList: 
    
            # If i is not present in currentPages list 
            if i not in s: 
    
                # Check if the list can hold equal pages 
                if(len(s) == capacity): 
                    s.remove(s[0]) 
                    s.append(i) 
    
                else: 
                    s.append(i) 
    
                # Increment Page faults 
                pageFaults +=1
    
            # If page is already there in  
            # currentPages i.e in Main 
            else: 
    
                # Remove previous index of current page 
                s.remove(i) 
    
                # Now append it, at last index 
                s.append(i) 
    
        return "{}".format(pageFaults)