thekushalghosh's blog

By thekushalghosh, 4 years ago, In English

Hello CodeForces Community,

Input / Output in Python can be sometimes time taking in cases when the input is huge or we have to output many number of lines, or a huge number of arrays(lists) line after line.

I have come across many questions on CodeForces where the style of taking input and printing makes your code quite faster.

For Input :-

Normally, the input is taken from STDIN in the form of String using input(). And this STDIN is provided in the Judge's file. So why not try reading the input directly from the Judge's file using the Operating system(os) module, and input / output(io) module. This reading can be done in the form of bytes.

The code for that would be :-

import io,os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

or a native method (which can be used for not too large inputs) :-

import sys
input = sys.stdin.readline

Now for Output :-

Instead of throwing the output to the STDOUT, we can try writing to the Judge's sytem file. The code for that would be to use sys.stdout.write instead of print. But remember you can only output strings using this, so convert the output to string using str or map.

Examples :-

For printing an integer, instead of

print(n)

Use :-

sys.stdout.write(str(n) + "\n")

For printing a list of integers, instead of

print(*list)

Use :-

sys.stdout.write(" ".join(map(str,list)) + "\n")

Now Program examples On CodeForces, where this was useful :-

Question 1
TLE Solution
AC Solution

Question 2
Earlier Solution
Faster Solution

Full text and comments »

  • Vote: I like it
  • +19
  • Vote: I do not like it

By thekushalghosh, history, 5 years ago, In English

Hello Codeforces community,

A few months ago, I had noticed a fact and had written a blog on how the Double division operator should be used for floor division instead of math.floor() in Python. This is because the single division operator behaves abnormally for very large numbers in Python, and thus math.floor() on them would work incorrectly.

Link to my blog.

Today in Codeforces Educational Round #86 Question C, I first submitted a solution in which I used the Single Division Operator in floor division and got WA. But then I realised the large constraints which could cause an error here, so on changing the single division operator to Double Division Operator, I got AC.

My WA Solution

My AC Solution

This is the first time my blog has helped me in a Codeforces contest, so I thought of sharing this with the community.

PS: Please visit the blog to know the insight I am talking about.

Full text and comments »

  • Vote: I like it
  • +18
  • Vote: I do not like it

By thekushalghosh, history, 5 years ago, In English

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.

Full text and comments »

  • Vote: I like it
  • +26
  • Vote: I do not like it

By thekushalghosh, history, 5 years ago, In English

Hey Codeforces Community. This is to remind everyone that we have the Facebook Hackercup 2019 Round 1 this weekend from 29th June 10:00 AM (PDT) to 30th June 10:00 AM (PDT). Click here to see when this event starts in your time zone.

Best of luck to everyone. Thank You.

Full text and comments »

  • Vote: I like it
  • +37
  • Vote: I do not like it