Question

In: Computer Science

(python please) The Cache Directory (Hash Table):The class Cache()is the data structure you will use to...

(python please)

The Cache Directory (Hash Table):The class Cache()is the data structure you will use to store the three other caches (L1, L2, L3). It stores anarray of 3 CacheLists, which are the linked list implementations of the cache (which will be explained later). Each CacheList has been already initialized to a size of 200. Do not change this. There are 3 functions you must implement yourself:

•def hashFunc(self, contentHeader)oThis function determines which cache level your content should go in. Create a hash function that sums the ASCII values of each content header, takes the modulus of that sum with the size of the Cache hash table, and accordingly assigns an index for that content –corresponding to the L1, L2, or L3 cache. So, for example, let’sassume the header “Content-Type: 2”sumsto an ASCII value of 1334. 1334% 3 = 2. So, that content would go in the L3cache. You should notice a very obvious pattern in which contentheaders correspond to which caches. The hash function should be used by your insert and retrieveContent functions.

•def insert(self, content, evictionPolicy)oOnce a content object is created, call the cache directory’s insert function to insert it into the proper cache. This function should call the linked list’s implementation of the insert function to actually insert into the cache itself. The eviction policywill be a string –either ‘lru’or ‘mru’. These eviction policies will be explained later. oThis function should return a message including the attributes of the content object inserted into the cache. This is shown in the doctests.

•def retrieveContent(self, content)oThis function should take a content object, determine which level it is in, and then return the object if it is in the cache at that level. If not, it should return a message indicating that it was not found. This is known as a “cache miss”. Accordingly, finding content in a cache is known as a “cache hit”. The doctests show the format of the return statements.

Solutions

Expert Solution

Code:

class Cache:
def __init__(self):
self.L1cache = []
self.L2cache = []
self.L3cache = []
for i in range(200):
L1cache.append("")
L2cache.append("")
L3cache.append("")
  
def hashFunc(self,contentHeader):
temp = 0
for i in contentHeader:
temp += ord(i)
return temp%3
  
def insert(self,content,evictionPolicy):
ci = hashFunc(content)
ei = findIndex(evictionPolicy,ci)
if(ci==0):
L3cache[ei] = content
elif(ci == 1):
L1cache[ei] = content
else:
L2cache[ei] = content
return "The content: "+ content + " has been inserted successfully."
  
def retrieveContent(self,content):
ci = hashFunc(self,content)
if(ci == 0):
for cont in self.L3cache:
if cont==content:
return "Cache hit" + content
return "Cache Miss"
elif(ci == 1):
for cont in self.L1cache:
if cont==content:
return "Cache hit" + content
return "Cache Miss"
else:
for cont in self.L2cache:
if cont == content:
return "Cache hit" + content
return "Cache Miss"

Note: I used an example Cache() class with given specifications. You'll have to change the variable names and add the function definition to your own Cache() class. If you want answer according to your Cache() class, please post the entire code provided to you. We'd be happy to help :)


Related Solutions

C++ only please Description A hash table is a data structure that is used to store...
C++ only please Description A hash table is a data structure that is used to store keys/value pairs. It is perfect to use when you have a large amount of directory-type information and the operations you need to perform are to insert, delete, print, and search. I am giving you all a lot more freedom in this program in that the value held in your hash table can be a pointer to any object created from your own custom class....
PYTHON--- regarding a hash table, why would it be bad to delete an item to successive...
PYTHON--- regarding a hash table, why would it be bad to delete an item to successive searches or insertions that uses open addressing
Use double hashing and the following hash functions: hash(key) = (key % table size) and hash2(key)...
Use double hashing and the following hash functions: hash(key) = (key % table size) and hash2(key) = (16 - (key % 10)) + 1 to insert the following numbers into a dictionary's hash table: 1, 12, 14, 3, 5 and 17. The table size is 11. Show the table and where each value is inserted. No coding required for this problem.
By using the following: -Python(you can use the filter function) -Data class -Functional Programming(don't use while...
By using the following: -Python(you can use the filter function) -Data class -Functional Programming(don't use while & for loops but you can use if statement) Write a program that reads table with given columns from input stream. Columns are name, amount, debt.(ONLY 3 columns, the person's first name and surname is under the column 'name'). Then filter the table (condition: debt is equal to 0). After that increase debt by 14% then output the results. ----------------------------------------- Example. User's Input: 3...
Please show it with python class Node {     int data;     Node left, right;    ...
Please show it with python class Node {     int data;     Node left, right;     public Node(int item)     {         data = item;         left = right = null;     } } public class BinaryTree { // Root of the tree implemented in Node class Node root; Node findLowestCommonAncestor(int node1, int node2) {     return findLowestCommonAncestor(root, node1, node2); } // This function returns pointer to LCA of two given // values node1 and node2. This function assumes that...
Python: Solve following problems using Linked List Data Structure 2. Create a Queue class. In the...
Python: Solve following problems using Linked List Data Structure 2. Create a Queue class. In the queue class create enqueue, dequeue, first, empty, len and resize methods. The class should support circular queue and have the ability to resize the queue.
Important: please use python. Using while loop, write python code to print the times table (from...
Important: please use python. Using while loop, write python code to print the times table (from 0 to 20, incremented by 2) for number 5. Add asterisks (****) so the output looks exactly as shown below.   Please send the code and the output of the program. ****************************************************************** This Program Shows Times Table for Number 5 (from 0 to 20) Incremented by 2 * ****************************************************************** 0 x 5 = 0 2 x 5 = 10 4 x 5 = 20 6...
Python Please Define a class that has one data attribute of type float. The initializer does...
Python Please Define a class that has one data attribute of type float. The initializer does not accept an argument, however, it initializes the data attribute to zero. Include in class the following methods: 1. func that will accept as two arguments. The first argument a value of float type and the second argument if string. The function will perform the following depending on the string argument: a. if string is '+', the first argument will be added to the...
Python Please (The Fan class) Design a class named Fan to represent a fan. The class...
Python Please (The Fan class) Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan. ■ A private bool data field named on that specifies whether the fan is on (the default is False). ■ A private float data field named radius that...
DROP DATABASE class;CREATE DATABASE class;Use class;drop table if exists Class;drop table if exists Student;CREATE TABLE Class...
DROP DATABASE class;CREATE DATABASE class;Use class;drop table if exists Class;drop table if exists Student;CREATE TABLE Class (CIN int PRIMARY KEY, FirstName varchar(255), LastName varchar(255), Gender varchar(1), EyeColor varchar(50), HairColor varchar(50), HeightInches int,CurrentGrade varchar(1));CREATE TABLE Student (SSN int PRIMARY KEY,FirstName varchar(255),LastName varchar(255), Age int,BirthMonth varchar(255),HeightInches int,Address varchar(255),City varchar(255),PhoneNumber varchar(12),Email varchar(255),FavColor varchar(255),FavNumber int);INSERT INTO Class VALUES(1, "David", "San", "M", "BRN", "BLK", 72, "-");INSERT INTO Class VALUES(2, "Jeff", "Gonzales", "M", "BRN", "BLK", 68, "B");INSERT INTO Class VALUES(3, "Anna", "Grayson", "F", "BRN", "BRN", 62,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT