Skip to content

Instantly share code, notes, and snippets.

@arastu
Last active August 29, 2018 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arastu/1224df34285ea73551dbf81ca3ad6369 to your computer and use it in GitHub Desktop.
Save arastu/1224df34285ea73551dbf81ca3ad6369 to your computer and use it in GitHub Desktop.
Problem 1: Write an iterator class reverse_iter, that takes a list and iterates it from the reverse direction. https://anandology.com/python-practice-book/iterators.html
class reverse_iter:
def __init__(self, l):
self.l = l[::-1]
self.i = 0
def __iter__(self):
return self
def __next__(self):
if self.i < len(self.l):
i = self.i
self.i += 1
return self.l[i]
else:
raise StopIteration()
def next(self):
return self.__next__()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment