Please help me get this Mind - tap homework to validate
Programming Exercise 5.7
Complete the implementation of the link-based set defined in the new class named LinkedSet
that implements a set type using linked nodes.
Hint: Much of the code in the LinkedBag class from your solution of the linkedbag.py file in
Programming Exercise 5.5 can be reused.
In the linkedset.py file, complete the following:
Complete the accessor methods:
isEmpty(), returns true if len(self) == 0, or false otherwise.
__len__, returns the number of items in self.
__str__, returns the string representation of self.
__iter__, supports iteration over a view of self.
__add__, returns anew set containing the contents of self and other
clone, return a copy of self.
__eq__, returns true if self equals other, or false otherwise
count, return the numer of instance of item in self.
Complete the mutator methods:
clear, makes self become empty.
add, adds item to self and ignores the item if its already in the set.
Check array memory and increase it if necessary
remove, removes item from self if it's in self.
Check precondition and raise KeyError if necessary
Search for the node containing the target item, probe will point to the target node, and trailer will
point to the one before it, if it exists.
Unhook the node to be deleted, either the first one or one thereafter.
Decrement logical size
To test your program run the test method in the testset.py file.
Your program's output should look like the following:
Grading
Write your Python code in the code editor. Use the Run button to execute and run the code. To
review your progress, open up the "Tasks" panel on the left to view the curated list of tasks for
the project.
Once you are happy with the test results, click Submit and then the Confirm button to submit
your project for grading.
+++.Linkedbag.py+++
Reuse your solution from Programming Exercise 5.5 as your starter file
"""
from node import Node
from abstractbag import AbstractBag
class LinkedBag(AbstractBag):
def __init__(self,sourceCollection = None):
self._items = None
AbstractBag.__init__(self, sourceCollection)
def __iter__(self):
cursor = self._items
while not cursor is None:
yield cursor.data
cursor = cursor.next
def add(self, item):
self._items = Node(item, self._items)
self._size += 1
def append(self, item):
node = Node(item, None)
if self.isEmpty():
self._items = node
else:
cursor = self._items
while cursor.next != None:
cursor = cursor.next
cursor.next = node
self._size +=1
+++Linkedset.py+++
from node import Node
import copy
class LinkedSet(object):
"""A link-based set implementation."""
# Constructor
def __init__(self, sourceCollection = None):
"""Sets the initial state of self, which includes the
contents of sourceCollection, if it's present."""
#add the items in the LinkedSet
self.head = None
self.temp = self.head
self.count = 0
for num in sourceCollection[::-1]:
#check if the first element is None
if self.head is None:
self.temp = Node(num)
self.head = self.temp
.
Please help me get this Mind - tap homework to validateProgramming.pdf
1. Please help me get this Mind - tap homework to validate
Programming Exercise 5.7
Complete the implementation of the link-based set defined in the new class named LinkedSet
that implements a set type using linked nodes.
Hint: Much of the code in the LinkedBag class from your solution of the linkedbag.py file in
Programming Exercise 5.5 can be reused.
In the linkedset.py file, complete the following:
Complete the accessor methods:
isEmpty(), returns true if len(self) == 0, or false otherwise.
__len__, returns the number of items in self.
__str__, returns the string representation of self.
__iter__, supports iteration over a view of self.
__add__, returns anew set containing the contents of self and other
clone, return a copy of self.
__eq__, returns true if self equals other, or false otherwise
count, return the numer of instance of item in self.
Complete the mutator methods:
clear, makes self become empty.
add, adds item to self and ignores the item if its already in the set.
Check array memory and increase it if necessary
remove, removes item from self if it's in self.
Check precondition and raise KeyError if necessary
Search for the node containing the target item, probe will point to the target node, and trailer will
point to the one before it, if it exists.
Unhook the node to be deleted, either the first one or one thereafter.
Decrement logical size
To test your program run the test method in the testset.py file.
Your program's output should look like the following:
Grading
Write your Python code in the code editor. Use the Run button to execute and run the code. To
review your progress, open up the "Tasks" panel on the left to view the curated list of tasks for
the project.
Once you are happy with the test results, click Submit and then the Confirm button to submit
your project for grading.
2. +++.Linkedbag.py+++
Reuse your solution from Programming Exercise 5.5 as your starter file
"""
from node import Node
from abstractbag import AbstractBag
class LinkedBag(AbstractBag):
def __init__(self,sourceCollection = None):
self._items = None
AbstractBag.__init__(self, sourceCollection)
def __iter__(self):
cursor = self._items
while not cursor is None:
yield cursor.data
cursor = cursor.next
def add(self, item):
self._items = Node(item, self._items)
self._size += 1
def append(self, item):
node = Node(item, None)
if self.isEmpty():
self._items = node
else:
cursor = self._items
while cursor.next != None:
cursor = cursor.next
cursor.next = node
self._size +=1
+++Linkedset.py+++
from node import Node
3. import copy
class LinkedSet(object):
"""A link-based set implementation."""
# Constructor
def __init__(self, sourceCollection = None):
"""Sets the initial state of self, which includes the
contents of sourceCollection, if it's present."""
#add the items in the LinkedSet
self.head = None
self.temp = self.head
self.count = 0
for num in sourceCollection[::-1]:
#check if the first element is None
if self.head is None:
self.temp = Node(num)
self.head = self.temp
else:
self.temp.next = Node(num)
self.temp = self.temp.next
self.count += 1
# Accessor methods
def isEmpty(self):
"""Returns True if len(self) == 0, or False otherwise."""
#check if the head is None or not
if self.head:
return False
else:
return True
def __len__(self):
"""-Returns the number of items in self."""
#return the count
4. return self.count
def __str__(self):
"""Returns the string representation of self."""
ans = "Expect the set's string : {"
self.temp = self.head
while self.temp.next != None:
ans += str(self.temp.data)+ ","
self.temp = self.temp.next
ans += str(self.temp.data)+"}"
return ans
def __iter__(self):
"""Supports iteration over a view of self."""
self.temp = self.head
while self.temp is not None:
yield self.temp
self.temp = self.temp.next
def __add__(self, other):
"""Returns a new set containing the contents
of self and other."""
ans = set()
self.temp = self.head
while self.temp is not None:
ans.add(self.temp.data)
self.temp = self.temp.next
#for each element in the other linked list check if it is present in self
#if not the add it
other.temp = other.head
while other.temp is not None:
5. ans.add(other.temp.data)
other.temp = other.temp.next
return ans
def clone(self):
"""Returns a copy of self."""
temp = copy.copy(self.head )
return temp
def __eq__(self, other):
"""Returns True if self equals other,
or False otherwise."""
other.temp = other.head
self.temp = self.head
while self.temp.data and other.temp.data :
if self.temp.data != other.temp.data :
return False
other.temp = other.temp.next
self.temp = self.temp.next
if self.temp is None and other.temp is None:
return True
return False
# Mutator methods
def clear(self):
"""Makes self become empty."""
self.head = None
def add(self, item):
6. """Adds item to self."""
self.temp = self.head
#check if item is present in the linked list
while(self.temp.next != None):
if self.temp.data == item:
return
self.temp = self.temp.next
if self.temp.data != item:
self.temp.next = Node(item)
self.count += 1
def remove(self, item):
"""Precondition: item is in self.
Raises: KeyError if item in not in self.
Postcondition: item is removed from self."""
self.temp = self.head
while(self.temp.next.data != item):
self.temp = self.temp.next
self.temp.next = self.temp.next.next
self.count -= 1
++++testset.py+++++
from linkedset import LinkedSet
def test(setType):
"""Expects a bag type as an argument and runs some tests
on objects of that type."""
print("Testing", setType)
lyst = list(range(1, 11))
print("The list of items added is:", lyst)
s = setType(lyst)
print("Expect the set's string:", s)
7. print("Add same items to test for uniqueness:")
for i in range(1, 11):
s.add(i)
print("Expect the set's string:", s)
test(LinkedSet)
++++Node.py++++
class Node(object):
"""Represents a singly linked node."""
def __init__(self, data, next = None):
self.data = data
self.next = next