WEDNESDAY EXERCISES
1. Time Module: import the time module and call the sleep function. make the cell sleep for 5 seconds, and then print “Time module imported”. Although we haven’t covered this module, this exercise will provide good practice for you to try and work with a module on your own. Feel free to use Google, Quora, etc.
2 . Calculating Area: Create a module named “calculation.py” that has a single function within it. that function should take in two parameters and return the product of them. We can imagine that we’re trying to calculate the area of a rectangle and it needs to take in the length and width properties. run the module within Jupyter notebook, and use the following function call within the cell:
>>> calcArea(15, 30)
today’s focus was all about modules, how to import them, how to use them, how to create our own, and how to call our own modules within Jupyter notebook. Understanding how modules work will give you the ability to work with frameworks in python. Flask, for example, uses a lot of different modules, as each module serves a specific purpose. When you need to keep your project organized, modules are the answer.
Thursday: Understanding Algorithmic Complexity
Throughout this book, we’ve been learning by doing. At the beginning, I spoke about how we wouldn’t go much into theory, but rather we would learn by building projects together and coding along. Today’s focus is primarily on the theory of programming and algorithms. If there is a theory in programming that you should understand, it should be Big O Notation.
To follow along with this lesson, let’s continue from our previous notebook file
“Week_09” and simply add a markdown cell at the bottom that says, “Understanding Algorithmic Complexity.”
What Is Big O Notation?
As a software engineer, you’ll often need to estimate the amount of time a program may take to execute. In order to give a proper estimate, you must know the time complexity of the program. This is where algorithmic complexity comes in to play, otherwise known as Big O Notation. It is the concept to describe how long an algorithm or program takes to execute. Take a list, for example. As the number of items within the list grows, so does the amount of time it takes to iterate over the list. This is known as O(n), where n represents the number of operations. It’s called Big O Notation because you put a “Big O” in front of the number of operations.
Big O establishes a worst-case scenario runtime. Even if you search through a list of 100 items and find what you’re looking for on the first try, this would still be considered O(100) because it could possibly take up to 100 operations.
The most efficient Big O Notation is O(1), also known as constant time. It means that no matter how many items or steps are required, it will always take the same amount of time and generally occurs instantly. If we took the same list of 100 items and accessed an index directly, this would be known as O(1). We would retrieve the value in that index immediately without needing to iterate over the list.
One of the least efficient time complexities is O(n∗∗2). This is a representation of a double loop. Our Bubble Sort algorithm that we wrote uses a double for loop and is known as one of the less efficient sorting algorithms in programming; however, it is simple to understand, so it makes for a good introduction into algorithms. We’ll see later today how Bubble Sort compares to another algorithm that is designed to be much more efficient.
ChAptEr 9 AdvAnCEd topiCs ii: ComplExity
When you compare a simple search that iterates through each element of a list to an efficient algorithm like Binary Search, you begin to see that they don’t grow at the same rate over time. Take Table 9-1 that illustrates the amount of time to search for a given item.
Do'stlaringiz bilan baham: |