문제

복습

from itertools import combinations
def solution(dots):
    def delta(p1,p2):
        #튜플로 변화량 리턴, (dy,dx)
        return (p1[1]-p2[1],p1[0]-p2[0])
    for a,b in combinations(2,4): #combination으로 무작위 점 2개를 고르고
        rest=[]
        for i in range(1,4):
            if(a!=i and b!=i):
                rest.append(dots[i]) #그중에서 아닌것만 rest 배열에 넣는다
    #dy,dx 나누기로 하면 소수점때문에 안맞아서 곱으로 해야한다
    
    answer = 0
    return answer

slope 함수보다는 변화량을 계산해주는거니까 delta로 이름짓는게 더 직관적일것 같았다
combination도 4C2 이렇게 구하니까 반대로 써야한다
range(1,4)는 1부터 3까지이다...

정답

from itertools import combinations
def solution(dots):
    def delta(p1,p2):
        #튜플로 변화량 리턴, (dy,dx)
        return (p1[1]-p2[1],p1[0]-p2[0])
    for a,b in combinations(range(4),2): #combination으로 무작위 점 2개를 고르고
        rest=[]
        for i in range(4):
            if(a!=i and b!=i):
                rest.append(i) #그중에서 아닌것만 rest 배열에 넣는다
    #dy,dx 나누기로 하면 소수점때문에 안맞아서 곱으로 해야한다
        dy1,dx1 = delta(dots[a],dots[b])
        dy2,dx2 = delta(dots[rest[0]],dots[rest[1]])
        if dx1*dy2 == dy1*dx2:
            return 1
    return 0
profile
여기 초과학기가 확정되어 울면서 학교를 다니는 사람이 있습니다.

0개의 댓글