

나의 첫번쨰 코드
from itertools import product
def solution(n):
answer = 0
list_num = ['1','2','4']
o_t_t_lang = []
i=1
while True:
for j in product(list_num, repeat=i):
o_t_t_lang.append(''.join(j))
if len(o_t_t_lang) == n:
break
if len(o_t_t_lang) == n:
break
i+=1
answer = o_t_t_lang[-1]
#print(answer)
return answer
solution(4)
중복순열을 사용해 문제를 풀어냈음 그러나 .... 그러어나 ㅠㅠ 시간 초과 두둥!!!! 시이이이이xxxx

수정된 코드
from itertools import product
def solution(n):
answer = 0
i_num=0
while True:
if n-(3**(i_num+1)) <= 0:
break
i_num+=1
n-=3**i_num
list_num = ['1','2','4']
lang_one_two_three = []
for i in product(list_num, repeat=i_num+1):
lang_one_two_three.append(''.join(i))
if len(lang_one_two_three)==n:
break
#print(n)
#print(lang_one_two_three)
answer = lang_one_two_three[n-1]
#print(answer)
return answer
solution(4)

2배 넘게 단축되긴 했지만 이게 아닌가 보다......
최종 코드
def solution(n):
answer = ''
lang_one_two_three = []
while True:
if n%3 == 0:
lang_one_two_three.append('4')
n=(n//3)-1
else:
lang_one_two_three.append(str(n%3))
n=(n//3)
if n==0:
break
lang_one_two_three = lang_one_two_three[::-1]
answer = ''.join(lang_one_two_three)
#print(answer)
return answer
solution(6)
아 솔찍히 이걸 어케아노 ㅋㅋㅋㅋ 아닌가 아직 많이 부족한건가 ㅎ.ㅎ.ㅎㅎㅎ.ㅎㅎ
'코딩테스트 파이썬 > 파이썬 프로그래머스 2단계' 카테고리의 다른 글
| 행렬의 곱셈 (0) | 2023.05.29 |
|---|---|
| n^2 배열 자르기 (0) | 2023.05.29 |
| 삼각 달팽이 (1) | 2023.05.28 |
| 연속 부분 수열 합의 개수 (0) | 2023.05.25 |
| H-Index (0) | 2023.05.25 |