This is an introduction to the short writing technique in Python 3 that has been cultivated with AtCoder. In addition, Python3 version of AtCoder is 3.4.3, and you may not be able to use functions that are likely to be shortened, such as f-strings and walrus operators. It's just a technique on AtCoder (I don't know if it can be used elsewhere). I think it's messy because I write down what I came up with. Excuse me.
When you think of Python's standard input, you think of input (), but when writing short, open (0) is more likely to be used than input (). open is a function that opens a file, but by specifying 0 as the first argument, it will be read from the standard input. For example
4
1
2
3
4
Given input like, if you want to store the first row in the variable n and the remaining rows in the list a
n,*a=map(int,open(0))
Can be written as. Also,
5 4
1 2 3 4 5
Given input like, if you want to store the first line in n and k and the second line in list a
n,k,*a=map(int,open(0).read().split())
Can be written as.
In some cases it may be useful to use eval. eval executes the string given as an argument as an expression. Therefore, it may be shortened by processing the input string appropriately and passing it to eval. For example
10 20
In the case of a problem where two numbers are given in one line and the product is output,
print(eval(input().replace(' ','*')))
Can be written as. in this case, By replacing ‘’ in the input string with ‘*’
10*20
It becomes a character string, and the solution can be obtained by executing this as an expression. Also, if the constraint has two numbers less than 100,
s=input()
print(int(s[:2])*int(s[2:]))
You can also write.
If you want to output the contents of a list a separated by spaces,
print(*a)
Can be written as. If you want to separate this from line breaks
print(*a,sep='\n')
You can do it. In AtCoder, even if you want to output the solution separated by line breaks, it is often output by separating them with spaces. YNeos I think many people think of this when they hear Python golf. For problems such as outputting'Yes' when certain conditions are met and'No' when not.
print('YNeos'[Conditional expression::2])
And so on. In addition, since'No'is output when the conditional expression becomes True, if the condition is forcibly turned over and it becomes long,
print('NYoe s'[Conditional expression::2])
You can also do it like this. In this case, when the conditional expression becomes False,'No' (blank at the end) is output, but AtCoder often passes even in such a case.
In Python, you can use semicolons as sentence breaks in addition to line breaks. For example, when writing multiple processes with a for statement
for _ in'_'*n:hoge;fuga;piyo
And so on. Also, in the case of repeating the same process n times like this, use a function that executes the character string given by the argument exec as an expression.
exec('hoge;fuga;piyo;'*n)
Can be written as.
Since bool is a subclass of int, you can perform operations on int. For example, if you want to add 1 to a when a certain condition is met and 1 to b when it is not met.
f=Conditional expression
a+=f
b+=1-f
And so on.
When you want to add a trailing 0 to a list a
a+=[0]
Just add a list of size 1 like
a+=0,
You can also add tuples like this. If you want to add only when certain conditions are met
a+=[0]*Conditional expression
Can be written as import For example
from numpy import*
By writing like, you can use NumPy functions without adding np.
Divide n by m (round up)
0--n//m
(n+1)*2
n+1<<1
Or
-~n*2
(n-1)*2
~-n*2
Here are some links to some of the interesting things that ABC-A submitted.
https://atcoder.jp/contests/abc092/submissions/4568294 with eval
+min(int(input()),int(input()),)+min(int(input()),int(input()),)
Is executed as an expression.
https://atcoder.jp/contests/abc115/submissions/4390864 input can output arguments.
https://atcoder.jp/contests/abc122/submissions/4701952 https://atcoder.jp/contests/abc119/submissions/4376176
If you come up with something, I will add it.
Recommended Posts