In 2017, when ACM allowed Python support for its prestigious competition, the ACM ICPC, a whole new community became interested in the sport of competitive programming. This meant more people coming back to the basics, learning algorithms that are the building blocks of complex packages they use to build their high level packages.
Unfortunately, not a lot of information exists out there on how to effectively use data structures and even the scoping rules of python which lead people to believe that python is subpar for competitive programming.
Today I’ll show you how python sometimes is even more powerful than C++ or Java thanks to its amazing libraries, and how simple it actually can be.
Let me demonstrate with a simple example, look at the following snippets of code-
You might think I’ve assigned an alias to the function ‘len’ and it might not make a difference.
So i wrote a performance testing function as follows.
I encourage you to try it on your systems.
Here’s the output on mine on running the performance.py script.
Okay, now let’s try to analyse why this happened. Reason? Lookup for a function is a costly operation.
In the second snippet, I stored the function directly in the scope of the function, so it doesn’t matter how many times I call it, each time the runtime knows exactly where it has to look for the results.
If you’ve been to codeforces, you know by now that the a lot of programming challenges involve backtracking. So today I’ll tell you about a library to generate all permutations and combinations using a inbuilt library package which is extremely fast. Itertools. If you’re looking to solve algorithmic challenges with python then itertools is library you must definitely explore.
To generate all permutations –
The combinations() functions behaves similarly I encourage the readers to try it on their own.
Python is a slow language only if your code is not leveraging the power of it successfully. Do not feel like you are at a disadvantage if you’re a python coder, it’s actually very neat and very quick!
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course