DEV Community

Cover image for Using Python for Competitive Coding
Anurag Pandey
Anurag Pandey

Posted on

Using Python for Competitive Coding

I recently started using Python for competitive coding.These are some of minor tips and tricks that I have learnt to improve speed in Python and also different ways to could you run the test cases.
This would help with those TLE(Time Limit Exceeded).

Consider the following trivial problem:

Given a list of numbers, find the absolute minimum difference.

The first line contains the number of test cases. For each test case the first line is a single integer indicating number of numbers in list, and the second and final line contains list of numbers separated with space.

testcase

2
5
1 2 5 6 8
7
2 3 5 6 8 99 26
Enter fullscreen mode Exit fullscreen mode

sol.py

t = int(input())

for _ in range(t):
    n = int(input())
    arr = list(map(int, input().split()))
    arr.sort()
    mini = float('inf')
    for p, n in zip(arr, arr[1:]):
        mini = min(mini, abs(n-p))
    print(mini)
Enter fullscreen mode Exit fullscreen mode

This is one method to run the code with the given inputs from a file.

$ python sol.py < testcase
Enter fullscreen mode Exit fullscreen mode

To improve the read and write calls add the following code:

import sys

sys.stdin = open('testcase', 'r')

input = lambda : sys.stdin.readline().strip()
print = lambda s, end='\n' : sys.stdout.write(str(s) + end)
Enter fullscreen mode Exit fullscreen mode

This won't disrupt your current code and will improve the reading and writing speed(Test locally before submitting).
The line with sys.stdin = open('testcase', 'r') would enable to read directly from file instead of terminal.

If you are using vim editor, you can put the following line in your .vimrc configuration file and could run the file using t in command mode. And you would never have to leave the editor.

autocmd Filetype python nmap t <Esc>:w<CR>:!clear;python3.6 %<CR>
Enter fullscreen mode Exit fullscreen mode

(Change the python as per as your environment, e.g, python3 or python; Modify as per requirement)

If not using IDE or other editors, you could directly run on terminal as:

$ python sol.py
Enter fullscreen mode Exit fullscreen mode

While submitting the code, just comment the line with
sys.stdin = open('testcase', 'r') and you are good to go.

Use similar techniques as suitable to your environment for improving productivity while using Python for competitive coding.

If you work even faster and not waste time in commenting, use this

import sys

try:
    sys.stdin = open('testcase', 'r')
except:
    pass
Enter fullscreen mode Exit fullscreen mode

If the file exits it would take input from the file, else keep it unchanged.

I have battle tested the code on codechef, so the above tricks won't be a problem on similar platforms. If facing any problem comment below with the same.

P.S : There are some limitations to use the print function, such as using sep argument, passing comma separated values to print, but they are hardly required in competitive coding. Some easiness to lose to gain some speed. You could tweak it as per your needs.

Top comments (0)