Coding Test
[프로그래머스] 10주차_교점에 별 만들기
상상쓰
2021. 10. 12. 16:26
https://programmers.co.kr/learn/courses/30/lessons/87377
코딩테스트 연습 - 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
문제에 참고 사항에 친절하게 해를 구하는 공식이 나타나 있다. 이를 활용하여 해가 정수인 부분에 대해서 result 배열에 넣어주고 가로, 세로 최대 크기를 구하여 구한 좌표에 맞게 result 의 원소(교점)를 평행 이동하여 '.' 을 '*' 로 만들어 준다.
배열로 나타낼 때 y 의 값은 x 와 달리 배열과 부호가 반대이므로 해를 구할 때 미리 -1 을 곱한 값으로 나타냈다.
def solution(line):
answer = []
result = []
for i in range(len(line)-1):
for j in range(i+1, len(line)):
s = solve(line[i], line[j])
if s != None:
r1 = min(r1, s[1]) if len(result) != 0 else s[1]
r2 = max(r2, s[1]) if len(result) != 0 else s[1]
c1 = min(c1, s[0]) if len(result) != 0 else s[0]
c2 = max(c2, s[0]) if len(result) != 0 else s[0]
result.append(s)
answer = [['.'] * (c2 - c1 + 1) for i in range(r2 - r1 + 1)]
for x, y in result:
answer[y - r1][x - c1] = '*'
for i in range(len(answer)):
answer[i] = ''.join(answer[i])
return answer
def solve(eq1, eq2):
A, B, E = eq1
C, D, F = eq2
if A * D - B * C != 0 and (B * F - E * D) % (A * D - B * C) == 0 and (E * C - A * F) % (A * D - B * C) == 0:
return [(B * F - E * D) // (A * D - B * C), - (E * C - A * F) // (A * D - B * C)]
else:
return None
print(solution([[2, -1, 4], [-2, -1, 4], [0, -1, 1], [5, -8, -12], [5, 8, 12]])) # ['....*....', '.........', '.........', '*.......*', '.........', '.........', '.........', '.........', '*.......*']