Sqrt Iterable example 1
This commit is contained in:
parent
53d8d941f2
commit
fc8f0baf83
28
python-temel/SqrtIterable.py
Normal file
28
python-temel/SqrtIterable.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
import math
|
||||
|
||||
class SqrtIterable:
|
||||
def __init__(self, limit):
|
||||
self.limit = limit
|
||||
|
||||
def __iter__(self):
|
||||
return SqrtIterator(self.limit)
|
||||
|
||||
class SqrtIterator:
|
||||
def __init__(self, limit):
|
||||
self.limit = limit
|
||||
self.i = 0
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
if self.i == self.limit:
|
||||
raise StopIteration()
|
||||
self.i += 1
|
||||
|
||||
return math.sqrt(self.i)
|
||||
|
||||
s = SqrtIterable(25)
|
||||
|
||||
for f in s:
|
||||
print(f)
|
Loading…
Reference in New Issue
Block a user