Sqrt Iterable example 2

This commit is contained in:
Mert Gör 🇹🇷 2023-09-02 13:23:36 +03:00
parent fc8f0baf83
commit 2478a4b4f1
No known key found for this signature in database
GPG Key ID: 2100A876D55B39B9
2 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,34 @@
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)
l = list(SqrtIterable(100))
for f in l:
print(f)
print()

View File

@ -0,0 +1,5 @@
l = list(SqrtIterable(100))
for f in l:
print(f)
print()