Algorithm/Programmers
[Programmers] 코딩테스트 입문 #2 (with Python)
j4ko
2024. 4. 25. 00:20
728x90
반응형
목차
특이한 정렬
https://school.programmers.co.kr/learn/courses/30/lessons/120880/questions
from collections import defaultdict
def solution(numlist, n):
index_lst = [abs(n - e) for e in numlist]
inverse = [(idx, value) for idx, value in enumerate(index_lst)]
offset_group = {} # 같은 offset 위치를 같은 그룹으로 묶기
for index, value in sorted(inverse, key=lambda x: x[1]):
if value not in offset_group:
offset_group[value] = [index]
else:
offset_group[value].append(index)
answer = []
for _, index in offset_group.items():
index = sorted([numlist[i] for i in index])
for e in sorted(index, reverse=True):
answer.append(e)
return answer
중복된 문자 제거
https://school.programmers.co.kr/learn/courses/30/lessons/120888
def solution(my_string):
hmap = {}
for ch in my_string:
if ch not in hmap:
hmap[ch] = 0
return ''.join(hmap.keys())
문자열 카운트를 활용하는것도 좋은 방법같다.
def solution(my_string):
answer = []
for i in my_string :
if answer.count(i) == 0 :
answer.append(i)
return ''.join(answer)
728x90
반응형