Skip to content

Instantly share code, notes, and snippets.

@NimaBavari
Last active November 19, 2020 19:53
Show Gist options
  • Save NimaBavari/6d20a28094965ca20708a5d55aefecea to your computer and use it in GitHub Desktop.
Save NimaBavari/6d20a28094965ca20708a5d55aefecea to your computer and use it in GitHub Desktop.
class It:
"""An example of iterators."""
def __init__(self, max_, start=1, increment=1):
self.max_ = max_
self.start = start
self.increment = increment
self.counter = self.start - self.increment
def __iter__(self):
return self
def __next__(self):
self.counter += self.increment
if self.counter > self.max_:
raise StopIteration
return self.counter
it = It(101, 7, 3)
for i in it:
print(i)
class Fibonacci:
"""Fibonacci sequence implementation."""
def __init__(self, max_):
self.max_ = max_
self.current = 0
self.next = 1
def __iter__(self):
return self
def __next__(self):
self.current, self.next = self.next, self.current + self.next
if self.current > self.max_:
raise StopIteration
return self.current
fib = Fibonacci(12000)
for itm in fib:
print(itm)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment