데이터사이언스 기록기📚

[프로그래머스] Level 2_모음사전(완전탐색) 본문

Coding Test/프로그래머스(Python)

[프로그래머스] Level 2_모음사전(완전탐색)

syunze 2022. 10. 1. 16:54

📌문제 유형

완전탐색

 

📌문제

 

프로그래머스

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

programmers.co.kr

 

📌나의 문제풀이

 - 중복순열 이용하여 단어 조합 만들기

 - 단어 조합 정렬

  •  중복제거하기 위해 set 이용

 - 인덱스 추출 위해 in 사용

from itertools import product

def solution(word):
    answer = 0
    word_dict = ['','A','E','I','O','U']
    
    # 경우의 수 이용, sort
    find_word = list(map("".join, product(word_dict,repeat = 5)))
    find_word = sorted(set(find_word))
    if word in find_word:
        return find_word.index(word)

 

📌다른 사람들의 문제풀이

 1) 유사한 풀이 - 한 줄 코드

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

 2) 문자 하나씩 다 넣기

def solution(word):
    answer = 0
    alpha  = ["A","E","I","O","U",""]
    ans = []
    for i in alpha:
        for j in alpha:
            for k in alpha:
                for l in alpha:
                    for m in alpha:
                        w = i+j+k+l+m
                        if w not in ans: ans.append(w)
    ans.sort()
    answer = ans.index(word)
    return answer

 

📌리뷰

 - 완전탐색 : 수학적으로 생각하기

 - 정렬에 대해 다시 생각하기

 

📌참조

  - 순열, 조합, 중복순열, 중복조합

 

(Python) 순열, 조합, 중복순열, 중복조합 쉽게 구현하기

<!DOCTYPE html> (Python) 순열, 조합, 중복순열, 중복조합 구현하기 (Python) 순열, 조합 쉽게 만들기¶ 결론부터 말하자면, 라이브러리에서 불러온 함수와 직접 구현한 함수가 속도차이 10배정도를 보였

juhee-maeng.tistory.com

 

728x90
Comments