Question: Need help with my code. My put function isn't adding to my listString and therefore not giving me the correct output and I'm not sure
Need help with my code. My put function isn't adding to my listString and therefore not giving me the correct output and I'm not sure why. In quotes is example input/outputs. Anytime I 'put' one of the contents in the list, the list remains the same size. As an example, say I put 3 contents in, when I ask for the lst back, it only says 1 content is in. below is my code and attatchde are directions.
class CacheList:
'''
>>> content1 = ContentItem(1000, 10, "Content-Type: 0", "0xA")
>>> content2 = ContentItem(1004, 50, "Content-Type: 1", "110010")
>>> content3 = ContentItem(1005, 180, "Content-Type: 2", "
'CMPSC132'
")
>>> content4 = ContentItem(1006, 18, "another header", "111110")
>>> content5 = ContentItem(1008, 2, "items", "11x1110")
>>> lst=CacheList(200)
>>> lst
REMAINING SPACE:200
ITEMS:0
LIST:
>>> lst.put(content1, 'mru')
'INSERTED: CONTENT ID: 1000 SIZE: 10 HEADER: Content-Type: 0 CONTENT: 0xA'
>>> lst.put(content2, 'lru')
'INSERTED: CONTENT ID: 1004 SIZE: 50 HEADER: Content-Type: 1 CONTENT: 110010'
>>> lst.put(content4, 'mru')
'INSERTED: CONTENT ID: 1006 SIZE: 18 HEADER: another header CONTENT: 111110'
>>> lst
REMAINING SPACE:122
ITEMS:3
LIST:
[CONTENT ID: 1006 SIZE: 18 HEADER: another header CONTENT: 111110]
[CONTENT ID: 1004 SIZE: 50 HEADER: Content-Type: 1 CONTENT: 110010]
[CONTENT ID: 1000 SIZE: 10 HEADER: Content-Type: 0 CONTENT: 0xA]
>>> lst.put(content5, 'mru')
'INSERTED: CONTENT ID: 1008 SIZE: 2 HEADER: items CONTENT: 11x1110'
>>> lst
REMAINING SPACE:120
ITEMS:4
LIST:
[CONTENT ID: 1008 SIZE: 2 HEADER: items CONTENT: 11x1110]
[CONTENT ID: 1006 SIZE: 18 HEADER: another header CONTENT: 111110]
[CONTENT ID: 1004 SIZE: 50 HEADER: Content-Type: 1 CONTENT: 110010]
[CONTENT ID: 1000 SIZE: 10 HEADER: Content-Type: 0 CONTENT: 0xA]
>>> lst.put(content3, 'lru')
"INSERTED: CONTENT ID: 1005 SIZE: 180 HEADER: Content-Type: 2 CONTENT:
'CMPSC132'
"
>>> lst
REMAINING SPACE:0
ITEMS:3
LIST:
[CONTENT ID: 1005 SIZE: 180 HEADER: Content-Type: 2 CONTENT:
'CMPSC132'
]
[CONTENT ID: 1008 SIZE: 2 HEADER: items CONTENT: 11x1110]
[CONTENT ID: 1006 SIZE: 18 HEADER: another header CONTENT: 111110]
>>> lst.put(content1, 'mru')
'INSERTED: CONTENT ID: 1000 SIZE: 10 HEADER: Content-Type: 0 CONTENT: 0xA'
>>> lst
REMAINING SPACE:170
ITEMS:3
LIST:
[CONTENT ID: 1000 SIZE: 10 HEADER: Content-Type: 0 CONTENT: 0xA]
[CONTENT ID: 1008 SIZE: 2 HEADER: items CONTENT: 11x1110]
[CONTENT ID: 1006 SIZE: 18 HEADER: another header CONTENT: 111110]
>>> lst.find(1006)
CONTENT ID: 1006 SIZE: 18 HEADER: another header CONTENT: 111110
>>> lst
REMAINING SPACE:170
ITEMS:3
LIST:
[CONTENT ID: 1006 SIZE: 18 HEADER: another header CONTENT: 111110]
[CONTENT ID: 1000 SIZE: 10 HEADER: Content-Type: 0 CONTENT: 0xA]
[CONTENT ID: 1008 SIZE: 2 HEADER: items CONTENT: 11x1110]
>>> contentExtra = ContentItem(1034, 2, "items", "other content")
>>> lst.update(1008, contentExtra)
'UPDATED: CONTENT ID: 1034 SIZE: 2 HEADER: items CONTENT: other content'
>>> lst
REMAINING SPACE:170
ITEMS:3
LIST:
[CONTENT ID: 1034 SIZE: 2 HEADER: items CONTENT: other content]
[CONTENT ID: 1006 SIZE: 18 HEADER: another header CONTENT: 111110]
[CONTENT ID: 1000 SIZE: 10 HEADER: Content-Type: 0 CONTENT: 0xA]
>>> lst.clear()
'Cleared cache!'
>>> lst
REMAINING SPACE:200
ITEMS:0
LIST:
'''
def __init__(self, size):
self.head = None
self.maxSize = size
self.remainingSize = size
self.numItems = 0
def __str__(self):
listString = ""
current = self.head
while current is not None:
listString += "[" + str(current.value) + "] "
current = current.next
return 'REMAINING SPACE:{} ITEMS:{} LIST: {}'.format(self.remainingSize, self.numItems, listString)
__repr__=__str__
def __len__(self):
return self.numItems
def put(self, content, evictionPolicy):
# YOUR CODE STARTS HERE
nn = Node(content)
ev = evictionPolicy
if nn.value.size > self.maxSize:
return "Insertion not allowed. Content size is too large."
elif self.head == None:
self.head = self.tail = nn
self.numItems +=1
self.remainingSize -= nn.value.size
return "INSERTED:{}".format(nn.value)
elif ev == 'mru':
while nn.value.size > self.remainingSize:
self.mruEvict()
return "INSERTED:{}".format(nn.value)
elif ev == 'lru':
while nn.value.size > self.remainingSize:
self.lruEvict()
return "INSERTED:{}".format(nn.value)
elif self.find(nn.value.cid):
return "Insertion of content item {} not allowed. Content already in cache.".format(nn.value.cid)
else:
return None
nn.next = self.head
self.head = nn
self.numItems += 1
self.remainingSize -= nn.value.size
return ('CONTENT ID: {} SIZE: {} HEADER: {} CONTENT: {}'.format(self.cid, self.size, self.header, self.content))
pass
def find(self, cid):
# YOUR CODE STARTS HERE
if self.numItems != 0:
curr = self.head
if curr.value.cid != cid:
while curr.next != None:
if curr.next.value.cid != cid:
curr = curr.next
else:
break
if curr.next == None:
return None
move = curr.next
curr.next = move.next
move.next = self.head
self.head = move
return self.head.value
return curr.value
return None
def update(self, cid, content):
# YOUR CODE STARTS HERE
if self.find(cid):
nn = Node(content)
temp = self.head
self.head = temp.next
temp.next = None
nn.next = self.head
self.head == nn
return "UPDATED: {}".format(content)
else:
return None
pass
def mruEvict(self):
# YOUR CODE STARTS HERE
removed = self.head
self.head = removed.next
self.numItems -=1
self.remainingSize += removed.value.size
pass
def lruEvict(self):
# YOUR CODE STARTS HERE
curr == self.head
if self.numItems == 1:
self.remainingSize += self.head.value.size
self.head = None
self.tail = None
self.numItems -= 1
else:
while curr.next.next != None:
curr = curr.next
removed = curr.next
curr.next = None
self.tail = curr
self.numItems -=1
self.remainingSize += removed.value.size
pass
def clear(self):
# YOUR CODE STARTS HERE
self.head = None
self.tail = None
self.remainingSize = self.maxSize
self.numItems = 0
return 'Cleared cache!'
pass

put(self, content, evictionPolicy) (15 pts) Adds nodes at the beginning of the list and evicts items as necessary to free up space. If the content is larger than the maximum size, do not evict anything. Otherwise, if there is currently not enough space for the content, evict items according to the eviction policy. If the content id exists in the list prior the insertion, content is not added into the list and the current content is moved to the beginning of the list. Input ContentItem content The content item to add to the list str evictionPolicy |The desired eviction policy (either 'Iru' or 'mru') Output str 'INSERTED: contentItem' if insertion was successful str "Insertion not allowed. Content size is too large.' if content size > maximum size str Insertion of content item id not allowed. Content already in cache.' if id is already present in the list find(self, cid) (15 pts) Finds a ContentItem from the list by id, moving the ContentItem to the front of the list if found. Input int cid The id to search for in the CacheList Output ContentItem The matching ContentItem None None is returned if no match is found update(self, cid, content) (10 pts) Updates a ContentItem with a given id in the list. If a match is found, it is moved to the beginning of the list and the old ContentItem is entirely replaced with the new ContentItem. You can assume the size of the content will not change while updating it. Input int cid The id to search for in the CacheList ContentItem content The values to update the existing ContentItem with Output ContentItem The updated ContentItem None None is returned if no match is found Iru Evict(self) / mruEvict(self) (5 pts each) Removes the last (least recently used) or the first (most recently used) item of the list. Output None This function returns nothing
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
