Handling input efficiently is an essential skill in competitive programming. Whether you are solving problems on platforms like HackerRank, Codeforces, or LeetCode, managing input in different formats can significantly impact the performance and readability of your solutions. In this blog, we’ll explore various input scenarios and how to handle them in Python. From single-line inputs to complex matrices, we’ve got you covered.
1. Single Integer Input
In many coding challenges, you might be required to accept a single integer, for example, the number of test cases or the size of an array.
Use Case:
You need to accept a single integer as input.
# Input: A single integer
n = int(input("Enter a number: "))
# Output the input numberprint(“You entered:”, n)
2. Multiple Integer Inputs on a Single Line
Some problems require multiple inputs on the same line, such as reading the dimensions of a matrix or accepting multiple values simultaneously.
Use Case:
Accept multiple integers separated by spaces.
# Input: Multiple integers in one line
a, b, c = map(int, input("Enter three numbers: ").split())
# Output the sum of the numbersprint(“Sum:”, a + b + c)
3. List of Integers Input
When you need to accept a list of integers, this method is useful for reading arrays or sequences.
Use Case:
Accept a list of integers on a single line.
# Input: A list of integers in one line
arr = list(map(int, input("Enter space-separated integers: ").split()))
# Output the listprint(“List of numbers:”, arr)
4. String Input
Many problems involve reading a string, whether it’s a word, sentence, or sequence of characters.
Use Case:
Accept a string and manipulate it.
# Input: A string
s = input("Enter a string: ")
# Output the string in reverseprint(“Reversed string:”, s[::-1])
5. Multiple Lines of Input
Some problems require handling inputs spread across multiple lines. This is especially common in problems with multiple test cases.
Use Case:
Accept inputs over several lines.
# Input: Multiple lines of input
n = int(input("Enter the number of test cases: "))
for _ in range(n):
x = input("Enter a string: ")
print("Input received:", x)
6. Matrix Input
Handling matrix input is common in problems involving grids, tables, or 2D arrays. You may be required to read the matrix row by row.
Use Case:
Accept a matrix as input.
# Input: Matrix dimensions
rows, cols = map(int, input("Enter rows and columns: ").split())
# Input: Matrix elementsmatrix = []
for i in range(rows):
matrix.append(list(map(int, input().split())))
# Output the matrix
print(“Matrix:”)
for row in matrix:
print(row)
7. Dictionary Input
When solving problems that require key-value pairs (e.g., word counts, frequency mapping), dictionaries are the best choice.
Use Case:
Accept dictionary inputs.
# Input: Number of key-value pairs
n = int(input("Enter number of items: "))
# Input: Dictionary itemsmy_dict = {}
for _ in range(n):
key, value = input(“Enter key and value: “).split()
my_dict[key] = int(value)
# Output the dictionary
print(“Dictionary:”, my_dict)
8. Tuple Input
Tuples are useful for storing pairs or more data points, such as coordinates, results, or relations.
Use Case:
Accept tuples in a list.
# Input: Number of tuples
n = int(input("Enter number of tuples: "))
# Input: List of tuplestuple_list = []
for _ in range(n):
a, b = map(int, input(“Enter two numbers: “).split())
tuple_list.append((a, b))
# Output the list of tuples
print(“List of tuples:”, tuple_list)
9. Floating Point Input
Some problems require handling floating-point numbers, especially in domains like geometry or physics, where precision matters.
Use Case:
Accept floating-point numbers as input.
# Input: Floating-point numbers
x, y = map(float, input("Enter two floating point numbers: ").split())
# Output their productprint(“Product:”, x * y)
10. Custom Delimiters
In certain problems, inputs may be separated by custom delimiters such as commas or semicolons.
Use Case:
Accept inputs separated by custom delimiters (e.g., commas).
# Input: Comma-separated values
values = list(map(int, input("Enter comma-separated values: ").split(',')))
# Output the list of valuesprint(“List of values:”, values)
Conclusion
Efficient input handling is a fundamental skill in competitive programming and problem-solving. Whether you’re reading single integers or complex matrices, the right approach can simplify your code and make it more readable. By mastering these techniques, you’ll be better equipped to tackle diverse coding challenges and optimize your solutions.
Happy coding! 🎉