Created
July 17, 2018 22:32
-
-
Save solen003/66122b995d81bb657860605656156ba9 to your computer and use it in GitHub Desktop.
Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n.
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
n = int(input()) | |
divBy7 = [i for i in range(0, n) if (i % 7 == 0)] | |
print(divBy7) | |
def divChecker(n): | |
for i in range(n): | |
if i % 7 == 0: | |
value = True | |
else: | |
value = False | |
print(i, value) | |
divChecker(n) |
generator not used here.
Here generator is - range(n) . Suppose if n = 5 then range(5) has to gives the values 0,1,2,3,4 . Simply if u give range(5) it won't give the result . To get the resulting values we should use loop or list
list(range(5)) - gives the values [0,1,2,3,4]
or
for i in range(5):
print(i)
gives the result as
0
1
2
3
4
So here range(4) is called the generator
Here in this example this is a generator - divBy7 = [i for i in range(0, n) if (i % 7 == 0)]
since it gives the values from 0 to n which r divisible by 7
you didn't use class here rather made a function.
I think you had to explicitly use generator here.
Instead of [i for i in range(0, n) if (i % 7 == 0)] you could use ((i for i in range(0, n) if (i % 7 == 0)).
class Iter:
def __init__(self):
pass
def divid(self,n):
for i in range(0,n):
if i%7==0:
print(i)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
could u explain the code? Where is the generator used in this code?