LAB4

py

School

University of Pennsylvania *

*We aren’t endorsed by this school

Course

MISC

Subject

Computer Science

Date

Jan 9, 2024

Type

py

Pages

4

Uploaded by ColonelPencilSandpiper19

Report
# LAB4 # REMINDER: The work in this assignment must be your own original work and must be completed alone. class Node: # You are not allowed to modify this class def __init__(self, value=None): self.next = None self.value = value def __str__(self): return f"Node({self.value})" __repr__ = __str__ class Malloc_Library: """ ** This is NOT a comprehensive test sample, test beyond this doctest >>> lst = Malloc_Library() >>> lst <BLANKLINE> >>> lst.malloc(5) >>> lst None -> None -> None -> None -> None >>> lst[0] = 23 >>> lst 23 -> None -> None -> None -> None >>> lst[0] 23 >>> lst[1] >>> lst.realloc(1) >>> lst 23 >>> lst.calloc(5) >>> lst 0 -> 0 -> 0 -> 0 -> 0 >>> lst.calloc(10) >>> lst[3] = 5 >>> lst[8] = 23 >>> lst 0 -> 0 -> 0 -> 5 -> 0 -> 0 -> 0 -> 0 -> 23 -> 0 >>> lst.realloc(5) >>> lst 0 -> 0 -> 0 -> 5 -> 0 >>> other_lst = Malloc_Library() >>> other_lst.realloc(9) >>> other_lst[0] = 12 >>> other_lst[5] = 56 >>> other_lst[8] = 6925 >>> other_lst[10] = 78 Traceback (most recent call last): ... IndexError >>> other_lst.memcpy(2, lst, 0, 5) >>> lst None -> None -> None -> 56 -> None >>> other_lst 12 -> None -> None -> None -> None -> 56 -> None -> None -> 6925 >>> temp = lst.head.next.next
>>> lst.free() >>> temp.next is None True >>> node_1 = Node() >>> node_1 Node(None) >>> node_2 = Node(7) >>> node_2 Node(7) >>> node_3 = Node('hi') >>> node_3 Node(hi) """ def __init__(self): # You are not allowed to modify the constructor self.head = None def __repr__(self): # You are not allowed to modify this method current = self.head out = [] while current != None: out.append(str(current.value)) current = current.next return " -> ".join(out) __str__ = __repr__ def __len__(self): current=self.head count=0 while current!=None: count+=1 current=current.next return count def __setitem__(self, pos, value): current= self.head count=0 if pos>=len(self): raise IndexError else: while current!= None: if count==pos: current.value=value return None else: current=current.next count+=1 def __getitem__(self, pos): current= self.head count=0 if pos>=len(self): raise IndexError("Index out of range") if len(self)==0: return None
else: while current is not None: if count==pos: return current.value else: current=current.next def malloc(self, size): count=0 while count!=size: new_node=Node() if len(self)==0: self.head=new_node else: new_node.next=self.head self.head=new_node count+=1 def calloc(self, size): count=0 self.head=None while count!=size: new_node=Node(0) if len(self)==0: self.head=new_node else: new_node.next=self.head self.head=new_node count+=1 def free(self): current=self.head while self.head!=None: self.head=self.head.next current.next=None def realloc(self, size): if size!= len(self): if size==0: self.free() if size==1: self.head.next=None if size>len(self): count=0 while count!= (size-len(self)): new_node=Node() new_node.next=self.head self.head=new_node count+=1 if size<len(self): count=0
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
while count!= (size-len(self)): current=self.head while current.next.next!=None: current=current.next current.next= None count+=1 def memcpy (self, ptr1_start_idx, pointer_2, ptr2_start_idx, size): if self.head is None: return current1 = self.head count1 = 0 while current1 is not None and count1 < ptr1_start_idx: current1 = current1.next count1 += 1 if count1 < ptr1_start_idx: return current2 = pointer_2.head count2 = 0 while current2 is not None and count2 < ptr2_start_idx: current2 = current2.next count2 += 1 if count2 < ptr2_start_idx: return if size > 0: size = min(size, len(self)) else: return for i in range(size): current2.value = current1.value current1 = current1.next current2 = current2.next def run_tests(): import doctest doctest.testmod(verbose=True) if __name__ == "__main__": run_tests()