Skip to content

Commit

Permalink
Merge branch 'master' into functional-programming
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Stojanovic authored Jan 10, 2022
2 parents bf184a2 + 4a27333 commit 813adb4
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 33 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Types of change:
- [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 - Functional Programming - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3024)
- [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 - Python Immutability - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3028)
- [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
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ Take a look at this simple class, `MutableShoppingBasket`, representing a user's
class MutableShoppingBasket:
def __init__(self, itemcount):
if itemcount < 0:
raise ValueError("""You can't have
less than zero items in the basket!""")
raise ValueError("""You can't have less than zero items in the basket!""")
self.itemcount = itemcount

def increment_items(self):
Expand All @@ -44,8 +43,7 @@ class MutableShoppingBasket:
self.itemcount -=1

def __repr__(self):
return("Shopping Basket with " +
str(self.itemcount) + " items.")
return("Shopping Basket with " + str(self.itemcount) + " items.")
```

Can you see how this constraint could be broken? Let's do it:
Expand Down Expand Up @@ -92,9 +90,7 @@ What is the code snippet below an example of?
(Remember that the `Connection` class defaults to the last HTTP method used if one is not specified in `request()`. See the footnotes in the insight for more information.)

```python
conn = Connection(
http.client.HTTPConnection(
"httpbin.org", 80))
conn = Connection(http.client.HTTPConnection("httpbin.org", 80))
r1 = conn.request("POST")
r2 = conn.request("", "text=hello")
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ category: must-know
links:
- >-
[PYTHON OBJECTS: MUTABLE VS.
IMMUTABLE](https://codehabitude.com/2013/12/24/python-objects-mutable-vs-immutable/){website}
IMMUTABLE](https://www.geeksforgeeks.org/mutable-vs-immutable-objects-in-python/){website}
practiceQuestion:
formats:
- fill-in-the-gap
Expand Down Expand Up @@ -63,7 +63,7 @@ For example, consider the following code snippet:
```python
string = ""
for line in file:
string += str(line)
string += str(line)
```

In this case, while the code will execute and perform the functionality correctly, as we increase the size of the string it will become increasingly more inefficient. This is because of the immutability of the `string` type, which causes the concatenation operation performed at each iteration to create a whole new copy of the string. As we reach the end of a large file, every iteration of the loop will be creating and discarding a very large string, which could potentially be needlessly I/O intensive and a waste of memory.
Expand All @@ -73,7 +73,7 @@ With the knowledge of which data types are mutable, you can choose a better data
```python
list = [] # List is mutable!
for line in file:
list.append(str(line))
list.append(str(line))
"".join(list)
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ class Connection(object):
def post(self):
self.method = "POST"
# ^ mutates the Connection object
self.httpconnection.request(
self.method, "/")
self.result =
self.httpconnection.getresponse()
self.httpconnection.request(self.method, "/")
self.result = self.httpconnection.getresponse()
conn.result.read()
return self.result
```
Expand Down

0 comments on commit 813adb4

Please sign in to comment.