Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Input Methods for Competitive Programming?
In competitive programming, efficient input/output methods can significantly improve your solution's performance. Python offers several approaches for reading input, each with different speed characteristics.
Let's explore various I/O methods using a simple example: reading four numbers a, b, c, d and printing their product.
Basic Input Methods
Using List Comprehension
This method uses list comprehension to convert input strings to integers ?
a, b, c, d = [int(x) for x in input().split()] print(a * b * c * d)
Using map() Function
The map() function provides a cleaner syntax for type conversion ?
a, b, c, d = map(int, input().split()) print(a * b * c * d)
Fast I/O Using sys Module
For large inputs, using sys.stdin and sys.stdout provides better performance ?
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")
Practical Example: SPOJ INTEST Problem
Let's solve the "Enormous Input Test" problem to compare different approaches. The problem requires counting how many numbers are divisible by k.
Problem Statement
Input: First line contains two integers n and k (n, k ? 107). Next n lines contain one integer each (? 109).
Output: Count of numbers divisible by k.
Example
Input: 7 3 1 51 966369 7 9 999996 11 Output: 4
Solution Approaches
Method 1: Basic Input
Standard approach using input() function ?
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: Using stdin/stdout
Faster approach using sys module ?
from sys import stdin, stdout
def main():
n, k = map(int, stdin.readline().split())
cnt = 0
for _ in range(n):
t = int(stdin.readline())
if t % k == 0:
cnt += 1
stdout.write(str(cnt) + "\n")
if __name__ == "__main__":
main()
Method 3: Bulk Reading
Most efficient approach by reading all input at once ?
from sys import stdin, stdout
def main():
lines = stdin.read().strip().split('\n')
n, k = map(int, lines[0].split())
cnt = 0
for i in range(1, n + 1):
if int(lines[i]) % k == 0:
cnt += 1
stdout.write(str(cnt) + "\n")
if __name__ == "__main__":
main()
Performance Comparison
| Method | Speed | Best For |
|---|---|---|
input() |
Slowest | Small inputs, practice problems |
stdin.readline() |
Fast | Medium to large inputs |
stdin.read() |
Fastest | Very large inputs, time-critical problems |
Key Tips
- Use
stdin.readline().rstrip()to remove trailing newlines - For multiple test cases, read all input at once when possible
- Use
stdout.write()instead ofprint()for faster output - Avoid repeated
int()conversions in loops
Conclusion
For competitive programming, use sys.stdin and sys.stdout for large inputs. The bulk reading approach with stdin.read() provides the best performance for time-critical problems.
