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 "", line 1, in ?
aList.append( 5 )
File "", line 88, in append
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

Computer Science & Information Technology

You might also like to view...

On the Facebook Timeline, the ________ includes all your Facebook activity, including status updates, photo and video uploads, events, and so forth

A) right column B) far left side of the page C) far right side of the page D) left column

Computer Science & Information Technology

When you open a ________ from the default location on your computer, Excel opens a new copy of it as a workbook and preserves the original

Fill in the blank(s) with correct word

Computer Science & Information Technology