[Codility] AbsDistinct

snusun·2021년 11월 28일
0

Codility

목록 보기
12/13

AbsDistinct

1차 시도

def solution(A):
    # write your code in Python 3.6
    B=[]
    count=0
    for i in A:
        if abs(i) not in B:
            B.append(abs(i))
            count+=1
    return count

답은 맞는데 역시나 time error 나는군

2차 시도

def solution(A):
    # write your code in Python 3.6
    B={}
    #count=0
    for i in A:
        if abs(i) not in B:
            B[abs(i)] = 1 #  . append(abs(i))
            #count+=1
    return len(B)

dictionary를 이용해서 자동으로 값이 덮어 씌워지게 해서 if 문을 생략했고, dictionary의 길이를 반환

profile
대학생 근데 이제 컴공을 곁들인

0개의 댓글