From the course: Faster Python Code

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

bisect

bisect - Python Tutorial

From the course: Faster Python Code

Start my 1-month free trial

bisect

- [Instructor] Say you'd like to convert numerical scores to grades in the US system, where A is above 90, B is between 80 to 90, and so forth until the failed score below 60, which is F. Let's start with a simple implementation. I'll place this in a file called grades.py. As you see, we have a list called cutoffs and the names, and then a function on line six called grade, which just goes over the cutoffs, and once it find the grade, which is below the cutoff, returns the name. And we also have a test in line 17, which make sure that our code is working as expected. Once we have a test that passes, we can measure our code. We're going to use timeit module with ipython. So, we'll start ipython and we'll run the code grades.py. This will also run the test and since there are no errors, we're good. And now, let's time our code, timeit grade and let's say 74. This was 486 nanoseconds. If this time is acceptable, you should stop and work on something else. However, if you identified that…

Contents