__ * This article is for those who are having trouble understanding standard input such as AtCoder and paiza. __
There are many types and it may seem difficult, but in reality, we are just selecting the one that matches the type of input.
At first, you don't have to remember it at all, so let's get used to it little by little, checking each time.
Note) __ This article is for beginners. __ Since we give priority to the understanding of beginners, some inadequate wording etc. are included, but we appreciate your understanding.
In Atcoder and paiza problems, there are two main considerations when choosing which code to receive standard input:
--What kind of input is (one line or multiple lines, character string or number) --How do you want to receive that input (put it in a variable, list it, etc.)
Of these, the second one will gradually become clear as you solve the problem repeatedly, so this article will focus on the first one.
input
abc #String
12 #String(Even if it looks like a number, it will be treated as a character string as it is)
code
source.py
a = input()
a = int(input())
output.py
abc #String
12 #Numbers
__input () is a function that receives input as a one-line string. __ (Since a in a = input () is a variable name, you can change it freely.) In a = input (), the string abc received by input () is stored in the variable a.
__int () is a function that converts the inside of () to a number (integer). __ If you want to receive numbers, use __int () __. Int (input ()) converts what was received by input () (still a string) to an integer with int ().
input
abc
code
source.py
list_a = list(input())
output.py
['a', 'b', 'c']
You can convert a string to a list with __list () __. It's not very frequent, but it's easy, so let's make it available.
input
apple lemon grape
code
source.py
list_a = input().split()
red, yellow, purple = input().split()
output.py
['apple', 'lemon', 'grape']
apple #print(red)
__split () is a function that splits a string into a list. __ If multiple inputs are separated by spaces, split () can be used to make each element a list. If you want to receive them one by one, you need to prepare variables for each number. (If there are not enough numbers, an error will occur.)
input
1 2 3
code
source.py
list_a = list(map(int,input().split()))
x, y, z = map(int,input().split())
output.py
[1, 2, 3]
1 #print(x)
__map () is a function that applies another function to a list or the like. __ (Explained in a very simplified way) Use map () like map (function you want to use, list of targets, etc.). It's a little complicated, so let's break down the process of list (map (int, input (). Split ())).
You can also receive them individually in variables instead of listing them as you would for strings.
input
3
apple
lemon
grape
code
source.py
n = int(input())
list_a = [input() for i in range(n)]
output.py
['apple', 'lemon', 'grape']
For multi-line input, the number of lines of input is often given first. This code receives the number of input lines as n, and then stores n lines of input (3 lines this time) in the list.
Writing like [input () for i in range (n)] is called __list comprehension __. It repeats input () n times and stores it in order in the list. It's often used because it makes the list simple, but it looks complicated until you get used to it, so you don't have to learn how it works. (If you tackle the problem many times, you will naturally understand it.)
The above code has the same content as the following code.
source.py
n = int(input())
list_a = []
for i in range(n):
kudamono = input()
list_a.append(kudamono)
input
3 #Represents the number of input lines
1
2
3
code
source.py
n = int(input())
list_a = [int(input()) for i in range(n)]
output.py
[1, 2, 3]
It's okay if you convert the input () part of the previous code to an int (input ()) and convert each input to a number.
input
3
a b
c d
e f
code
source.py
n = int(input())
list_a = [input().split() for i in range(n)]
output.py
[['a', 'b'], ['c', 'd'], ['e', 'f']]
['c', 'd'] #print(list_a[1])
e #print(list_a[2][0])
If you have multiple lines of input like this, in most cases it will work if you receive it as a two-dimensional array. As the content of "Multiple inputs per line", input (). Split () is a list by itself, so it is easy to imagine if you think that the work of entering a large list_a list is repeated 3 times. think.
input
3 #Represents the number of input lines
1 2
3 4
5 6
code
source.py
n = int(input())
list_a = [list(map(int,input().split())) for i in range(n)]
output.py
[[1, 2], [3, 4], [5, 6]]
It looks complicated, but I'm repeating the work when the input is one line. You can see that this also repeats the work of "multiple inputs per line (when you want to make numbers)" for the number of input lines.
As I mentioned at the beginning, you don't have to try to remember everything all at once. __ Even with copy-paste at first, you will naturally come to your mind as you challenge various problems and deepen your understanding of the language. I hope this page helps you.
If you find something difficult to understand, please leave it in the comment section.
Recommended Posts