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

현지·2021년 9월 5일
0

프로그래머스

목록 보기
5/34

문제

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

사전에 알파벳 모음 'A', 'E', 'I', 'O', 'U'만을 사용하여 만들 수 있는, 길이 5 이하의 모든 단어가 수록되어 있습니다. 사전에서 첫 번째 단어는 "A"이고, 그다음은 "AA"이며, 마지막 단어는 "UUUUU"입니다.

단어 하나 word가 매개변수로 주어질 때, 이 단어가 사전에서 몇 번째 단어인지 return 하도록 solution 함수를 완성해주세요.

제한사항

  • word의 길이는 1 이상 5 이하입니다.
  • word는 알파벳 대문자 'A', 'E', 'I', 'O', 'U'로만 이루어져 있습니다.

입출력 예시

아이디어

  1. itertools에 product를 이용하여 각 모음이 2,3,4,5의 개수로 반복되는 리스트를 만든다.
  2. 만들어진 모든 리스트를 더한다.
  3. 리스트를 sort시키고 해당 인덱스를 찾아 1을 더해준다.

solution 함수_python

import itertools

def solution(word):
    list1 = ['A','E','I','O','U']
    list2 = list(map(list, itertools.product('AEIOU',repeat=2)))
    list3 = list(map(list, itertools.product('AEIOU',repeat=3)))
    list4 = list(map(list, itertools.product('AEIOU',repeat=4)))
    list5 = list(map(list, itertools.product('AEIOU',repeat=5)))
    data_list = list1 + list2 + list3 + list4 + list5
    for i in range(len(data_list)):
        data_list[i] = ''.join(data_list[i])
    data_list.sort()

    return data_list.index(word)+1

0개의 댓글