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!
- Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming)
- Python Input Methods for Competitive Programming
- Input/Output from external file in C/C++, Java and Python for Competitive Programming
- Input/Output from external file in C/C++, Java and Python for Competitive Programming | Set 2
- How can competitive programming help you get a job?
- C++ tricks for competitive programming (for C++ 11)
- A Better Way To Approach Competitive Programming
- How to begin with Competitive Programming?
- Fast I/O for Competitive Programming
- How to become a master in competitive programming?
- Bit Tricks for Competitive Programming
- How to read Competitive Programming Questions?
- Bitwise Hacks for Competitive Programming
- Some useful C++ tricks for beginners in Competitive Programming
- Knowing the complexity in competitive programming
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.