What seems to be a template of the standard input part of the competition pro in python3

at first

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.

Standard input in Python

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.

Easy to use the entered character string

Integer input

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())

Input of multiple integers

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.

Finally

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

What seems to be a template of the standard input part of the competition pro in python3
Python standard input summary that can be used in competition pro
Python3 standard input (competition pro)
I tried to implement what seems to be a Windows snipping tool in Python
Change the standard output destination to a file in Python
How to determine the existence of a selenium element in Python
How to check the memory size of a variable in Python
Read the standard output of a subprocess line by line in Python
How to check the memory size of a dictionary in Python
[Python] The role of the asterisk in front of the variable. Divide the input value and assign it to a variable
[Python] A program that calculates the number of socks to be paired
[Python] How to put any number of standard inputs in a list
I want to color a part of an Excel string in Python
I made a program to check the size of a file in Python
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
What is the fastest way to create a reverse dictionary in python?
A super introduction to Django by Python beginners! Part 2 I tried using the convenient functions of the template
Get the caller of a function in Python
Make a copy of the list in Python
Output in the form of a python array
Various ways to read the last line of a csv file in Python
How to pass the execution result of a shell command in a list in Python
A solution to the problem that the Python version in Conda cannot be changed
How to count the number of elements in Django and output to a template
Japanese translation of self-study "A Beginner's Guide to Getting User Input in Python"
Python script to get a list of input examples for the AtCoder contest
How to get a list of files in the same directory with python
How to get the number of digits in Python
Cut a part of the string using a Python slice
Can be used in competition pros! Python standard library
A reminder about the implementation of recommendations in Python
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Python Note: The mystery of assigning a variable to a variable
What does the last () in a function mean in Python?
Run the output code with tkinter, saying "A, pretending to be B" in python
How to identify the element with the smallest number of characters in a Python list?
How to check in Python if one of the elements of a list is in another list
A story about trying to introduce Linter in the middle of a Python (Flask) project
I tried to summarize what python strong people are doing in the competition professional neighborhood
Find out the apparent width of a string in python
How to debug the Python standard library in Visual Studio
Make a note of what you want to do in the future with Raspberry Pi
How to shuffle a part of a Python list (at random.shuffle)
How to use the __call__ method in a Python class
A collection of competitive pro techniques to solve with Python
Get the value of a specific key up to the specified index in the dictionary list in Python
Part 1 of receiving standard input
[For beginners] Summary of standard input in Python (with explanation)
Get the number of specific elements in a python list
A standard way to develop and distribute packages in Python
How to quickly count the frequency of appearance of characters from a character string in Python?
How to develop in a virtual environment of Python [Memo]
[Note] Import of a file in the parent directory in Python
How to get the last (last) value in a list in Python
I tried to create a Python script to get the value of a cell in Microsoft Excel
What beginners learned from the basics of variables in python
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
Find the eigenvalues of a real symmetric matrix in Python
I want to make input () a nice complement in python
How to pass the execution result of a shell command in a list in Python (non-blocking version)
[Python] A program to find the number of apples and oranges that can be harvested