Python Input Methods for Competitive Programming?


In this we are going to see different ways of I/O methods for competitive programming in Python. In competitive programming it is important to read the input as fast as possible so as take advantage over others.

Suppose you’re in a codeforces or similar online jude (like SPOJ) and you have to read numbers a, b, c, d and print their product. There are multiple ways to do, let’s explore them – one by one

One way to doing it is either through list comprehension and map function.

Method 1: Using a list comprehension

a, b, c, d = [int(x) for x in input().split()]
print(a*b*c*d)

Method 2: Using the map function

a, b, c, d = map(int, input().split())
print(a*b*c*d)

Another way of doing above problem is using stdin and stdout which is much faster.

Method 1a: List comprehension with stdin and stdout

from sys import stdin, stdout
a, b, c, d = [int(x) for x in stdin.readline().rstrip().split()]
stdout.write(str(a*b*c*d) + "\n")

Let’s look at another problem from the competitive programming where we can test our input and output methods on the problems. The problem is called INTEST-Enormous Input Test on SPOJ.

Input

The input begins with two positive integers n k (n, k<=107). The next n lines of input contain one positive integer ti, not greater than 109, each.

Output

Write a single integer to output, denoting how many integers ti are divisible by k.

Where

  • INPUT: The input begins with two positive integers n and k (where- n, k <=10). The next lines of input contain one positive integer t not greater than 10*9 each.

  • OUTPUT: A single integer denoting how many integers t are divisible by k.

For example

Input
7 3
1
51
966369
7
9
999996
11
Output
4

Method 1

One way to solve above problem is below though not the efficient one

def main():
   n, k = [int(c) for c in input().split()]
   cnt = 0
   for _ in range(n):
      t = int(input())
      if t % k == 0:
         cnt += 1
   print(cnt)

if __name__ == "__main__":
   main()

Method 2

Another more efficient way of solving above problem is using stdin and stdout. Below program runs much faster compared to the previous one.

from sys import stdin, stdout

def main():
   n, k = [int(c) for c in input().split()]
   cnt = 0
   for _ in range(n):
      t = int(stdin.readline())
      if t % k == 0:
         cnt += 1
   stdout.write(str(cnt))

if __name__ == "__main__":
   main()

Method 3

Another way to solve above problem which much faster than the previous two mentioned above is by using stdin and stdout (very similar to the way we used in method 2) however, read the whole input at once and load it into a list.

def main():
   for sys import stdin, stdout
   n, k = stdin.readline().split()
   n = int(n)
   k = int(k)

   cnt = 0
   lines = stdin.readlines()
   for line in lines:
      if int(line) % k == 0:
         cnt += 1
   stdout.write(str(cnt))

if __name__ == "__main__":
   main()

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements