Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 1차원 DP
- 2차원 dp
- 99클럽
- @BeforeAll
- @BeforeEach
- @Builder
- @Entity
- @GeneratedValue
- @GenericGenerator
- @NoargsConstructor
- @Query
- @Table
- @Transactional
- Actions
- Amazon EFS
- amazon fsx
- Android Studio
- ANSI SQL
- api gateway 설계
- api gateway 필터
- ApplicationEvent
- argocd
- assertThat
- async/await
- AVG
- AWS
- aws autoscaling
- aws eks
- aws iam role
- AWS KMS
Archives
- Today
- Total
기록
프로그래머스_python_교점에 별 만들기 본문
문제
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방향이 좌표와 반대이므로 뒤집어서 출력
'코딩테스트 > python' 카테고리의 다른 글
프로그래머스_python_퍼즐 조각 채우기 (1) | 2021.12.24 |
---|---|
프로그래머스_python_보석 쇼핑 (0) | 2021.12.23 |
프로그래머스_python_다단계 칫솔 판매 (0) | 2021.12.22 |
프로그래머스_피로도 (0) | 2021.12.13 |
프로그래머스_거리두기 확인하기 (0) | 2021.08.28 |
Comments