Python standard input summary that can be used in competition pro

-1. Introduction & front mouth

This article is almost the same as the article posted on my Hatena Blog.

I'm doing competitive programming, which has become a hot topic recently (don't look for it because it's a very weak coder ... crying).

In C ++, you can just connect >> after std :: cin (prejudice), but Python doesn't, so here are some ways to receive standard input.

The code below has been tested with Python 3.7.3.

If you have any mistakes, please let me know! !!

0. Table of contents

In the competitive programming standard, the explanation is centered on integers, but I think that the basic structure does not change whether you receive integers or strings.

1. Behavior of ʻinput ()`

input([prompt])

If the argument prompt is present, it will be written to standard output except for trailing newlines. This function then reads a line from the input, converts it to a string and returns it (except for the trailing newline). EOFError is thrown when EOF is loaded.

Source: Python 3.7.4 documentation

(^ o ^) ………… Hey.

For the time being, what you should hold down when competing professionally

I think there are two. With this in mind, let's look at an actual use case.

2. Receive standard input (1 line)

Example 1. Character (string) with one input

input

apple

Code 1

s = input()
print(s)

Output 1

apple

The type of the variable s is, of course, the str type.

If you want to list each character

Code 2

s = list(input())
print(s)

Output 2

['a', 'p', 'p', 'l', 'e']

The type of the variable s is the list type.

Example 2. An integer with one input

input

334

Code 3

n = int(input())
print(n)

Output 3

334

Since ʻinput ()returns334 of type str, it is enclosed in ʻint.

Therefore, the type of the variable n is ʻint` type.

Of course, even if the input is a number, if you want to treat it like a character string, you can write it as code 1 or 2 above.

Example 3. Input is multiple integers

It is overwhelmingly frequently used in competition professionals. Use a function called map.

Input (each number is separated by a space)

334 -334 364

Code 4

a, b, c = map(int, input().split())
print(a, b, c)

Output 4

334 -334 364

If you write down what you are doing

  1. ʻinput (). split ()returns a list with each integer separated by a half-width space as an element (in the above example,['334','-334',, '364'] `). I wrote "integer as an element", but Python says these are still a list of strings.

  2. map is simply a function that applies the same processing to all elements of a list (or something). Here, the integers treated as strings are converted to ʻint` type.

  3. Insert each element into the variable ʻa b`` c`.

In Code 4, I prepared three variables and put them in each, but of course you can also list them.

Code 5

a = list(map(int, input().split()))
print(a)

Output 5

[334, -334, 364]

One thing to note is that you have to enclose it in list and convert it to a list type. This is because the return value of map is not a list, but a map object , something that is difficult for a fucking programmer to understand </ s>.

When receiving multiple strings </ b>, it is almost the same as codes 4 and 5. However, if it remains ʻint, ValueError Let's make itstr` properly because it spits.

(Added on 2019/12/31) </ b> When receiving multiple strings, you can simply use ʻinput (). Split ()instead of changing frommap to list`.

3. Receive standard input (multiple lines)

Basically, you just repeat the above method for the number of lines, so you can just jump into the for loop and spin it.

Here, we will describe the method using the comprehension notation.

Example 1. One integer for each row (with number of rows specified)

Most of the competition pros give you the number of lines to enter first, so here we will show you how to enter according to that.

input

5
441
484
529
576
625
  • However, the first line shows the number of lines for subsequent input.

Code 6

n = int(input())
l = [int(input()) for i in range(n)]
print(n, l)

Output 6

5 [441, 484, 529, 576, 625]

Of course, you can use it for strings by removing ʻint that encloses ʻinput ().

This is the method (link) that has recently been posted on Twitter.

Code 7

n, *l = map(int, open(0))

Similar to output 6. hyoujun.PNG

However, when testing in a local environment, if you run the program and then enter the standard input, there is a high possibility that it will not work. Honestly, I'm not sure what I'm doing, so I'll leave the reference to code 7 so much. Asakugaku is out ... (already out).

Example 2. Multiple integers in each row (number of rows and columns specified)

It seems to be used for inputting problems on a two-dimensional plane or problems in which what appears has multiple elements.

Rotate code 5 in list comprehensions.

input

5 9
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
  • However, the first row shows the number of rows and columns in order.

Code 8

n, m = map(int, input().split())
l = [list(map(int, input().split())) for i in range(n)]
print(n, m, l)

Output 8

5 9 [[1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 4, 6, 8, 10, 12, 14, 16, 18], [3, 6, 9, 12, 15, 18, 21, 24, 27], [4, 8, 12, 16, 20, 24, 28, 32, 36], [5, 10, 15, 20, 25, 30, 35, 40, 45]]

I'm sorry it's hard to see, but you can see that the variable l contains a two-dimensional list. The number of columns is not used in Python. You did it.

Example 3. One integer for each row (no number of rows specified)

I think this is more of a practical aspect than a competitive professional.

input

441
484
529
576
625

Code 9

import sys
array = []
for i in sys.stdin.readlines():
    array.append(int(i.rstrip()))
print(array)

Output 9

[441, 484, 529, 576, 625]

hyoujun2.PNG

If I confess, I will not use it in the competition pro, so I thought about it now. Is it really right ...?

Code 9 is also likely to not work well if you run the program and then enter it as standard in a local environment.

4. What I want to write about standard input

  • The big difference between ʻinput ()andsys.stdin.readline () is whether or not it contains a trailing newline character. ʻInput () excludes the trailing newline character, while sys.stdin.readline () contains the newline character. So when using sys.stdin.readline (), you often use rstrip () to strip the newline character (see Gugu for more details).

  • It seems that list comprehension is faster than writing for loops and using list comprehension (reference article: https://qiita.com/intermezzo-fr/items/43f90e07e4cebe63aeb6)

5. At the end

It was unexpectedly long when I wrote it. As I wrote at the beginning, please let me know if you have any mistakes!

Recommended Posts