Hello Codeforces Community,
Actually all of the Python users over here would agree that "sometimes" we need to write a bit long for taking input in Python, and it is also slow.
That's why I made a good template for taking Quick And Fast Input, which I would like to share.
import sys
input = sys.stdin.readline
############ ---- Input Functions ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
Just paste this template at the beginning of your Code.
It comprises of 4 functions :-
1) inp — For taking integer inputs.
2) inlt — For taking List inputs.
3) insr — For taking string inputs. Actually it returns a List of Characters, instead of a string, which is easier to use in Python, because in Python, Strings are Immutable.
4) invr — For taking space seperated integer variable inputs.
The input = sys.stdin.readline
is actually for Faster Inputs, because line reading through System STDIN (Standard Input) is faster in Python.
This blog may be useful.
Thank You! This is so useful!
Hi, I just want to ask when to use sys.stdin.readline, because once I used it and it gave me wrong output (WA) and when I removed it, my code was Accepted. Here are my submissions:
With sys.stdin.readline: 108547882 Without: 108548171
sys.stdin.readline
also reads end of line"\n"
character. See here vs hereAlso, note how does the blog takes string input in
insr()
function. It explicitly removes the last character.can anyone share the full template for Python please
I won't say my template is the best out there for Python. But I think it might be of some use to you. Sharing it below, I have made separate ones for both Codechef and Codeforces. Here you go. Hope you find it helpful :)
@a_vantik_a Thanks! though I need to understand it all, looks daunting. Do you've a blog or something of that sort explaining it in chunks? Would be of big BIG help !
Hey, you are welcome. I am glad you found it helpful :) I have added commments to the code below, to make it a bit easy to understand. In case you want more reference or meaning about the code, you can simply google it. I referred a lot of blogs to come up with this FASTIO :)
Have a good day, cheers!
I know using Python in Codeforces on a long term basis isn't really a good idea. But I am doing it for my own research reasons. Here is the whole list of FASTIO Comparisons I did for Python3, to come up with the best case for both Codechef and Codeforces, results were taken using best of 3 runs for each template. Hope you find it helpful :)