Implement methods append, count, index, insert, pop, remove, reverse and sort for class SingleList. Review the description of list methods in Section 5.6—the corre- sponding SingleList methods should specify the same arguments and should return the same val- ue. Any new method that modifies the list should ensure that only unique values are inserted. The method should raise an exception if
the client attempts to insert an existing value. Also, implement methods __delitem__ and __contains__ to enable clients to delete list elements with key- word del or perform membership tests with keyword in.
What will be an ideal response?
```
# Simple class SingleList with list methods.
class SingleList:
def __init__( self, initialList = None ):
"""Initializes SingleList instance"""
self._list = [] # internal list, contains no duplicates
# process list passed to __init__, if necessary
if initialList:
for value in initialList:
if value not in self._list:
self._list.append( value ) # add original value
# string representation method
def __str__( self ):
"""Overloaded string representation"""
tempString = ""
i = 0
# build output string
for i in range( len( self ) ):
tempString += "%12d" % self._list[ i ]
if ( i + 1 ) % 4 == 0: # 4 numbers per row of output
tempString += "\n"
if i % 4 != 0: # add newline, if necessary
tempString += "\n"
return tempString
# overloaded sequence methods
def __len__( self ):
"""Overloaded length of the list"""
return len( self._list )
def __getitem__( self, index ):
"""Overloaded sequence element access"""
return self._list[ index ]
def __setitem__( self, index, value ):
"""Overloaded sequence element assignment"""
if value in self._list:
raise ValueError, \
"List already contains value %s" % str( value )
self._list[ index ] = value
def __delitem__( self, index ):
"""Overloaded sequence element deletion"""
del self._list[ index ]
def __contains__( self, value ):
"""Overloaded membership test (in)"""
return value in self._list
# overloaded equality operator
def __eq__( self, other ):
"""Overloaded == operator"""
if len( self ) != len( other ):
return 0 # lists of different sizes
for i in range( 0, len( self ) ):
if self.aList[ i ] != other.aList[ i ]:
return 0 # lists are not equal
return 1 # lists are equal
# list methods
def append( self, value ):
"""Appends a value to the list, raises exception if value is
already in list"""
if value in self._list:
raise ValueError, \
"List already contains value %s" % str( value )
return self._list.append( value )
def count( self, element ):
"""Returns number of occurrences (0 or 1) of element in list"""
return self._list.count( element )
def index( self, element ):
"""Returns index of first occurrence of element in list"""
return self._list.index( element )
def insert( self, index, element ):
"""Inserts element at specified index. Raises exception if
list already contains element"""
if element in self._list:
raise ValueError, \
"List already contains value %s" % str( value )
return self._list.insert( index, element )
def pop( self, index = -1 ):
"""Removes element from list"""
return self._list.pop( index )
def remove( self, element ):
"""Removes first occurrence of element"""
return self._list.remove( element )
def reverse( self ):
"""Reverses list in place"""
return self._list.reverse()
def sort( self ):
"""Sorts the list in place"""
return self._list.sort()
```
>>> aList = SingleList( [ 2, 10, 5 ] )
>>> aList.append( 8 )
>>> aList.append( 5 )
Traceback (most recent call last):
File "
aList.append( 5 )
File "
raise ValueError, \
ValueError: List already contains value 5
>>> aList.index( 5 )
2
>>> aList.insert( 2, 20 )
>>> print aList
2 10 20 5
8
>>> aList.sort()
>>> print aList
2 5 8 10
20
>>> aList.reverse()
>>> print aList
20 10 8 5
2
>>> aList.pop()
2
>>> print aList
20 10 8 5
>>> aList.remove( 20 )
>>> print aList
10 8 5
You might also like to view...
Which of the following are correct? Why are the others incorrect?
When a function having an array formal parameter is called, the formal array parameter … a) names a copy of the array argument. b) refers to exactly the same array as the calling program c) is passed the address of the argument, and the function needs further information about the array size to safely use an array parameter d) refers to the array using a name that is always different from the calling program's argument.
The primary difference between the ordered and unordered associative containers is ____________.
Fill in the blank(s) with the appropriate word(s).
The mail merge process requires just one file as input, a main document
Indicate whether the statement is true or false
What command could be used from a command prompt environment to stop a process?
A) STOP B) HALT C) DOWN D) TASKKILL