50번 가장 가까운 같은 글자
> 딕셔너리를 활용한 방법
def solution(s):
answer = []
dic = dict()
for i in range(len(s)):
if s[i] not in dic:
answer.append(-1)
else:
answer.append(i - dic[s[i]])
dic[s[i]] = i
return answer
> 리스트 활용한 방법
def solution(s):
answer = []
for i in range(len(s)) :
if s[i] in s[:i] and i>0 :
temp = list(reversed(s[:i]))
answer.append(temp.index(s[i])+1)
else :
answer.append(-1)
return answer
51번 푸드 파이터 대회
def solution(food):
temp = ''
for i in range(1, len(food)):
temp += str(i) * (food[i]//2)
answer = temp + '0' + temp[::-1]
return answer
> 한 수 배워가는 다른 방법들
def solution(food):
answer ="0"
for i in range(len(food)-1, 0,-1):
c = int(food[i]/2)
while c>0:
answer = str(i) + answer + str(i)
c -= 1
return answer
def solution(food):
answer = ''
for i,n in enumerate(food[1:]):
answer += str(i+1) * (n//2)
return answer + "0" + answer[::-1]
'[스파르타코딩클럽]데이터분석 과정 > PYTHON' 카테고리의 다른 글
[Python][matplotlib](2) 다양한 그래프 유형 (0) | 2024.01.20 |
---|---|
[python][matplotlib](1) 시각화 기초 세팅 (0) | 2024.01.20 |
[Python 코드카타] 47 ~ 49번 (프로그래머스) (1) | 2024.01.19 |
[Python 코드카타] 44~46번 (프로그래머스) (0) | 2024.01.18 |
[Python 코드카타] 41 ~ 43번 (프로그래머스) (0) | 2024.01.17 |