Coding Test/programmers

[Python] 파이썬 프로그래머스 모음사전

파송송 2023. 6. 30. 19:02
728x90

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

 

프로그래머스

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

programmers.co.kr


나의 풀이

처음에 사전의 규칙을 파악하는데 시간이 좀 걸렸다.

A

AA

AAA

AAAA

AAAAA

AAAAE

AAAAI

AAAAO

AAAAU

AAAE

AAAEA

AAAEE

AAAEI

AAAEO

AAAEU

다음과 같은 규칙을 가지고 백트래킹을 쓰면 되겠다고 생각했다.

word_list를 통해 모든 사전을 구하고 답을 찾는 거라 시간이 오래 걸린다.


def solution(word):
    answer = 0
    word_list = []
    words = "AEIOU"
         
    def all_word(cnt, w):
        if cnt == 5:
            return
        for i in range(len(words)):
                word_list.append(w + words[i])
                all_word(cnt + 1, w + words[i])
            
    all_word(0, "")

    return word_list.index(word) + 1


다른 사람의 풀이

join과 product를 통해 input까지만 해서 구함

from itertools import product

solution = lambda word: sorted(["".join(c) for i in range(5) for c in product("AEIOU", repeat=i+1)]).index(word) + 1

728x90