Created
September 5, 2022 19:17
-
-
Save les-peters/8d1c6fe2deccadfc9c62a2bd7edbeb5f to your computer and use it in GitHub Desktop.
From To
This file contains 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
question = """ | |
Write a function fromTo that produces a generator, that will produce values in a range. | |
Usage: | |
let gen = fromTo(5,7) | |
> gen() | |
5 | |
> gen() | |
6 | |
> gen() | |
7 | |
> gen() | |
undefined | |
""" | |
class fromTo: | |
def __init__(self, min, max): | |
self.n=min | |
self.min=min | |
self.max=max | |
def __iter__(self): | |
return self | |
def __next__(self): | |
if self.n <= self.max: | |
result = self.n | |
self.n += 1 | |
return result | |
else: | |
return None | |
gen = fromTo(5,7) | |
print(next(gen)) | |
print(next(gen)) | |
print(next(gen)) | |
print(next(gen)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment