[프로그래머스]평행

allnight5·2022년 12월 5일
0

프로그래머스 입문

목록 보기
47/53

점 네 개의 좌표를 담은 이차원 배열 dots가 다음과 같이 매개변수로 주어집니다.

[[x1, y1], [x2, y2], [x3, y3], [x4, y4]]
주어진 네 개의 점을 두 개씩 이었을 때, 두 직선이 평행이 되는 경우가 있으면 1을 없으면 0을 return 하도록 solution 함수를 완성해보세요.

파이썬

def solution(dots):
    answer = 0
    #1, 2 번 연결
    ans1 = (dots[0][0]-dots[1][0])/(dots[0][1]-dots[1][1])
    #3,4번연결
    ans2 = (dots[2][0]-dots[3][0])/(dots[2][1]-dots[3][1])
    #1번 3번연결
    ans3 = (dots[0][0]-dots[2][0])/(dots[0][1]-dots[2][1])
    #2,4번 연결
    ans4 = (dots[1][0]-dots[3][0])/(dots[1][1]-dots[3][1])
    
    #1, 4 번 연결
    ans1 = (dots[0][0]-dots[3][0])/(dots[0][1]-dots[3][1])
    #2,3번연결
    ans2 = (dots[1][0]-dots[2][0])/(dots[1][1]-dots[2][1])
    if ans1 == ans2 or ans3 == ans4 :
        return 1
    return 0
profile
공부기록하기

2개의 댓글

comment-user-thumbnail
2023년 3월 3일

오타가 있소
ans5 = (dots[0][0]-dots[3][0])/(dots[0][1]-dots[3][1])
#2,3번연결
ans6 = (dots[1][0]-dots[2][0])/(dots[1][1]-dots[2][1])
if ans1 == ans2 or ans3 == ans4 or ans5 == ans6:
return 1
return 0

1개의 답글