Python Golf Tech (AtCoder)

Introduction

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.

Standard input

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.

output

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.

semicolon

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.

bool arithmetic

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.

Append to list

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.

Various operations

Divide n by m (round up)

0--n//m

(n+1)*2

n+1<<1

Or

-~n*2

(n-1)*2

~-n*2

Other

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

in conclusion

If you come up with something, I will add it.

Recommended Posts

Python Golf Tech (AtCoder)
atCoder 173 Python
AtCoder ABC 174 Python
AtCoder ABC187 Python
AtCoder ABC188 Python
AtCoder ABC 175 Python
Daily AtCoder # 36 in Python
Daily AtCoder # 2 in Python
Daily AtCoder # 32 in Python
Daily AtCoder # 18 in Python
Daily AtCoder # 33 in Python
Daily AtCoder # 7 in Python
Daily AtCoder # 24 in Python
Solve AtCoder 167 with python
Daily AtCoder # 8 in Python
Daily AtCoder # 42 in Python
Daily AtCoder # 21 in Python
Daily AtCoder # 17 in Python
Daily AtCoder # 38 in Python
Daily AtCoder # 54 in Python
Daily AtCoder # 11 in Python
Daily AtCoder # 15 in Python
Daily AtCoder # 47 in Python
Daily AtCoder # 13 in Python
Daily AtCoder # 45 in Python
Daily AtCoder # 30 in Python
Daily AtCoder # 40 in Python
Daily AtCoder # 10 in Python
Daily AtCoder # 5 in Python
Daily AtCoder # 28 in Python
Daily AtCoder # 39 in Python
Automate AtCoder submission (Python)
Daily AtCoder # 20 in Python
Daily AtCoder # 19 in Python
Daily AtCoder # 52 in Python
Daily AtCoder # 3 in Python
Daily AtCoder # 14 in Python
Daily AtCoder # 50 in Python
Daily AtCoder # 26 in Python
Daily AtCoder # 4 in Python
Daily AtCoder # 43 in Python
Daily AtCoder # 29 in Python
Daily AtCoder # 49 in Python
Daily AtCoder # 27 in Python
Daily AtCoder # 1 in Python
Daily AtCoder # 25 in Python
Daily AtCoder # 16 in Python
Daily AtCoder # 12 in Python
Daily AtCoder # 48 in Python
Daily AtCoder # 23 in Python
Daily AtCoder # 34 in Python
Daily AtCoder # 51 in Python
Daily AtCoder # 31 in Python
Daily AtCoder # 46 in Python
Daily AtCoder # 35 in Python
Daily AtCoder # 9 in Python
Daily AtCoder # 44 in Python
Daily AtCoder # 41 in Python
AtCoder ABC 177 Python (A ~ E)
Solve AtCoder ABC166 with python
Light blue with AtCoder @Python