-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator_example.py
More file actions
56 lines (39 loc) · 1.19 KB
/
generator_example.py
File metadata and controls
56 lines (39 loc) · 1.19 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import itertools
# Generators use yield to make a sequence
# been in Python3 forever
def fib_sequence():
"""
Infinite sequence
:return: A sequence of ints in the Fibonacci sequence, where the number is the sum of the two prior
"""
a, b = 0, 1
while True:
yield a
a, b = b, a + b
def print_fib_sequence() -> None:
for item in fib_sequence():
if item <= 50:
print(item)
else:
break
def print_fib_concise() -> None:
for item in itertools.takewhile(lambda x: x <= 50, fib_sequence()):
print(item)
def print_fib_squared() -> None:
fib_squared_sequence = (x * x for x in fib_sequence())
for item in itertools.takewhile(lambda x: x <= 100, fib_squared_sequence):
print(item)
def factors(number):
for i in range(2, int(number/2)+1):
if number % i == 0:
yield i
def print_finite_sequence():
sequence_of_factors_of_100 = factors(100)
sequence_of_factors_of_1000 = factors(1000)
factors_16 = list(factors(16))
print(factors_16)
if __name__ == '__main__':
print("** Fib Sequence")
print_fib_concise()
print("** Factors 16")
print_finite_sequence()