Python in Competitive Programming

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-

filter_none

edit
close

play_arrow

link
brightness_4
code

alphabets = ['a', 'b', 'c']
for item in alphabets:
    len(item) 

chevron_right


filter_none

edit
close

play_arrow

link
brightness_4
code

alphabets = ['a', 'b', 'c']
fn = len
for item in alphabets:
        fn(item)

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

import datetime
alphabets = [str(x)for x in range(10000000)]
a = datetime.datetime.now() # store initial time
for item in alphabets:
    len(item)
b = datetime.datetime.now() # store final time
print (b-a).total_seconds() # results
a = datetime.datetime.now()
fn = len                    # function stored locally
for item in alphabets:
    fn(item)
b = datetime.datetime.now()
print (b-a).total_seconds()

chevron_right


I encourage you to try it on your systems.
Here’s the output on mine on running the performance.py script.



Almost half!

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.

Itertools
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 –

filter_none

edit
close

play_arrow

link
brightness_4
code

from itertools import permutations
perm = permutations([1, 2, 3], 2)
for i in list(perm):
    print i
  
# Answer->(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)

chevron_right


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!



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.