Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generators #3026

Merged
merged 5 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Types of change:
### Changed
- [Html - Link Relative Paths - Change part of PQ as it wasn't worder properly](https://github.com/enkidevs/curriculum/pull/2985)
- [Python - Format Text Paragraphs With Textwrap - Make the fill method more clear](https://github.com/enkidevs/curriculum/pull/2981)
- [Python - Generators - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3026)
- [Python - Iterators - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3027)

## January 4th 2022
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Last insight, we've seen how **recursion** and **generators** can work together.

Consider the following example:

```plain-text
```python
def fibonacci():
#Generating fibonacci sequence
a, b = 0, 1
Expand All @@ -51,7 +51,7 @@ This is why we define the second **generator** called `firstn` which accepts two

Finally, we print a list containing the first 10 *elements* of the *Fibonacci sequence*:

```plain-text
```python
# Output:
# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
```
Expand Down Expand Up @@ -99,8 +99,7 @@ def n_power(g,n):
for i in range(n):
yield next(g)

print(list(n_power(
power_of_two(), 4)))
print(list(n_power(power_of_two(), 4)))
```

???
Expand Down
16 changes: 8 additions & 8 deletions python/functional-programming/generators/recursive-generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ Consider the following example:

```python
def infinity(start):
yield start
for x in infinity(start + 1)
yield x
yield start
for x in infinity(start + 1)
yield x
```

We defined a **generator** that counts up to infinity. During the first evaluation, the starting value will be **returned**. Then we loop on the new **generators** created in the `for`'s body.
Expand All @@ -43,8 +43,8 @@ Let's check out the example above implemented using `yield from`:

```python
def infinity(start):
yield start
yield from infinity(start + 1)
yield start
yield from infinity(start + 1)

gen = infinity(20)
print(next(gen)) # 20
Expand All @@ -64,9 +64,9 @@ Can you spot which of the following generators are recursive?

```python
def list_gen(l):
if l:
yield l[0]
yield from list_gen(l[1:])
if l:
yield l[0]
yield from list_gen(l[1:])

def cubic_generator(n):
for i in range(n):
Expand Down
14 changes: 7 additions & 7 deletions python/functional-programming/generators/yield-and-next.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ Consider the following generator:

```py
def range_gen(n):
i = 0
while i < n:
yield i
i += 1
i = 0
while i < n:
yield i
i += 1
```

This **function** generates all natural numbers up to `n`. Let's use the `next()` method now:
Expand Down Expand Up @@ -73,9 +73,9 @@ What is the output of the following snippet?

```py
def countdown(num):
while num > 0:
yield num
num -= 1
while num > 0:
yield num
num -= 1

>>> gen = countdown(5)
>>> print(next(gen))
Expand Down