Question: Using Python 3+ 2.) Implement a class intlist which a list that stores only integers. You MUST subclass list. Please note the following: constructor can

Using Python 3+

2.) Implement a class intlist which a list that stores only integers. You MUST subclass list. Please note the following:

constructor can be passed a list of ints, or, by default constructs an empty intlist.

append, insert,extend can also be used to add ints to an intlist. If you dont know how extend works for a list, look it up. All should raise errors if a non-int is added.

__setitem__ - can be used for item assignment using an index. Raises error if non-int is used.

odds() write a method odds() which returns an intlist consisting of the odd ints. They should not be removed from the original.

evens() same as odds(), but for even ints

NotIntError also write an Exception class NotIntError that subclasses Exception.

NotIntError a NonIntError should be raised when client code attempts to place something other than an int in an intlist. This can happen in three ways (all shown in code below):

append

insert

The constructor when passed a list that contains something other an int

Note: you can check whether item is an int by evaluating the expression type(item)==int

The following Doc Test must pass with 0 failed results for the program to be 100% complete:

##### intlist #####

>>> il = intlist() >>> il intlist([]) >>> il = intlist([1,2,3]) >>> il intlist([1, 2, 3]) >>> il.append( 5 ) >>> il intlist([1, 2, 3, 5]) >>> il.insert(1,99) >>> il intlist([1, 99, 2, 3, 5]) >>> il.extend( [22,44,66] ) >>> il intlist([1, 99, 2, 3, 5, 22, 44, 66]) >>> odds = il.odds() >>> odds intlist([1, 99, 3, 5]) >>> evens = il.evens() >>> evens intlist([2, 22, 44, 66]) >>> il intlist([1, 99, 2, 3, 5, 22, 44, 66]) >>> il[2] = -12 # calls __setitem__ >>> il intlist([1, 99, -12, 3, 5, 22, 44, 66]) >>> il[4] # calls __getitem__ 5

#trying to get non ints into an intlist #will always raise an error

>>> il.append(33.4) #doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... NotIntError: 33.4 not an int >>> il.insert(2,True) #doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... NotIntError: True not an int >>> il = intlist([2,3,4,"apple"]) #doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... NotIntError: apple not an int >>> il.extend( [2,3,'hello']) #doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... NotIntError: hello not an int >>> il[2] = 22.3 #doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... NotIntError: 22.3 not an int

#make sure using None as default argument

>>> lst1 = intlist() >>> lst1.append( 99 ) >>> lst2 = intlist() >>> lst2 intlist([])

#make sure that code really subclasses list #actually check that self has no attributes

>>> vars(lst1) # check that subclasses list {}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!