Skip to content

Commit 931d848

Browse files
Added the generator concepts code files
1 parent e648ff4 commit 931d848

7 files changed

Lines changed: 165 additions & 1 deletion

generator_methods.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# .send method
2+
import asyncio
3+
4+
5+
def isPrime(n):
6+
if n < 2 or n % 1 > 0:
7+
return False
8+
elif n == 2 or n == 3:
9+
return True
10+
for x in range(2, int(n**0.5) + 1):
11+
if n % x == 0 :
12+
return False
13+
return True
14+
15+
def getPrimes():
16+
value = 0
17+
while True:
18+
if isPrime(value):
19+
i = yield value
20+
if i is not None:
21+
value = i
22+
value += 1
23+
24+
prime_gen = getPrimes()
25+
print(next(prime_gen))
26+
print(prime_gen.send(1000))
27+
print(next(prime_gen))
28+

infinite_sequence.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
a = range(5)
2+
print(list(a))
3+
4+
5+
def infinite_sequence():
6+
num = 0
7+
while True:
8+
yield num
9+
num += 1
10+
for i in infinite_sequence():
11+
print(i, end=" ")
12+
gen = infinite_sequence()
13+
print(next(gen))
14+
print(next(gen))
15+
print(next(gen))
16+
print(next(gen))
17+
18+
19+
def multiple_yield():
20+
value = "I'm here for the first time"
21+
yield value
22+
value = "My Second time here"
23+
yield value
24+
25+
multi_gen = multiple_yield()
26+
print(next(multi_gen))

lazy_evaluation.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Lazy evaluation is a programming technique in which the evaluation of an expression is delayed until its value is actually needed. This can be useful for improving the performance of programs, especially when dealing with large data sets or complex computations.
2+
3+
# In lazy evaluation, expressions are not evaluated immediately when they are created, but are instead stored in a data structure such as a thunk or a promise. When the value of the expression is eventually needed, the thunk or promise is "forced" to evaluate the expression and return its value. This can reduce the amount of unnecessary computation that is performed, because only the parts of the program that are actually used are evaluated.
4+
5+
#Sample program
6+
# Define a function that generates a sequence of Fibonacci numbers
7+
def fibonacci():
8+
a, b = 0, 1
9+
while True:
10+
yield a
11+
a, b = b, a + b
12+
13+
# Define a generator expression that generates the first 10 Fibonacci numbers
14+
fibonacci_sequence = (n for i, n in enumerate(fibonacci()) if i < 10)
15+
16+
# Print the first 10 Fibonacci numbers
17+
for n in fibonacci_sequence:
18+
print(n)

reading_files_using_generator.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def csv_reader(file_name):
2+
for row in open(file_name, "r"):
3+
yield row
4+
csv_gen = csv_reader("testfile.txt")
5+
row_count = 0
6+
7+
for row in csv_gen:
8+
row_count += 1
9+
10+
print(f"Row count is {row_count}")
11+
12+
# csv_gen = (row for row in open("testfile.txt"))

send_method_coroutine.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# import asyncio
2+
3+
# async def my_coroutine():
4+
# print('Coroutine started')
5+
# value = await asyncio.sleep(1)
6+
# print(f'Coroutine received value: {value}')
7+
# result = await asyncio.sleep(1)
8+
# print(f'Coroutine received result: {result}')
9+
10+
# async def main():
11+
# coro = my_coroutine()
12+
# print('Main coroutine started')
13+
# await asyncio.sleep(0.5)
14+
# print('Main coroutine sending value to my_coroutine')
15+
# await coro # This will start the coroutine and reach the first yield statement
16+
# try:
17+
# coro.send('Hello')
18+
# except StopIteration:
19+
# pass
20+
# await asyncio.sleep(0.5)
21+
# print('Main coroutine sending result to my_coroutine')
22+
# try:
23+
# coro.send(42)
24+
# except StopIteration:
25+
# pass
26+
27+
# asyncio.run(main())
28+
29+
# Sample program
30+
def isPrime(n):
31+
if n < 2 or n % 1 > 0:
32+
return False
33+
elif n == 2 or n == 3:
34+
return True
35+
for x in range(2, int(n**0.5) + 1):
36+
if n % x == 0:
37+
return False
38+
return True
39+
40+
def getPrimes():
41+
value = 0
42+
while True:
43+
if isPrime(value):
44+
i = yield value
45+
if i is not None:
46+
value = i
47+
value += 1
48+
49+
prime_gen = getPrimes()
50+
print(next(prime_gen))
51+
print(prime_gen.send(1000))
52+
print(next(prime_gen))

testfile.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
I love python
1+
I love python
2+
bfsbflfjkdbnfnqjbqqjbqkdlbdvqhjb dns dj dfkld d n dj dc j clsd;clscml;njvdv vnmsnkbdvdjvdmvnmabvnbvd dvkjdbvkbadkjvkadbjvbdkvbdkv
3+
vdjbvkjdvkjdbjvkbdkbvd
4+
nkbjdbfnkvbldfsvdvhdjvbdsfjsvbhkvfdvbjdhsbvjfbhvbjvbdfbvjbvhfbvfdvbfdjivbfkjvnfkbvfbvjbvjv
5+
bfjvjv fdjhvbdf vhbvjhflbvhfvmbvjbv bvjvndlvavnbvbdjv sjd dv dfjh vhjdfbvjdbvbad

yield_from.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# yield returns from a simple generator function, yield from allows you to yield from another generator function. This comes in handy when you need to use two functions in a generator such as when we did merge sort in the sorting
2+
def yieldOnly():
3+
yield "A"
4+
yield "B"
5+
yield "C"
6+
7+
def yieldFrom():
8+
for i in [1, 2, 3]:
9+
yield from yieldOnly()
10+
11+
test = yieldFrom()
12+
for i in test:
13+
print(i)
14+
15+
16+
# Sample program for yield from
17+
def sample_generator(i):
18+
for j in range(i):
19+
yield j
20+
21+
def yf_generator(i):
22+
yield from sample_generator(i)
23+
for value in yf_generator(5):
24+
print(value)

0 commit comments

Comments
 (0)