|
1464 | 1464 | "\n", |
1465 | 1465 | "The [Fibonacci Sequence](https://en.wikipedia.org/wiki/Fibonacci_number) is a famous concept from mathematics, originally developed to model the growth of populations. Please read the Wikipedia article [here](https://en.wikipedia.org/wiki/Fibonacci_number) to get a basic understanding of how to calculate numbers in the Fibonacci Sequence (aka \"Fibonacci numbers\") by hand.\n", |
1466 | 1466 | "\n", |
1467 | | - "In this exercise, we ask you: how would you write a Python program to implement the `n`-th Fibonacci number?\n", |
1468 | | - "\n", |
1469 | | - " Implement fibonacci sequence for a given number" |
| 1467 | + "In this exercise, we ask you: how would you write a Python program to implement the `n`-th Fibonacci number?" |
1470 | 1468 | ], |
1471 | 1469 | "id": "super-kinase" |
1472 | 1470 | }, |
|
1488 | 1486 | "id": "1jXj6QVCuB4W" |
1489 | 1487 | }, |
1490 | 1488 | "source": [ |
1491 | | - "n = 20" |
| 1489 | + "n = 20 # fib(20) equals 6,765 by hand" |
1492 | 1490 | ], |
1493 | 1491 | "id": "1jXj6QVCuB4W", |
1494 | | - "execution_count": 45, |
| 1492 | + "execution_count": 52, |
1495 | 1493 | "outputs": [] |
1496 | 1494 | }, |
1497 | 1495 | { |
|
1774 | 1772 | "source": [ |
1775 | 1773 | "### Solution 5: Using a Generator\n", |
1776 | 1774 | "\n", |
1777 | | - "At this point, you may be wondering: \"how could we possibly optimize our function any further? I thought we had reached the best time and space complexity!\"\n", |
| 1775 | + "At this point, you may be wondering: \"how could we possibly optimize our function any further? I thought we had already reached the best time and space complexity!\"\n", |
1778 | 1776 | "\n", |
1779 | 1777 | "*Intro to Generators*\n", |
1780 | 1778 | "\n", |
1781 | 1779 | "Well, the truth is although our function has reached optimal performance (as measured by Big O), in can still perform slowly on large datasets. This is because our code executes *synchronously* - that is to say, we have to wait for it to finish completely, before getting the return value. \n", |
1782 | 1780 | "\n", |
1783 | 1781 | "In the real world, this means we can still run into issues on massively sized input data. For example, let's say you have a function to preprocess a dataset of 870,000 color images, each of which is 2532x1170 pixels. Once preprocessed, we will use these images to train a machine learning model.\n", |
1784 | 1782 | "\n", |
1785 | | - "Wouldn't you prefer being able to train the model on the first say, 100 images, as soon as they've been preprocessed, rather than having to wait for the function to work through all 870,000 images first? In the first scenario, this means we want to make our function *asynchronous* - we allow it to execute at the same time as another task is happening (in this case, our model training). In short, this help both tasks take less time overall.\n" |
| 1783 | + "Wouldn't you prefer being able to train the model on the first say, 100 images, as soon as they've been preprocessed, rather than having to wait for the function to work through all 870,000 images first? In the first scenario, this means we want to make our function *asynchronous* - we allow it to execute at the same time as another task is happening (in this case, our model training). In short, this allows both tasks to take less time overall.\n" |
1786 | 1784 | ], |
1787 | 1785 | "id": "HIq0p51Y_IWD" |
1788 | 1786 | }, |
|
1846 | 1844 | "source": [ |
1847 | 1845 | "**Option 1**: Using the `next()` function\n", |
1848 | 1846 | "\n", |
1849 | | - "In this option we iterate over the values generated by the `fib_generator`. In a given iteration, we are able to get the actual value that was computed by using the built-in `next()` function." |
| 1847 | + "In this option we iterate over the values generated by the `fib_generator` using an outer `for` loop. In a given iteration, we are able to get the actual value that was computed by using the built-in `next()` function." |
1850 | 1848 | ], |
1851 | 1849 | "id": "igFzEULLdV8b" |
1852 | 1850 | }, |
|
0 commit comments