-
교점에 별 만들기코딩 테스트/Level 2 2021. 10. 16. 11:23반응형
https://programmers.co.kr/learn/courses/30/lessons/87377
from itertools import combinations def solution(lines): cross_points = set() x_max = y_max = -float('inf') x_min = y_min = float('inf') for line1, line2 in combinations(lines, 2): a, b, e = line1 c, d, f = line2 denominator = a * d - b * c if denominator == 0: continue cross_point_x = (b * f - e * d) / denominator cross_point_y = (e * c - a * f) / denominator if (temp_x := int(cross_point_x)) == cross_point_x and \ (temp_y := int(cross_point_y)) == cross_point_y: cross_points.add((temp_x, temp_y)) x_min, y_min = min(x_min, temp_x), min(y_min, temp_y) x_max, y_max = max(x_max, temp_x), max(y_max, temp_y) result = [] for y in range(y_max, y_min - 1, -1): temp_line = [] for x in range(x_min, x_max + 1): if (x, y) in cross_points: temp_line.append('*') else: temp_line.append('.') result.append(''.join(temp_line)) return result
더보기from itertools import combinations def solution(lines): cross_points = {(x, y) for (a, b, e), (c, d, f) in combinations(lines, 2) if (denominator := a * d - b * c) != 0 and (x := int(cross_point_x := (b * f - e * d) / denominator)) == cross_point_x and (y := int(cross_point_y := (e * c - a * f) / denominator)) == cross_point_y} return [''.join(['*' if (x, y) in cross_points else '.' for x in range(min(cross_points, key=lambda x: x[0])[0], max(cross_points, key=lambda x: x[0])[0] + 1)]) for y in range(max(cross_points, key=lambda x: x[1])[1], min(cross_points, key=lambda x: x[1])[1] - 1, -1)]
반응형