Python - Competitive Programming Tricks

I know that python is slower than C and C++ and is not preferred by many for competitive programming but I want to practice programming in python. So I thought of practicing some problems in python. Are there any tricks to be followed in competitive programming like -

  1. Is there Fast IO in python? If yes, How?

  2. Should I use stdin.readline() or input() for getting user input?

  3. What is the difference between PYPY, PYTH and PYTH 3.1.2 in Codechef?

  4. Who are some regular python coders whom I can follow and refer their code for doubts?

Any other suggestions are also welcomed.

EDIT : The Execution Limit for Python is 5x than the time specified in the problem statement. As the Codechef IDE supports only 5sec Execution Time, will there be instances where my Python code may not run on Codechef IDE but will run in specified time in the submission? If this may happen, How will I handle it?

5 Likes

I prefer input() and the python coder u can follow is @mathecodician because he prefers python during the contests also.

In version 3.1.2 no use of raw_input and code is small in size.

1 Like

For your 4th part, two of the best coders in python i saw are-

  1. @meooow (he knows a variety of languages)
  2. @mathecodician

You can always send me a mail of your code if you urgently need help, but i know very little python and would need apt descriptions. Since i am a c++ user, i cant answer first three doubts, sorry for that. :slight_smile:

EDIT- You said you’d be practicing problems in python, just keep this fact in mind. There is some possibility of test cases having extra lines and/or blank spaces, and it messes up with python’s input. So if you get a WA, make sure to check if anyone else (who used python) has got a AC or not. If no such users are found, skip that Q as there might be faulty test cases.

1 Like

Answers to your questions

  1. Read - https://discuss.codechef.com/questions/9307/need-faster-io-for-python-programming
    http://stackoverflow.com/questions/7982146/fastest-stdin-out-io-in-python-3

Conclusion - Read all the input at once using os.read(0, some_big_enough_number) (fastest method)

  1. Use stdin.readline();

  2. http://python-guide-pt-br.readthedocs.io/en/latest/starting/which-python/

Excerpt from above link -
PyPy is a Python interpreter implemented in a restricted statically-typed subset of the Python language called RPython. The interpreter features a just-in-time compiler and supports multiple back-ends (C, CLI, JVM).

PyPy aims for maximum compatibility with the reference CPython implementation while improving performance.

If you are looking to increase performance of your Python code, it’s worth giving PyPy a try. On a suite of benchmarks, it’s currently over 5 times faster than CPython.

PyPy supports Python 2.7. PyPy3, released in beta, targets Python 3.
PYTH is python 2.7.

  1. As @vijju123 and @adhish_kapoor said you can follow @mathecodician and @meooow
2 Likes

Is python better for competitive programming or c++ or Java?

I know learning many programming languages don’t matter but still some languages would be more intuitive and contain more useful library functions. So, I am asking in this context.

I think one really nice python tip for people that just started doing CP is to look at itertools. It can often be nice for easier problems with small datasets where brute-force solutions work, but implementation is tricky. And if brute-force doesn’t work, it is often useful to quickly write a small brute force solution to generate a few valid answers to some randomly generated testcases to verify correctness and help with debugging of the real solution.

PYTH 3.1.2 is Python 3.x version. Hence it doesn’t support raw_input(). What about the other two?

No doubts.

1 Like

@only4 , i am sorry i didnt noticed you asked a Q in comments to me a few days ago. The karma requirement to edit others post is 2000. Try dropping me a mail in case i fail to answer any such query (esp. when posted as comments. They Kind of slip by…)

1 Like

Personally i will favour C++ dear. The reason is that, at times where there is some fault in test cases (like extra spacing or blank lines), python users suffer. C++ handles exceptions well and gets AC< but same thing in python gives WA due to error in input. Also, @mathecodician said that he was frustrated that one algo might AC in C++ but TLE in python despite the multiplier. So yes, since not all contests are error-free [its nothing but natural for errors to creep by], i would stick to C++.

1 Like

I feel that C++ is the best language for competitive programming. I have used C++ for all the contests till now. Only because I want to have problem solving experience in Python I want to try Python for competitive programming.

1 Like

I solved the same problem with input() and stdin.readline(). The input() solution was 0.02 secs faster.

please tell how to start CP with c++ , i only use C or Python,i know C++ only what college curriculum taught us. also please tell sources for c++

Derek Banas or The New Boston on YouTube is good for C++. I started from there, and then you’ll learn by practice.

1 Like

Hackerrank’s 30 day of code goes through quite a lot of stuff. It at least lets me know the in built data structures. Finding there application is easy -usually Div2 P3-P5 in long challenge on codechef will involve some data structures one way or other, and will be easy enough for you to solve.

2 Likes

I first fell in love with C++ when I went through this playlist. It’s really informative and covers quite a lot of things in C++, including modern C++(which they don’t teach you in college at all). You may even go through the posts of these people in Quora, they are like C++ Gods to me: \newline \bullet Joe Zbiciak, \newline \bullet David Vandevoorde, \newline \bulletKurt Guntheroth, \newline \bulletMario Galindo Queralt and \newline \bulletSergey Zubkov \newline After you practice a lot by trying to apply what you’ve learnt in problem solving, I’m sure you too will eventually morph into a C++ God. Hope this helps everyone trying to learn C++ or follow some epic people. :slight_smile:

1 Like