코딩 기초 트레이닝 - 2 days

Joonlaxy·2023년 11월 3일
0

옹알이 (1)

첫느낌

생각보다 쉬운 느낌인걸?

가설1

네 가지 발음("aya", "ye", "woo", "ma")을 빈 문자(스페이스)로 변환하여, 최종 리스트에 남은 빈 문자열의 개수를 빼는 방향으로 접근

def solution(babbling):
    answer = len(babbling)
    n = []
    for i in babbling:
        n.append((((i.replace('aya', ' ')).replace('ye', ' ')).replace('woo', ' ')).replace('ma', ' '))
    _n = []
    for i in n:
        _n.append(i.replace(' ', ''))
    answer = abs(len([item for item in _n if item != '']) - answer)

    return answer

결과

후기

스페이스 한 칸을 빈 문자열로 만드는 작업을 추가하여 문제를 해결하였다.
더 쉬운 방법이 있는것 같은데, 한번에 성공해서 다음 문제를 알아봐야겠다.

평행

첫느낌

기울기를 구하면 쉽겠는데?

가설1

네 점을 조합하면 6가지 기울기가 구해지는데, 이때 두 기울기가 같으면 평행한다.

def solution(dots):
    answer = 0
    # y = ax + b, y = a'x + b' 
    # 두 일차 방정식이 평행하다 a = a'
    # 1번, 2번 index로 a를 구하고
    # 1번, 3번 index로 a'를 구해 비교하고
    # 1번, 4번 index로 a''를 구해 비교하는 loop
    _a = []
    for i in range(len(dots)):
        for j in range(i+1, len(dots)):
            x_1 = dots[i][0]
            x_2 = dots[j][0]
            y_1 = dots[i][1]
            y_2 = dots[j][1]
            try:
                _a.append((x_2 - x_1) / (y_2 - y_1))
            except:
                pass
    seen = []
    for i in _a:
        if i in seen:
            return 1
        seen.append(i)
    return 0

12번부터 틀리다..

가설2

"네 개의 점을 두 개씩 이었을 때," 라는 이야기는 혹시 점1-점2 직선이 생기면, 점3-점4 직선 한가지 조건, 점1-점3 직선은 점2-점4 직선과 비교.. 이런 내용이 아닐까?

def solution(dots):
    answer = 0
    case = []
    # case1
    _x = dots[0][0] - dots[1][0]
    _y = dots[0][1] - dots[1][1]
    case.append(_x/_y)
    _x = dots[2][0] - dots[3][0]
    _y = dots[2][1] - dots[3][1]
    case.append(_x/_y)
    if case[0] == case[1]:
        return 1
    case = []
    # case2
    _x = dots[0][0] - dots[2][0]
    _y = dots[0][1] - dots[2][1]
    case.append(_x/_y)
    _x = dots[1][0] - dots[3][0]
    _y = dots[1][1] - dots[3][1]
    case.append(_x/_y)
    if case[0] == case[1]:
        return 1
    case = []
    # case3
    _x = dots[0][0] - dots[3][0]
    _y = dots[0][1] - dots[3][1]
    case.append(_x/_y)
    _x = dots[1][0] - dots[2][0]
    _y = dots[1][1] - dots[2][1]
    case.append(_x/_y)
    if case[0] == case[1]:
        return 1
    return 0

결과

후기

하드코딩으로 문제를 해결했는데, 더 쉬운 방법은 딱히 없어보인다.

profile
Come in here

0개의 댓글