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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started