백준 10815. 숫자 카드 코드 분석

고봉진·2023년 3월 1일
0

TIL/코딩테스트

목록 보기
9/27

lycoris1600님의 코드:

p,q,r,s=open(0)
d={*q.split()}
print(*[+(i in d)for i in s.split()])

2chanhaeng님의 코드:

def solution():
    import sys

    A, B = sys.stdin.buffer.read().splitlines()[1::2]
    A = set(A.split())
    B = B.split()
    print("".join("1 " if x in A else "0 " for x in B))

solution()
  1. buffer.read()가 빠른가?
    os.read는 unbuffered read. 즉 필요한 만큼만 따온다.
    sys.stdin.buffer.read()는 buffered read. 버퍼를 사용하면 작은 양의 데이터를 여러번 읽어와야 할 때 os를 귀찮게 할 필요가 없다(reduce the number of syscalls made and thus operate with lower context-switch overhead when reading lots of data (in particular, when you would otherwise be doing lots of short reads, buffering reduces the number of round-trips between userland and the OS kernel)).

  2. splitlines() : String에 대한 메서드. 한 줄씩 끊어서 리턴한다(Returns a list of the lines in the string, breaking at line boundaries). 개행문자로도 끊기 때문에 strip할 필요가 없다. (keepend=True 일때는 개행문자 보존)

  3. +(i in d) : 불리언 값 앞에 + 기호로 숫자로 변환. True -> 1, False -> 0.

profile
이토록 멋진 휴식!

0개의 댓글