python12 [알고리즘] 프로그래머스 | 가장 가까운 같은 글자 가장 가까운 같은 글자 답: 더보기 def solution(s): answer = [] a_dic = {} for i, alpha in enumerate(list(s)): if not alpha in a_dic: answer.append(-1) else: answer.append(i - a_dic[alpha]) a_dic[alpha] = i return answer 문제 분석 및 해석 문자열 s가 주어질 때 s의 알파벳이 처음 나왔을때는 -1, 이전에 있었던 알파벳이면 index의 차이를 배열에 더해 return하는 문제 >> 풀이생각 1. stack을 쌓기, stack에 없으면 -1 2. -1번에 있으면 1 -2번에 있으면 2... while문으로 처리해보자 단점 : >> if not 요소 in sta.. 2023. 11. 7. [알고리즘] 프로그래머스 | 대충 만든 자판, 추억 점수 추억 점수 답: 더보기 def solution(name, yearning, photo): answer = [0] * len(photo) yearn = {} for k, v in zip(name, yearning): yearn[k] = v for i, p in enumerate(photo): for name in p: if name in yearn: answer[i] += yearn[name] return answer 문제 분석 및 해석 각각의 사람마다 추억 점수가 있다. 사진에는 여러 사람이 들어가 있는데, name 과 yearning을 바탕으로 각 photo의 추억점수를 계산하고자 한다 입력 name 이름이 담긴 배열 yearning 추억점수가 담긴 배열 - name과 yearning의 길이는 동일 p.. 2023. 11. 6. [알고리즘] 프로그래머스 | 달리기경주 달리기 경주 답: 더보기 def solution(players, callings): player = {} # 이름 : 등수 ranking = {} # 등수 : 이름 for i, p in enumerate(players): player[p] = i ranking[i] = p for call in callings: idx = player[call] pre_idx = idx-1 pre_call = ranking[pre_idx] ranking[pre_idx], ranking[idx] = call, pre_call player[pre_call], player[call] = idx, pre_idx answer = sorted(player, key=lambda x : player[x]) return answer 문제.. 2023. 10. 24. [알고리즘] 프로그래머스 | 소수 만들기 소수만들기 답: 더보기 식 하나로 만들기 from itertools import combinations def solution(nums): result = 0 n_list = list((combinations(nums, 3))) for number in n_list: num = sum(number) new_num = int(num ** 0.5) + 1 for i in range(2, new_num): if num % i == 0: break else: result += 1 return result 함수로 나누기 from itertools import combinations def is_prime(n): new_num = int(n**0.5+1) for i in range(2, new_num): if n .. 2023. 8. 15. 이전 1 2 3 다음