0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python ショートコーディングのメモ帳

Last updated at Posted at 2023-12-30

Pythonを使ってショートコーディング(コードゴルフ)を行うときのテクニックまとめ

AtCoderでのみ確認
自分のメモの為わかりにくいものがあります
最善がほかにあるかもしれません

入力

入力
,*a=map(int,open(0)) # 各行1つのみの場合
n,*a=map(int,open(0).read().split()) # 上ではない場合
(h,w),*e=[[*map(int,t.split()]for t in open(0)] # 各行個別にしたい場合

出力

出力
print("YNoes"[f(x)::2])
print("NYoe s"[f(x)::2]) # AtCoderのみ
print(f(x)and"Alice"or"Bob")
print("Alice"*f(x)or"Bob")
print(S,sep="")
exit(print(10))

数値演算

-~n = n + 1
~-n = n - 1
-(-x//y) = (x+y-1)//y = math.ceil(x/y)
.99 # 0.99
1e7 # 10 ** 7

List関係

list
A=[]
A+=5, # A.append(5)
A+=[3,7]

[*eval("[],"*n)] # [[] for _ in range(n)]

その他

F=lambda x:x*2 # def F(x):return x*2
for a in range(h*w):i,j=a//w,a%w

for _ in[0]*n: # for _ in range(n)
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?