Last active
August 29, 2018 22:19
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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