Menu

[solved]-Using Python Class Create Class Called Unsortedlist Rename Class Unsortedlist2 First Ensur Q39014745

Using Python,

In class we create a class called UnsortedList. Rename the classto UnsortedList2.

First, ensure that anytime the structure of the list is altered,a call to getNextItem will return the first time. This idea wasdiscussed in class but was not fully implemented in the code.

Next, add the following methods to the class.

def replace(newString, oldString):

– Replaces ALL occurances of oldString with newString.

– Returns the number of replacements made (which may be 0).}

def count(valueToCount):

– Return the number of occurrences of valueToCount

def partialMatch (itemToFind=None):

– There are two ways that you can look at this method. IFitemToFind is set to anything other than None return the firststring that contains itemToFind, otherwise return None. IfitemToFind is set to None, return the next string that containsitemToFind – return None if there are no additional matches. Thiswill need to use information derived from the call to partialMatchwith itemToFind not set to None to define your search. This methodessentially requires partialMatch(stringTFind) be called beforecalling partialMatch(). Calling partialMatch (String) once and thenpartialMatch() continuously until it returns None will return allthe items in the data structure that contain itemToFind. Throw aException if there is no search criteria for the initial search.Once partialMatch returns None that search is consider completedand the next time this method is called it will be considered theinitial search.

The UnsortedList is as follows:

class UnsortedList:

def __init__ (self, maxSize=10):
self.__data = [None] * maxSize #create the entire list that couldever be used
self.__numOfItems = 0
self.__maxSize = maxSize
self.__currentItem = 0
  
def add (self, itemToAdd):
if not self.isFull():
self.__data[self.__numOfItems] = itemToAdd
self.__numOfItems += 1
return True
else:
return False

def add2 (self, itemToAdd):
if not self.isFull():
self.__data[self.__numOfItems] = itemToAdd
self.__numOfItems += 1
else:
raise Exception (“Unable to add ” + itemToAdd + ” to a fullUnsortedList”)

def isInList (self, itemToFind):
return self.__findIndexOf(itemToFind) >= 0
#if self.__findIndexOf(itemToFind) >= 0:
# return True
#else:
# return False
  
def __findIndexOf (self, itemToFind):
for index in range (len(self.__data)):
if itemToFind == self.__data[index]:
return index
return -1 #itemToFind is not in the list   

def remove (self, itemToRemove):
for index in range (len(self.__data)):
if itemToRemove == self.__data[index]:
#remove item
for index2 in range (index, self.__numOfItems – 1):
self.__data[index2] = self.__data[index2 + 1]
self.__numOfItems -= 1
return True

return False
  
#the better version of remove
def remove2 (self, itemToRemove):
#for index in range (len(self.__data)):
# if itemToRemove == self.__data[index]:
index = self.__findIndexOf (itemToRemove)
if index >= 0:
#remove item
self.__data[index] = self.__data[self.__numOfItems – 1]
self.__numOfItems -= 1
return True
else:
return False
  
def isFull(self):
return self.__numOfItems == self.__maxSize

def isEmpty(self):
return self.__numOfItems == 0
  

def size(self):
return self.__numOfItems

def reset(self):
self.__currentItem = 0

def getNextItem(self):
if self.__currentItem == self.__numOfItems:
e = GetNextItemError (“Trying to retreive item after end oflist”)
raise e
  
itemToReturn = self.__data[self.__currentItem]
self.__currentItem += 1
return itemToReturn
  
def getNextItem2(self):
if self.__currentItem == self.__numOfItems:
self.reset() # self.__currentItem = 0

itemToReturn = self.__data[self.__currentItem]
self.__currentItem += 1
return itemToReturn

def resize(self, newSize):
if newSize <= len(self.__data):
self.__data = self.__data[:newSize]
else:
self.__data += [None] * (newSize – len(self.__data))
if self.__numOfItems >= newSize:
self.__numOfItems = newSize
self.reset()   

I have this code from class but I’m a little stuck on what to donext. Please help!

Expert Answer


Answer to Using Python, In class we create a class called UnsortedList. Rename the class to UnsortedList2. First, ensure that anyt… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *