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?

More than 3 years have passed since last update.

【AtCorder Tips】any()でif文を省略する

Last updated at Posted at 2021-02-03

Atcorderの問題を題材に、
今後、活用できそうなコードの備忘録集

例えば、標準入力N行の値X,Yが、
それぞれ10未満、10より大きいことを確認する場合。

any()と、条件式を返すchcek()関数を組み合わると、
if文を1回省略できる。

■ if文を1回使用(参考例)
def check():
    X, Y = map(int, input().split())
    return X < 10 and 10 > D

if any(check() for i in range(N)):
    print("Yes")
else:
    print("No")
■ if文を2回使用(良くない例)
for _ in range(N):
  x,y = map(int,input().split(' '))
  
  if X < 10 and Y > 10:
    flag = 1
    break
    
  else:
    flag = 0
 
if flag == 1:
  print('Yes')
else:
  print('No')
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?