Algorithm

[1일 3알고리즘] Day6

  • -
728x90

1. 로또의 최고 순위와 최저 순위 (추후 Refactor)

https://school.programmers.co.kr/learn/courses/30/lessons/77484

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

def solution(lottos, win_nums):
    ans = []
    wins = 0
    for i in range(len(win_nums)):
        if lottos[i] in win_nums:
            win_nums.remove(lottos[i])
            wins += 1

    cnt = lottos.count(0)
    lessWin = wins

    if cnt <= len(win_nums):
        wins += cnt
    else:
        wins += len(win_nums)

    cal(wins, ans)
    cal(lessWin, ans)
    return ans


def cal(wins, ans):
    rank = {1: 6, 2: 5, 3: 4, 4: 3, 5: 2, 6: (1, 0)}
    for k, v in rank.items():
        if isinstance(v, tuple):
            if wins in v:
                ans.append(k)
        else:
            if wins == v:
                ans.append(k)

위 문제는 추후 다시 풀어볼 예정. rank를 훨씬 깔끔하게 처리할 수 있는 방법이 많았는데 아쉽다.... 너무 별로다

 

2. 다트 게임

https://school.programmers.co.kr/learn/courses/30/lessons/17682

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

import re


def solution(dartResult):
    darts = re.findall("([0-9]+)([SDT])([*#]?)", dartResult)
    score = [0] * len(darts)

    for i in range(len(darts)):
        word = list(darts[i])
        if word[1] == 'D':
            score[i] = int(word[0]) ** 2
        elif word[1] == 'T':
            score[i] = int(word[0]) ** 3
        else:
            score[i] = int(word[0])

        if word[2] == '*':
            score[i] *= 2
            if i != 0:
                score[i-1] *= 2
        elif word[2] == "#":
            score[i] *= -1
    return sum(score)

 

 

3. 옹알이 (2)

https://school.programmers.co.kr/learn/courses/30/lessons/133499

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

아래는 시도했던 풀이이다. (완전 틀린 풀이)

def solution(babbling):
    ans = 0
    possible = ["aya", "ye", "woo", "ma"]
    i = 0
    recent = ""
    for word in babbling:
        while 1:
            # 같은 옹알이
            if recent == possible[i % len(possible)]:
                break
            else:
                word = word.replace(possible[i % len(possible)], "", 1)
                recent = possible[i % len(possible)]
            if word in possible:
                i += 1
                continue
            else:
                break
            # for baby in possible:
            #     # TODO: 4번보다 더 될 수 있는 경우 검출하기 - 같은 발음이 연속되지만 않으면 됨 ex. ayayewooyema
            #     word = word.replace(baby, "", 1)

        if len(word) == 0:
            ans += 1
    return ans

접근 자체를 replace로 가다보니 문제가 많았다. 주어진 문장에 없는 옹알이도 그냥 replace 해버리니까 플래그를 세울수가 없었다. 옹알이를 딱 4번만 반복한다면 그냥 for문을 돌리면 되지만, 옹알이가 4번이 넘는 경우 (ex. "ayayewooyema")를 처리할 수가 없었다. 그래서 이것저것 시도해보다가 결국 포기했다.

 

def solution(babbling):
    ans = 0
    possible = ["aya", "ye", "woo", "ma"]

    for word in babbling:
        comp = ""
        temp = ""

        for char in word:
            comp += char

            if comp == temp:
                break

            if comp in possible:
                temp = comp
                comp = ""
        if comp == "":
            ans += 1

    return ans

위와 같이 다른 분의 풀이를 참조하여 완성하였다. 쉬운 문제일 줄 알았는데 풀지 못해서 아쉽다

'Algorithm' 카테고리의 다른 글

[1일 3알고리즘] Day7  (0) 2024.04.21
[1일 3알고리즘] Day5  (0) 2024.04.17
[1일 3알고리즘] Day4  (0) 2024.04.16
[1일 3알고리즘] Day3  (0) 2024.04.15
[1일 3알고리즘] Day2  (0) 2024.04.12
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.