코딩테스트/python
프로그래머스_python_교점에 별 만들기
zyin
2021. 12. 22. 06:01
문제
https://programmers.co.kr/learn/courses/30/lessons/87377
코딩테스트 연습 - 교점에 별 만들기
[[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. 교점 구하기
참고로 주어진 식을 이용한다.
2. 교점의 가장 큰/작은 x, y값 구하기
arr의 크기를 정하기 위해 가장 큰 x값, 가장 작은 x값, 가장 큰 y값, 가장 작은 y값이 필요하다.
아래에서는 정렬을 해서 각 값들을 구했다.
3. 배열에 별 그리기
배열과 좌표는 행이 증가하는 방향이 반대이다. 따라서 위 아래를 뒤집어서 제출한다.
코드
def solution(line):
# 교점구하기
nodeList = list()
for i in range(len(line)) :
a, b, e = line[i]
for c, d, f in line[i+1:] :
if a*d-b*c==0 : continue
x = (b*f-e*d)/(a*d-b*c)
y = (e*c-a*f)/(a*d-b*c)
if int(x)==x and int(y)==y and (x, y) not in nodeList:
nodeList.append((int(x), int(y)))
# 교점의 가장 큰/작은 값 구하기
nodeList.sort(key = lambda x : x[0])
minx, maxx = nodeList[0][0], nodeList[-1][0]
nodeList.sort(key = lambda x : x[1])
miny, maxy = nodeList[0][1], nodeList[-1][1]
# 배열에 별 그리기
arr = [["."for j in range(maxx-minx+1)] for i in range(maxy-miny+1)]
for x, y in nodeList :
r, c = y-miny, x-minx
arr[r][c] = "*"
return ["".join(item) for item in arr][::-1] # r방향이 좌표와 반대이므로 뒤집어서 출력