Question: Need help with Intro to Computer Science 2 (Python) problem: Complete the definition of the class OddList that is a subclass of list, of which
Need help with Intro to Computer Science 2 (Python) problem:
Complete the definition of the class OddList that is a subclass of list, of which a partial definition can be found in the template file posted to D2L. The class behaves just like an ordinary list except that it only prints the items in the odd-numbered (e.g. 1, 3, 5, ...) indices. To do this you will need to write the OddListIterator class which has two methods:
__init__ that initializes the list to be printed and the first index to be printed appropriately
__next__ that determines and returns the next item in the list, updating the variables of the class as needed
The template file (what is given):
#Subclass of list that iterates over odd indices class OddList(list): def __init__(self): pass
def __iter__(self): pass
class ListIterator: def __init__(self): pass
def __next__(self): pass
The following shows how the OddList class could be used:

Python 3.4.1 Shell File Edit Shell Debug Options Windows Help lst 1 2, 3, 4] lst 1 Odd List lst for num in lst 1 print (num) for num in lst print (num) lst2 Odd List cat i dog hermit crab for a in lst2 print (a, end dog lat Odd List C only for word in lst3 print (word) lstA Odd List for n in lstA print (n) Ln: 31 Col: 0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
