- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
- Python 3 Advanced Tutorial
- Python 3 - Classes/Objects
- Python 3 - Reg Expressions
- Python 3 - CGI Programming
- Python 3 - Database Access
- Python 3 - Networking
- Python 3 - Sending Email
- Python 3 - Multithreading
- Python 3 - XML Processing
- Python 3 - GUI Programming
- Python 3 - Further Extensions
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.
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.
Write a single integer to output, denoting how many integers ti are divisible by k.
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.
Input 7 3 1 51 966369 7 9 999996 11 Output 4
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()
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()
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()
- Related Articles
- How good is Python for competitive programming?
- Input/Output from external file in C/C++, Java and Python for Competitive Programming
- C++: Methods of code shortening in competitive programming?
- C++ tricks for competitive programming
- C++ tricks for competitive programming (for C++ 11)?
- Some useful C++ tricks for beginners in Competitive Programming
- Python Tricks for Competitive Coding
- What are the pros and cons of using Python in competitive programming?
- Writing C/C++ code efficiently in Competitive programming
- Why is python best suited for Competitive Coding
- Methods in Dart Programming
- String Methods in Dart Programming
- IDE for Python programming on Windows
- Math class methods in Java Programming
- Tips for Students to Ace any Competitive Examination