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 -
-
Is there Fast IO in python? If yes, How?
-
Should I use stdin.readline() or input() for getting user input?
-
What is the difference between PYPY, PYTH and PYTH 3.1.2 in Codechef?
-
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?
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-
-
@meooow (he knows a variety of languages)
- @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.
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
PYTH 3.1.2 is Python 3.x version. Hence it doesn’t support raw_input(). What about the other two?
Answers to your questions
- 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)
-
Use stdin.readline();
-
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.
- As @vijju123 and @adhish_kapoor said you can follow @mathecodician and @meooow
2 Likes
@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…)
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.
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++.
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.
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.