𝘼𝙣𝙖𝙡𝙮𝙨𝙞𝙨/ᴀʟɢᴏʀɪᴛʜᴍ

[Programmers] 평행 - combinations

콜라맛갈비 2023. 7. 5. 14:42
728x90

 

https://school.programmers.co.kr/learn/courses/30/lessons/120875

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

from itertools import combinations

def solution(dots):
    com = list(combinations(dots, 2))
    for i in com :
        for j in com :
            if i[0] not in j and i[1] not in j :
                iinc = (i[1][1]-i[0][1])/(i[1][0]-i[0][0])
                jinc = (j[1][1]-j[0][1])/(j[1][0]-j[0][0])
                if iinc == jinc :
                    return 1
    return 0

 

 

from itertools import combinations

def solution(dots):
    a = []
    for (x1,y1),(x2,y2) in combinations(dots,2):
        a.append((y2-y1,x2-x1))

    for (x1,y1),(x2,y2) in combinations(a,2):
        if x1*y2==x2*y1:
            return 1
    return 0

 

 

방법 2 ) 나올 수 있는 조합의 인덱스 배열을 만드는 거

def solution(dots):
    answer = 0
    dots_idx = [[0, 1, 3, 2], [0, 2, 3, 1], [1, 2, 0, 3]]
    for d1, d2, d3, d4 in dots_idx:
        x1, y1 = dots[d1][0], dots[d1][1]
        x2, y2= dots[d2][0], dots[d2][1]
        x3, y3= dots[d3][0], dots[d3][1]
        x4, y4= dots[d4][0], dots[d4][1]
        if (y1-y2)*(x3-x4) == (y3-y4)*(x1-x2) :
            answer = 1
    return answer
728x90