Recently, I started competitive programming with AtCoder to improve my skills. To be honest, I'm overwhelmed by the speed at which engineers can solve it, but it's fun to be able to solve it little by little.
The first thing that gets stuck when you're just starting out is how to handle the input of given data. A common problem for beginners is entering integers, entering N numbers, and so on. From the side of using Python, honestly, I don't use standard input so much (I'm not the only one ...?) Therefore, I would like to summarize the personal templates that are often used in the first input system.
What I'm actually using is sys.stdin.readline (), which reads line by line. I also read the line feed code, but I use it because it is easy to use. It is convenient to assign sys.stdin.readline to input in advance because it can be used only with input (). With this input, you get a string.
input.py
import sys
input = sys.stdin.readline
hogehoge = input()
If it is an example of input that is common when actually solving a problem (I made it appropriately)
3
1 5
2 8
9 15
In many cases, there is the number of lines to be entered first, and then multiple numbers are entered in that number of lines. Now, let's process this so that it is actually easy to use.
First, since the entered numerical value is a character string (str type), let's change it to an int. Also, since readline contains a line feed code, let's omit that as well. If you use rstrip (), the line feed code will be taken. Python can be converted to an int with int ().
input.py
import sys
input = sys.stdin.readline
hogehoge = int(input().rstrip())
This can be done when the input is only one character, but if you do it with multiple inputs such as the 2nd, 3rd and 4th lines, an error will be thrown. So, if you have more than one, use split () to separate them. I am very grateful that using split will process it by separating it with spaces. If you use split, it will come back as a list. Therefore, this cannot be converted with int () as it is. There are two methods, one is to convert each character to int using the for statement and the other is to use the map function. Personally, the map function is more beautiful.
input.py
import sys
input = sys.stdin.readline
hogehoge = [int(i) for i in input().rstrip().split()]
#If you use map
hogehoge = list(map(int, input().rstrip().split()))
I feel like this. Now you can get what you typed as a list. However, there are times when you don't want to get everything in a list. There are times when you want to get two inputs with two variables. In such a case, do it like this.
input.py
import sys
input = sys.stdin.readline
a, b = map(int, input().rstrip().split())
If you do this, you can even assign to a variable. For those who are more accustomed to it, the process of returning with list is written in lambda to simplify it. Please take a look at the source code of other people.
I personally had trouble with standard input, so I summarized it. I'm sorry if I make a mistake. It's hard to solve at first, but it's fun when you can solve it. Please enjoy competitive programming!
Recommended Posts