Algorithm/Problems
99클럽 코테 스터디 7일차 TIL + 완전탐색
공부좀하시졍
2024. 11. 4. 00:20
https://school.programmers.co.kr/learn/courses/30/lessons/84512
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
from itertools import product
def solution(word):
answer = 0
vowels = ['A', 'E', 'I', 'O', 'U']
dict = []
for i in range(1,6):
for v in product(vowels, repeat=i):
dict.append(''.join(v))
dict.sort()
answer = dict.index(word)+1
return answer
- product (중복순열)
- 리스트와 같은 iterable 객체에서 r개의 데이터를 뽑아 일렬로 나열하는 모든 경우
- repeat 속성을 이용해 뽑고자 하는 데이터의 수를 정한다.
- product(vowels, repeat = i)의 결과는 ('A',), ('A','A',) ... 처럼 나오기 때문에 join 함수를 이용한다.
- join : '구분자'.join(리스트)
- index()함수를 이용해 원하는 값의 위치를 찾아 answer을 찾아주었다.
다른 사람들 풀이는 되게 다양했다.
dfs를 이용한 방법
def solution(word):
vowels = ['A', 'E', 'I', 'O', 'U']
dict = []
def dfs(cnt, w):
if cnt == 5:
return
for i in range(5):
dict.append(w+vowels[i])
dfs(cnt+1, w+vowels[i])
dfs(0,'')
return dict.index(word)+1
- dfs를 이용해 dict 리스트를 채워준다.
- A, AA, AAA, AAAA, AAAAA, AAAAE ....
- 매일 헷갈렸던 itertools 라이브러리 활용 방법과 dfs 활용 방법에 대해 다시 한번 생각 할 수 있어서 좋았다.