The class behaves like an ordinary string except that when printed using a for l
ID: 3673618 • Letter: T
Question
The class behaves like an ordinary string except that when printed using a for loop the elements are starting in the middle and every third character is printed.
To complete the mid3string class you will write the iterator for it, which involves writing the __next__ method of the mid3stringIter class. The __next__ method determines if there are any elements left in the internal list to be returned. If there are, it saves the next element to be returned, updates the variables in the iterator appropriately, and returns the element it saved. If there are no more elements in the string to be returned, the function raises a StopIteration exception
class mid3StringIterator:
'create an iterator that starts in the middle of a string and moves to the back printing every third one'
def __init__(self, s):
def __next__(self):
simple code :
class mid3string(str):
def __init__(self, l):
self.l = l
def __iter__(self):
return mid3StringIterator(self.l)
class mid3StringIterator(str):
'create an iterator that starts in the middle of a string and moves to the back printing every third one'
def __init__(self, s):
self.s= s
something=[]
for x in str:
if x.isdigit():
something.append(x)
self.index=len(something)-1
else:
pass
def __next__(self):
if self.index<0:
raise StopIteration
res=self.l[self.index]
self.index +=3
return res
Explanation / Answer
I have Some Changes done in the code
# cook your code here
class mid3StringIterator(str):
#printing every third one'
def __init__(self, s,n):
self.s= s
self.n =n
something=[]
for x in str:
if x.isdigit():
something.append(x)
self.index=len(something)
else:
pass
def __iter__(self):
return self
def __next__(self):
if self.index< self.n:
index =self.index
self.index +=1
return index
else:
raise StopIteration()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.