개발자 톡

커뮤니티 톡 자유 주제

런타임 에러

등록일
2021-10-10 23:24:28
조회수
739
작성자
rokafhawk

테스트 코드를 돌리면 수행이 잘되는데, 결과 제출을 하면 런타임 오류가 발생합니다.


이럴 때는 어떻게 체크를 하면 좋을까요?


def check_array_sum_is_zero(the_list):
    """
    2차원 배열의 합계를 체크한다.
    :param the_list: 2차원 배열
    :return: 배열 원소의 합계
    """
    tot_sum = 0
    array_rows = len(the_list)

    for index in range(array_rows):
        tot_sum = tot_sum + sum(the_list[index])

    return tot_sum


def remove_blank_from_list(the_list):
    """
    2차원 배열에서 공백 원소를 제거하고 새로운 배열을 리턴함
    :param the_list: 2차원 배열
    :return: 공백 원소를 제외한 2차원 배열
    """
    return [int(value) for value in the_list if value != ' ']


def check_closed(the_list, param_row, param_col):
    """
    해당 주소가 벽으로 막혀 있는지 체크함
    :param the_list: 2차원 배열
    :param param_row: 행
    :param param_col: 열
    :return: 막혀 있는지 뚫려 있는지 True/False
    """
    return_value = False
    check_left_row = the_list[param_row][:param_col].count(1)
    if check_left_row > 1:
        check_left_row = 1

    check_right_row = the_list[param_row][param_col+1:].count(1)
    if check_right_row > 1:
        check_right_row = 1

    check_columns = []
    for inx in the_list:
        check_columns.append(inx[param_col])

    check_left_col = check_columns[:param_row].count(1)
    if check_left_col > 1:
        check_left_col = 1

    check_right_col = check_columns[param_row+1:].count(1)
    if check_right_col > 1:
        check_right_col = 1

    sum_digit = check_left_col + check_right_col + check_right_row + check_left_row

    if sum_digit >= 4:
        return_value = True

    return return_value


# 사용자 입력
row, col = map(int, input().split())

# 초기 입력 매트릭스
matrix = []

# 한 행씩 입력을 받는다.
for i in range(row):
    matrix.append(list(input()))

row = len(matrix)
col = len(matrix[0])

# 공백을 제거한 새로운 매트릭스
remove_blank_matrix = []
for i in range(row):
    remove_blank_matrix.append(remove_blank_from_list(matrix[i]))

# 새로운 매트릭스의 행과 열
new_row = len(remove_blank_matrix)
new_col = len(remove_blank_matrix[0])

test_hour = 0

while check_array_sum_is_zero(remove_blank_matrix) > 0:
    # 0인 값들에 대해서 막혀 있는 주소인지 체크한다.
    closed_array = []
    for closed_row_index in range(new_row):
        for closed_col_index in range(new_col):
            if closed_row_index > 0 and remove_blank_matrix[closed_row_index][closed_col_index] == 0:
                ret = check_closed(remove_blank_matrix, closed_row_index, closed_col_index)
                if ret:
                    closed_array.append([closed_row_index, closed_col_index])
    # 기존 매트릭스의 값을 변경함
    # @todo
    print('closed_array', closed_array)
    for closed_replace in closed_array:
        remove_blank_matrix[closed_replace[0]][closed_replace[1]] = 2

    result_array = []
    for i in range(new_row):
        for j in range(new_col):
            zero_cnt = 0
            if remove_blank_matrix[i][j] == 1:
                if remove_blank_matrix[i][j - 1] == 0:
                    zero_cnt = zero_cnt + 1
                if remove_blank_matrix[i][j + 1] == 0:
                    zero_cnt = zero_cnt + 1
                if remove_blank_matrix[i - 1][j] == 0:
                    zero_cnt = zero_cnt + 1
                if remove_blank_matrix[i + 1][j] == 0:
                    zero_cnt = zero_cnt + 1

            if zero_cnt > 1:
                result_array.append([i, j])

    # 기존 매트릭스의 값을 원복함
    # @todo
    for closed_replace in closed_array:
        remove_blank_matrix[closed_replace[0]][closed_replace[1]] = 0

    # 수행 시간 +1
    test_hour = test_hour + 1

    # 기존 값을 변경한다
    for replace in result_array:
        remove_blank_matrix[replace[0]][replace[1]] = 0
    print('result_array', result_array)

print(test_hour)



#python
#동계_테스트_시점_예측

이 카테고리의 톡 더보기