728x90
def calc_meet_x_y(line1, line2):
    A, B, E = line1
    C, D, F = line2
    AD_BC = A*D - B*C
    if AD_BC == 0:
        return None
    BF_ED = B*F - E*D
    EC_AF = E*C - A*F
    return (BF_ED)/(AD_BC), (EC_AF)/(AD_BC)

def isinteger(x):
    return int(x) == x

def transform_to_new_2d(point, max_min):
    x, y = point
    return x - max_min['min_x'], y - max_min['min_y']

def solution(line):
    max_min = {}
    met_points = []
    for outer_idx in range(len(line)):
        line1 = line[outer_idx]
        for inner_idx in range(outer_idx+1, len(line), 1):
            line2 = line[inner_idx]
            calc = calc_meet_x_y(line1, line2)
            if calc:
                x, y = calc
                if isinteger(x) and isinteger(y):
                    x, y = int(x), int(y)
                    if max_min.get('max_x', x) <= x:
                        max_min['max_x'] = x
                    if max_min.get('max_y', y) <= y:
                        max_min['max_y'] = y
                    if max_min.get('min_x', x) >= x:
                        max_min['min_x'] = x
                    if max_min.get('min_y', y) >= y:
                        max_min['min_y'] = y
                    met_points.append((x,y))


    outer_array_len = max_min['max_y'] - max_min['min_y'] + 1
    inner_array_len = max_min['max_x'] - max_min['min_x'] + 1

    answer = [["."]*inner_array_len for _ in range(outer_array_len)]
    for point in met_points:
        x, y = transform_to_new_2d(point, max_min)
        answer[y][x] = '*'
    answer = [''.join(row) for row in answer]
    return answer[::-1]
  • 포인트
    • numeric x가 integer인지 확인은 int(x) == x
    • 계속 max_x, max_y, min_x, min_y를 추적하기
    • string은 immutable이니까 2d-array로 answer template 만들기(not 1d of str)
728x90

+ Recent posts