알고리즘/프로그래머스
[프로그래머스] 위클리 챌린지_10주차_교점에 별 만들기
엉아_
2021. 10. 21. 22:56
728x90
📕 문제
https://programmers.co.kr/learn/courses/30/lessons/87377?language=python3
코딩테스트 연습 - 10주차_교점에 별 만들기
[[2, -1, 4], [-2, -1, 4], [0, -1, 1], [5, -8, -12], [5, 8, 12]] ["....*....", ".........", ".........", "*.......*", ".........", ".........", ".........", ".........", "*.......*"] [[0, 1, -1], [1, 0, -1], [1, 0, 1]] ["*.*"] [[1, -1, 0], [2, -1, 0], [4, -
programmers.co.kr
💡 풀이법
1. combinations을 써서 각 직선들의 조합마다 교점을 구해준다.
2. min_x, min_y, max_x, max_y는 교점중 제일 작고 큰 x,y 좌표를 담을 변수이다.
2. 교점의 x, y 좌표가 둘다 정수라면 point 리스트에 담아준다.
3. min_x, min_y, max_x, max_y를 갱신 시켜준다.
4. 격자판을 만들고, 교점들을 좌표이동해준 후 해당 위치를 '*'로 바꿔준다.
from itertools import combinations
def solution(line):
point = []
min_x = min_y = float('inf')
max_x = max_y = float('-inf')
for item in combinations(line, 2):
A, B, E = item[0]
C, D, F = item[1]
if A*D == B*C:
continue
px = (B*F-E*D)/(A*D-B*C)
py = (E*C-A*F)/(A*D-B*C)
if not px % 1 and not py % 1:
px, py = int(px), int(py)
point.append((px, py))
min_x = min(min_x, px)
max_x = max(max_x, px)
min_y = min(min_y, py)
max_y = max(max_y, py)
matrix = [['.' for _ in range(max_x-min_x+1)] for _ in range(max_y-min_y+1)]
for p in point:
x, y = p[0] - min_x, max_y - p[1]
matrix[y][x] = '*'
answer = []
for m in matrix:
answer.append(''.join(m))
return answer