5) h. Generators and Generator Expressions Examples




Marquess Samarkanden Obnoxsenssen wants to mark how many times his sworn archenemy Duke of Suspex trespasses the frontiers of his land. For that, he writes a loop in Python that iterates over a generator. Write a simple example of such a generator and check it within a driver code (Hint: use ‘def’ and ‘yield’).

1
2
3
4
5
6
7
8
9
10
11
Output:
Total encounters
Day 1 status :  0
Day 2 status :  0
Day 3 status :  3
Day 4 status :  4
Day 5 status :  4
Day 6 status :  5
Day 7 status :  5
Day 8 status :  7
Day 9 status :  9




Duke wants to strike back. He has a list ‘code_list’ of secret code numbers [3, 66, 999…] that wants to use an iterator. But he needs to print out each element, one after the other in a controlled manner. First, create a generator from Duke’s list, then use this to print each element. How many times can you print before raising an error? (Hint: use iter() and next).

1
2
3
4
5
6
7
8
9
10
Output:
we can call next a total of  7 times.
Secret code
3
66
999
12121212
151515
1818
21




Duke wants to protect his secret codes and encodes them. He wants to write a list ‘code2’ where each element is the n-th element of ‘code_list’ squared, in one line of code. (Hint: use list comprehension).

1
2
Output:
New code list =  [1089, 4356, 9801, 1468944, 2295225, 3305124, 4498641]




Marquess has managed to steal Duke’s secret codes! He wants to make fun of him and performs the same encoding as in ‘code2’ list and then prints all of its elements by involving generators. Do that via ‘generator expression‘.
Recall that generator expression = creating a generator in a single line. It’s classy and considered Pythonic!
Do you see any similarity with the list comprehension? Differences? Any advantages? (Comment: generator expressions take much less memory).

1
2
3
4
5
6
7
8
9
Output:
Type of object <generator object <genexpr>>
1089
4356
9801
1468944
2295225
3305124
4498641

There’s one one big difference compared to the previous example.

This uses very little memory (because you are “generating” the next value in every for-loop cycle). Hence, it places a lesser load on the CPU and consumes very little memory.

Compared to the previous example where you created an entire whopping list first before printing it out. Imagine if you had been working with millions of data! All that data would have gotten loaded into the memory. So your program would consume more memory and stress the CPU a lot more. That’s why generators are important.




We found a log file from a suspicious famous fast-food chain. The log file has three columns, the first is the name of the order, the second element is the quantity and the last column contains the seller’s nickname.
We know that the food chain is selling weapons, we don’t know their types but we must know the total number of weapons sent. Write a program that calculates the total number of weapons sent. Be aware because sometimes they
are using fake rows to fool us. If the quantity of the order is not a number, that the order is a fake one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Output:
apple 5 jack
Total weapon sold =  5


banana 8 daniels
Total weapon sold =  8


apple 3 little
Total weapon sold =  3


pizza 9 bullet
Total weapon sold =  9


lasagne 8 bullet
Total weapon sold =  8


ananas 18 jack
Total weapon sold =  9


ananas 13 little
Total weapon sold =  4


juice apple daniels
Total weapon sold =  0




You are surrounded by angry mathematicians. You have to tell them the Fibonacci set’s 25th element, otherwise, they kill you. They might ask other numbers of the Fibonacci set, so be prepared for the worst scenario too. You have 5 minutes to calculate the whole Fibonacci set.
Write a program that calculates the next element from the Fibonacci set after each call. Don’t forget! Your life
depends on it.

1
2
3
Output:
The 25th fibonacci number: 121393
The 30th fibonacci number: 1346269